Merge tag 'v3.10.105' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / usb / em28xx / em28xx-i2c.c
CommitLineData
a6c2ba28 1/*
f7abcd38 2 em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
a6c2ba28 3
f7abcd38
MCC
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
2e7c6dc3 6 Mauro Carvalho Chehab <mchehab@infradead.org>
f7abcd38 7 Sascha Sommer <saschasommer@freenet.de>
a3ea4bf9 8 Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
a6c2ba28
AM
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/usb.h>
28#include <linux/i2c.h>
a6c2ba28 29
f7abcd38 30#include "em28xx.h"
6c362c8e 31#include "tuner-xc2028.h"
5e453dc7 32#include <media/v4l2-common.h>
d5e52653 33#include <media/tuner.h>
a6c2ba28
AM
34
35/* ----------------------------------------------------------- */
36
ff699e6b 37static unsigned int i2c_scan;
a6c2ba28
AM
38module_param(i2c_scan, int, 0444);
39MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
40
ff699e6b 41static unsigned int i2c_debug;
a6c2ba28
AM
42module_param(i2c_debug, int, 0644);
43MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
44
a6c2ba28 45/*
f5ae371a
FS
46 * em2800_i2c_send_bytes()
47 * send up to 4 bytes to the em2800 i2c device
596d92d5 48 */
f5ae371a 49static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
596d92d5
MCC
50{
51 int ret;
52 int write_timeout;
a6bad040 53 u8 b2[6];
f5ae371a
FS
54
55 if (len < 1 || len > 4)
56 return -EOPNOTSUPP;
57
596d92d5
MCC
58 BUG_ON(len < 1 || len > 4);
59 b2[5] = 0x80 + len - 1;
60 b2[4] = addr;
61 b2[3] = buf[0];
62 if (len > 1)
63 b2[2] = buf[1];
64 if (len > 2)
65 b2[1] = buf[2];
66 if (len > 3)
67 b2[0] = buf[3];
68
2fcc82d8 69 /* trigger write */
3acf2809 70 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
596d92d5 71 if (ret != 2 + len) {
d230d5ad
FS
72 em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
73 addr, ret);
45f04e82 74 return (ret < 0) ? ret : -EIO;
596d92d5 75 }
2fcc82d8
FS
76 /* wait for completion */
77 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
596d92d5 78 write_timeout -= 5) {
3acf2809 79 ret = dev->em28xx_read_reg(dev, 0x05);
45f04e82 80 if (ret == 0x80 + len - 1) {
596d92d5 81 return len;
45f04e82
FS
82 } else if (ret == 0x94 + len - 1) {
83 return -ENODEV;
84 } else if (ret < 0) {
d230d5ad
FS
85 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
86 ret);
45f04e82
FS
87 return ret;
88 }
e8e41da4 89 msleep(5);
596d92d5 90 }
45f04e82 91 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
596d92d5
MCC
92 return -EIO;
93}
94
596d92d5 95/*
2fcc82d8
FS
96 * em2800_i2c_recv_bytes()
97 * read up to 4 bytes from the em2800 i2c device
596d92d5 98 */
2fcc82d8 99static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
596d92d5 100{
2fcc82d8 101 u8 buf2[4];
596d92d5 102 int ret;
2fcc82d8
FS
103 int read_timeout;
104 int i;
105
106 if (len < 1 || len > 4)
107 return -EOPNOTSUPP;
108
109 /* trigger read */
110 buf2[1] = 0x84 + len - 1;
111 buf2[0] = addr;
112 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
113 if (ret != 2) {
d230d5ad
FS
114 em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
115 addr, ret);
2fcc82d8 116 return (ret < 0) ? ret : -EIO;
596d92d5 117 }
d45b9b8a 118
2fcc82d8
FS
119 /* wait for completion */
120 for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0;
121 read_timeout -= 5) {
122 ret = dev->em28xx_read_reg(dev, 0x05);
123 if (ret == 0x84 + len - 1) {
124 break;
125 } else if (ret == 0x94 + len - 1) {
596d92d5 126 return -ENODEV;
2fcc82d8 127 } else if (ret < 0) {
d230d5ad
FS
128 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
129 ret);
2fcc82d8
FS
130 return ret;
131 }
e8e41da4 132 msleep(5);
596d92d5 133 }
2fcc82d8
FS
134 if (ret != 0x84 + len - 1)
135 em28xx_warn("read from i2c device at 0x%x timed out\n", addr);
136
137 /* get the received message */
138 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
139 if (ret != len) {
d230d5ad
FS
140 em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
141 addr, ret);
2fcc82d8
FS
142 return (ret < 0) ? ret : -EIO;
143 }
144 for (i = 0; i < len; i++)
145 buf[i] = buf2[len - 1 - i];
146
147 return ret;
596d92d5
MCC
148}
149
150/*
2fcc82d8
FS
151 * em2800_i2c_check_for_device()
152 * check if there is an i2c device at the supplied address
596d92d5 153 */
2fcc82d8 154static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
596d92d5 155{
2fcc82d8 156 u8 buf;
596d92d5 157 int ret;
f5ae371a 158
2fcc82d8
FS
159 ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
160 if (ret == 1)
161 return 0;
162 return (ret < 0) ? ret : -EIO;
596d92d5
MCC
163}
164
165/*
3acf2809 166 * em28xx_i2c_send_bytes()
a6c2ba28 167 */
a6bad040
FS
168static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
169 u16 len, int stop)
a6c2ba28 170{
bbc70e64 171 int write_timeout, ret;
a6c2ba28 172
f5ae371a
FS
173 if (len < 1 || len > 64)
174 return -EOPNOTSUPP;
fa74aca3
FS
175 /*
176 * NOTE: limited by the USB ctrl message constraints
177 * Zero length reads always succeed, even if no device is connected
178 */
f5ae371a 179
45f04e82
FS
180 /* Write to i2c device */
181 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
182 if (ret != len) {
183 if (ret < 0) {
d230d5ad
FS
184 em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
185 addr, ret);
45f04e82
FS
186 return ret;
187 } else {
d230d5ad 188 em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
45f04e82
FS
189 len, addr, ret);
190 return -EIO;
191 }
192 }
a6c2ba28 193
45f04e82 194 /* Check success of the i2c operation */
2fcc82d8 195 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
bbc70e64
MCC
196 write_timeout -= 5) {
197 ret = dev->em28xx_read_reg(dev, 0x05);
45f04e82
FS
198 if (ret == 0) { /* success */
199 return len;
200 } else if (ret == 0x10) {
201 return -ENODEV;
202 } else if (ret < 0) {
d230d5ad
FS
203 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
204 ret);
45f04e82
FS
205 return ret;
206 }
bbc70e64 207 msleep(5);
fa74aca3
FS
208 /*
209 * NOTE: do we really have to wait for success ?
210 * Never seen anything else than 0x00 or 0x10
211 * (even with high payload) ...
212 */
bbc70e64 213 }
45f04e82
FS
214 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
215 return -EIO;
a6c2ba28
AM
216}
217
218/*
3acf2809 219 * em28xx_i2c_recv_bytes()
a6c2ba28
AM
220 * read a byte from the i2c device
221 */
a6bad040 222static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
a6c2ba28
AM
223{
224 int ret;
f5ae371a
FS
225
226 if (len < 1 || len > 64)
227 return -EOPNOTSUPP;
fa74aca3
FS
228 /*
229 * NOTE: limited by the USB ctrl message constraints
230 * Zero length reads always succeed, even if no device is connected
231 */
f5ae371a 232
45f04e82 233 /* Read data from i2c device */
3acf2809 234 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
7f6301d1
FS
235 if (ret < 0) {
236 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
237 addr, ret);
238 return ret;
45f04e82 239 }
fa74aca3
FS
240 /*
241 * NOTE: some devices with two i2c busses have the bad habit to return 0
7f6301d1
FS
242 * bytes if we are on bus B AND there was no write attempt to the
243 * specified slave address before AND no device is present at the
244 * requested slave address.
245 * Anyway, the next check will fail with -ENODEV in this case, so avoid
246 * spamming the system log on device probing and do nothing here.
247 */
45f04e82
FS
248
249 /* Check success of the i2c operation */
250 ret = dev->em28xx_read_reg(dev, 0x05);
a6c2ba28 251 if (ret < 0) {
d230d5ad
FS
252 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
253 ret);
a6c2ba28
AM
254 return ret;
255 }
45f04e82
FS
256 if (ret > 0) {
257 if (ret == 0x10) {
258 return -ENODEV;
259 } else {
260 em28xx_warn("unknown i2c error (status=%i)\n", ret);
261 return -EIO;
262 }
263 }
264 return len;
a6c2ba28
AM
265}
266
267/*
3acf2809 268 * em28xx_i2c_check_for_device()
a6c2ba28
AM
269 * check if there is a i2c_device at the supplied address
270 */
a6bad040 271static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
a6c2ba28 272{
a6c2ba28 273 int ret;
45f04e82 274 u8 buf;
a6c2ba28 275
45f04e82
FS
276 ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
277 if (ret == 1)
278 return 0;
279 return (ret < 0) ? ret : -EIO;
a6c2ba28
AM
280}
281
a3ea4bf9
FS
282/*
283 * em25xx_bus_B_send_bytes
284 * write bytes to the i2c device
285 */
286static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
287 u16 len)
288{
289 int ret;
290
291 if (len < 1 || len > 64)
292 return -EOPNOTSUPP;
293 /*
294 * NOTE: limited by the USB ctrl message constraints
295 * Zero length reads always succeed, even if no device is connected
296 */
297
298 /* Set register and write value */
299 ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
300 if (ret != len) {
301 if (ret < 0) {
302 em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
303 addr, ret);
304 return ret;
305 } else {
306 em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
307 len, addr, ret);
308 return -EIO;
309 }
310 }
311 /* Check success */
312 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
313 /*
314 * NOTE: the only error we've seen so far is
315 * 0x01 when the slave device is not present
316 */
317 if (!ret)
318 return len;
319 else if (ret > 0)
320 return -ENODEV;
321
322 return ret;
323 /*
324 * NOTE: With chip types (other chip IDs) which actually don't support
325 * this operation, it seems to succeed ALWAYS ! (even if there is no
326 * slave device or even no second i2c bus provided)
327 */
328}
329
330/*
331 * em25xx_bus_B_recv_bytes
332 * read bytes from the i2c device
333 */
334static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
335 u16 len)
336{
337 int ret;
338
339 if (len < 1 || len > 64)
340 return -EOPNOTSUPP;
341 /*
342 * NOTE: limited by the USB ctrl message constraints
343 * Zero length reads always succeed, even if no device is connected
344 */
345
346 /* Read value */
347 ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
348 if (ret < 0) {
349 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
350 addr, ret);
351 return ret;
352 }
353 /*
354 * NOTE: some devices with two i2c busses have the bad habit to return 0
355 * bytes if we are on bus B AND there was no write attempt to the
356 * specified slave address before AND no device is present at the
357 * requested slave address.
358 * Anyway, the next check will fail with -ENODEV in this case, so avoid
359 * spamming the system log on device probing and do nothing here.
360 */
361
362 /* Check success */
363 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
364 /*
365 * NOTE: the only error we've seen so far is
366 * 0x01 when the slave device is not present
367 */
368 if (!ret)
369 return len;
370 else if (ret > 0)
371 return -ENODEV;
372
373 return ret;
374 /*
375 * NOTE: With chip types (other chip IDs) which actually don't support
376 * this operation, it seems to succeed ALWAYS ! (even if there is no
377 * slave device or even no second i2c bus provided)
378 */
379}
380
381/*
382 * em25xx_bus_B_check_for_device()
383 * check if there is a i2c device at the supplied address
384 */
385static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
386{
387 u8 buf;
388 int ret;
389
390 ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
391 if (ret < 0)
392 return ret;
393
394 return 0;
395 /*
396 * NOTE: With chips which do not support this operation,
397 * it seems to succeed ALWAYS ! (even if no device connected)
398 */
399}
400
401static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
402{
403 struct em28xx *dev = i2c_bus->dev;
404 int rc = -EOPNOTSUPP;
405
406 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
407 rc = em28xx_i2c_check_for_device(dev, addr);
408 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
409 rc = em2800_i2c_check_for_device(dev, addr);
410 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
411 rc = em25xx_bus_B_check_for_device(dev, addr);
412 if (rc == -ENODEV) {
413 if (i2c_debug)
414 printk(" no device\n");
415 }
416 return rc;
417}
418
419static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
420 struct i2c_msg msg)
421{
422 struct em28xx *dev = i2c_bus->dev;
423 u16 addr = msg.addr << 1;
424 int byte, rc = -EOPNOTSUPP;
425
426 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
427 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
428 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
429 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
430 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
431 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
432 if (i2c_debug) {
433 for (byte = 0; byte < msg.len; byte++)
434 printk(" %02x", msg.buf[byte]);
435 }
436 return rc;
437}
438
439static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
440 struct i2c_msg msg, int stop)
441{
442 struct em28xx *dev = i2c_bus->dev;
443 u16 addr = msg.addr << 1;
444 int byte, rc = -EOPNOTSUPP;
445
446 if (i2c_debug) {
447 for (byte = 0; byte < msg.len; byte++)
448 printk(" %02x", msg.buf[byte]);
449 }
450 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
451 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
452 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
453 rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
454 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
455 rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
456 return rc;
457}
458
a6c2ba28 459/*
3acf2809 460 * em28xx_i2c_xfer()
a6c2ba28
AM
461 * the main i2c transfer function
462 */
3acf2809 463static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
a6c2ba28
AM
464 struct i2c_msg msgs[], int num)
465{
aab3125c
MCC
466 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
467 struct em28xx *dev = i2c_bus->dev;
468 unsigned bus = i2c_bus->bus;
a3ea4bf9 469 int addr, rc, i;
3190fbee 470 u8 reg;
a6c2ba28 471
c05b6f0a
DC
472 if (!rt_mutex_trylock(&dev->i2c_bus_lock))
473 return -EAGAIN;
aab3125c
MCC
474
475 /* Switch I2C bus if needed */
a3ea4bf9
FS
476 if (bus != dev->cur_i2c_bus &&
477 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
aab3125c 478 if (bus == 1)
3190fbee 479 reg = EM2874_I2C_SECONDARY_BUS_SELECT;
aab3125c 480 else
3190fbee
MCC
481 reg = 0;
482 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
483 EM2874_I2C_SECONDARY_BUS_SELECT);
aab3125c
MCC
484 dev->cur_i2c_bus = bus;
485 }
486
487 if (num <= 0) {
488 rt_mutex_unlock(&dev->i2c_bus_lock);
a6c2ba28 489 return 0;
aab3125c 490 }
a6c2ba28
AM
491 for (i = 0; i < num; i++) {
492 addr = msgs[i].addr << 1;
d90f0677 493 if (i2c_debug)
d7a80eaa
FS
494 printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
495 dev->name, __func__ ,
496 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
497 i == num - 1 ? "stop" : "nonstop",
498 addr, msgs[i].len);
6ea54d93 499 if (!msgs[i].len) { /* no len: check only for device presence */
a3ea4bf9 500 rc = i2c_check_for_device(i2c_bus, addr);
45f04e82 501 if (rc == -ENODEV) {
aab3125c 502 rt_mutex_unlock(&dev->i2c_bus_lock);
a6c2ba28
AM
503 return rc;
504 }
596d92d5 505 } else if (msgs[i].flags & I2C_M_RD) {
a6c2ba28 506 /* read bytes */
a3ea4bf9 507 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
a6c2ba28
AM
508 } else {
509 /* write bytes */
a3ea4bf9 510 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
a6c2ba28 511 }
45f04e82 512 if (rc < 0) {
d90f0677 513 if (i2c_debug)
45f04e82 514 printk(" ERROR: %i\n", rc);
aab3125c 515 rt_mutex_unlock(&dev->i2c_bus_lock);
45f04e82
FS
516 return rc;
517 }
d90f0677 518 if (i2c_debug)
a6c2ba28
AM
519 printk("\n");
520 }
521
aab3125c 522 rt_mutex_unlock(&dev->i2c_bus_lock);
a6c2ba28 523 return num;
a6c2ba28
AM
524}
525
fa74aca3
FS
526/*
527 * based on linux/sunrpc/svcauth.h and linux/hash.h
03910cc3 528 * The original hash function returns a different value, if arch is x86_64
fa74aca3 529 * or i386.
03910cc3
MCC
530 */
531static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
532{
533 unsigned long hash = 0;
534 unsigned long l = 0;
535 int len = 0;
536 unsigned char c;
537 do {
538 if (len == length) {
539 c = (char)len;
540 len = -1;
541 } else
542 c = *buf++;
543 l = (l << 8) | c;
544 len++;
545 if ((len & (32 / 8 - 1)) == 0)
546 hash = ((hash^l) * 0x9e370001UL);
547 } while (len);
548
549 return (hash >> (32 - bits)) & 0xffffffffUL;
550}
551
fa74aca3
FS
552/*
553 * Helper function to read data blocks from i2c clients with 8 or 16 bit
554 * address width, 8 bit register width and auto incrementation been activated
555 */
aab3125c
MCC
556static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
557 bool addr_w16, u16 len, u8 *data)
d832c5b2
FS
558{
559 int remain = len, rsize, rsize_max, ret;
560 u8 buf[2];
561
562 /* Sanity check */
563 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
564 return -EINVAL;
565 /* Select address */
566 buf[0] = addr >> 8;
567 buf[1] = addr & 0xff;
aab3125c 568 ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
d832c5b2
FS
569 if (ret < 0)
570 return ret;
571 /* Read data */
572 if (dev->board.is_em2800)
573 rsize_max = 4;
574 else
575 rsize_max = 64;
576 while (remain > 0) {
577 if (remain > rsize_max)
578 rsize = rsize_max;
579 else
580 rsize = remain;
581
aab3125c 582 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
d832c5b2
FS
583 if (ret < 0)
584 return ret;
585
586 remain -= rsize;
587 data += rsize;
588 }
589
590 return len;
591}
592
aab3125c
MCC
593static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
594 u8 **eedata, u16 *eedata_len)
a6c2ba28 595{
510e884c 596 const u16 len = 256;
fa74aca3
FS
597 /*
598 * FIXME common length/size for bytes to read, to display, hash
510e884c 599 * calculation and returned device dataset. Simplifies the code a lot,
fa74aca3
FS
600 * but we might have to deal with multiple sizes in the future !
601 */
d832c5b2 602 int i, err;
510e884c
FS
603 struct em28xx_eeprom *dev_config;
604 u8 buf, *data;
a6c2ba28 605
a217968f 606 *eedata = NULL;
510e884c 607 *eedata_len = 0;
a217968f 608
aab3125c
MCC
609 /* EEPROM is always on i2c bus 0 on all known devices. */
610
611 dev->i2c_client[bus].addr = 0xa0 >> 1;
596d92d5
MCC
612
613 /* Check if board has eeprom */
aab3125c 614 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
f2a01a00 615 if (err < 0) {
12d7ce18 616 em28xx_info("board has no eeprom\n");
c41109fc 617 return -ENODEV;
f2a01a00 618 }
596d92d5 619
a217968f
FS
620 data = kzalloc(len, GFP_KERNEL);
621 if (data == NULL)
622 return -ENOMEM;
623
d832c5b2 624 /* Read EEPROM content */
aab3125c
MCC
625 err = em28xx_i2c_read_block(dev, bus, 0x0000,
626 dev->eeprom_addrwidth_16bit,
a217968f 627 len, data);
d832c5b2 628 if (err != len) {
12d7ce18 629 em28xx_errdev("failed to read eeprom (err=%d)\n", err);
510e884c 630 goto error;
a6c2ba28 631 }
90271964 632
87b52439 633 /* Display eeprom content */
a6c2ba28 634 for (i = 0; i < len; i++) {
87b52439
FS
635 if (0 == (i % 16)) {
636 if (dev->eeprom_addrwidth_16bit)
637 em28xx_info("i2c eeprom %04x:", i);
638 else
639 em28xx_info("i2c eeprom %02x:", i);
640 }
a217968f 641 printk(" %02x", data[i]);
a6c2ba28
AM
642 if (15 == (i % 16))
643 printk("\n");
644 }
510e884c
FS
645 if (dev->eeprom_addrwidth_16bit)
646 em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
a6c2ba28 647
87b52439 648 if (dev->eeprom_addrwidth_16bit &&
a217968f 649 data[0] == 0x26 && data[3] == 0x00) {
87b52439 650 /* new eeprom format; size 4-64kb */
510e884c
FS
651 u16 mc_start;
652 u16 hwconf_offset;
653
a217968f 654 dev->hash = em28xx_hash_mem(data, len, 32);
510e884c
FS
655 mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
656
d230d5ad 657 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
510e884c
FS
658 data[0], data[1], data[2], data[3], dev->hash);
659 em28xx_info("EEPROM info:\n");
d230d5ad 660 em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
510e884c 661 mc_start, data[2]);
fa74aca3
FS
662 /*
663 * boot configuration (address 0x0002):
87b52439
FS
664 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
665 * [1] always selects 12 kb RAM
666 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
667 * [4] 1 = force fast mode and no suspend for device testing
668 * [5:7] USB PHY tuning registers; determined by device
669 * characterization
670 */
671
fa74aca3
FS
672 /*
673 * Read hardware config dataset offset from address
674 * (microcode start + 46)
675 */
aab3125c
MCC
676 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
677 data);
510e884c
FS
678 if (err != 2) {
679 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
680 err);
681 goto error;
682 }
683
684 /* Calculate hardware config dataset start address */
685 hwconf_offset = mc_start + data[0] + (data[1] << 8);
686
687 /* Read hardware config dataset */
fa74aca3
FS
688 /*
689 * NOTE: the microcode copy can be multiple pages long, but
510e884c
FS
690 * we assume the hardware config dataset is the same as in
691 * the old eeprom and not longer than 256 bytes.
692 * tveeprom is currently also limited to 256 bytes.
87b52439 693 */
aab3125c
MCC
694 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
695 data);
510e884c
FS
696 if (err != len) {
697 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
698 err);
699 goto error;
700 }
87b52439 701
510e884c
FS
702 /* Verify hardware config dataset */
703 /* NOTE: not all devices provide this type of dataset */
704 if (data[0] != 0x1a || data[1] != 0xeb ||
705 data[2] != 0x67 || data[3] != 0x95) {
706 em28xx_info("\tno hardware configuration dataset found in eeprom\n");
707 kfree(data);
708 return 0;
709 }
710
711 /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
712
713 } else if (!dev->eeprom_addrwidth_16bit &&
714 data[0] == 0x1a && data[1] == 0xeb &&
715 data[2] == 0x67 && data[3] == 0x95) {
716 dev->hash = em28xx_hash_mem(data, len, 32);
d230d5ad 717 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
510e884c
FS
718 data[0], data[1], data[2], data[3], dev->hash);
719 em28xx_info("EEPROM info:\n");
720 } else {
87b52439 721 em28xx_info("unknown eeprom format or eeprom corrupted !\n");
510e884c
FS
722 err = -ENODEV;
723 goto error;
f55eacbe
FS
724 }
725
a217968f 726 *eedata = data;
510e884c 727 *eedata_len = len;
630d2243 728 dev_config = (void *)*eedata;
a217968f 729
510e884c 730 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
a6c2ba28 731 case 0:
12d7ce18 732 em28xx_info("\tNo audio on board.\n");
a6c2ba28
AM
733 break;
734 case 1:
12d7ce18 735 em28xx_info("\tAC97 audio (5 sample rates)\n");
a6c2ba28
AM
736 break;
737 case 2:
12d7ce18 738 em28xx_info("\tI2S audio, sample rate=32k\n");
a6c2ba28
AM
739 break;
740 case 3:
12d7ce18 741 em28xx_info("\tI2S audio, 3 sample rates\n");
a6c2ba28
AM
742 break;
743 }
744
510e884c 745 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
12d7ce18 746 em28xx_info("\tUSB Remote wakeup capable\n");
a6c2ba28 747
510e884c 748 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
12d7ce18 749 em28xx_info("\tUSB Self power capable\n");
a6c2ba28 750
510e884c 751 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
a6c2ba28 752 case 0:
12d7ce18 753 em28xx_info("\t500mA max power\n");
a6c2ba28
AM
754 break;
755 case 1:
12d7ce18 756 em28xx_info("\t400mA max power\n");
a6c2ba28
AM
757 break;
758 case 2:
12d7ce18 759 em28xx_info("\t300mA max power\n");
a6c2ba28
AM
760 break;
761 case 3:
12d7ce18 762 em28xx_info("\t200mA max power\n");
a6c2ba28
AM
763 break;
764 }
12d7ce18 765 em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
510e884c
FS
766 dev_config->string_idx_table,
767 le16_to_cpu(dev_config->string1),
768 le16_to_cpu(dev_config->string2),
769 le16_to_cpu(dev_config->string3));
a6c2ba28
AM
770
771 return 0;
510e884c
FS
772
773error:
774 kfree(data);
775 return err;
a6c2ba28
AM
776}
777
778/* ----------------------------------------------------------- */
779
a6c2ba28
AM
780/*
781 * functionality()
782 */
aab3125c 783static u32 functionality(struct i2c_adapter *i2c_adap)
a6c2ba28 784{
aab3125c 785 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
aab3125c 786
a3ea4bf9
FS
787 if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
788 (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
789 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
790 } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
791 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
792 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
793 }
794
795 WARN(1, "Unknown i2c bus algorithm.\n");
796 return 0;
a6c2ba28
AM
797}
798
3acf2809
MCC
799static struct i2c_algorithm em28xx_algo = {
800 .master_xfer = em28xx_i2c_xfer,
a6c2ba28
AM
801 .functionality = functionality,
802};
803
3acf2809 804static struct i2c_adapter em28xx_adap_template = {
a6c2ba28 805 .owner = THIS_MODULE,
3acf2809 806 .name = "em28xx",
3acf2809 807 .algo = &em28xx_algo,
a6c2ba28
AM
808};
809
3acf2809
MCC
810static struct i2c_client em28xx_client_template = {
811 .name = "em28xx internal",
a6c2ba28
AM
812};
813
814/* ----------------------------------------------------------- */
815
816/*
817 * i2c_devs
818 * incomplete list of known devices
819 */
820static char *i2c_devs[128] = {
0b3966e4 821 [0x3e >> 1] = "remote IR sensor",
a6c2ba28 822 [0x4a >> 1] = "saa7113h",
729841ed 823 [0x52 >> 1] = "drxk",
a6c2ba28 824 [0x60 >> 1] = "remote IR sensor",
da45a2a5 825 [0x8e >> 1] = "remote IR sensor",
a6c2ba28
AM
826 [0x86 >> 1] = "tda9887",
827 [0x80 >> 1] = "msp34xx",
828 [0x88 >> 1] = "msp34xx",
829 [0xa0 >> 1] = "eeprom",
2bd1d9eb 830 [0xb0 >> 1] = "tda9874",
a6c2ba28 831 [0xb8 >> 1] = "tvp5150a",
791a08fc 832 [0xba >> 1] = "webcam sensor or tvp5150a",
a6c2ba28
AM
833 [0xc0 >> 1] = "tuner (analog)",
834 [0xc2 >> 1] = "tuner (analog)",
835 [0xc4 >> 1] = "tuner (analog)",
836 [0xc6 >> 1] = "tuner (analog)",
837};
838
839/*
840 * do_i2c_scan()
841 * check i2c address range for devices
842 */
aab3125c 843void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
a6c2ba28 844{
fad7b958 845 u8 i2c_devicelist[128];
a6c2ba28
AM
846 unsigned char buf;
847 int i, rc;
848
fad7b958
SS
849 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
850
53c4e955 851 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
aab3125c
MCC
852 dev->i2c_client[bus].addr = i;
853 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
a6c2ba28
AM
854 if (rc < 0)
855 continue;
fad7b958 856 i2c_devicelist[i] = i;
aab3125c
MCC
857 em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
858 i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
a6c2ba28 859 }
fad7b958 860
aab3125c
MCC
861 if (bus == dev->def_i2c_bus)
862 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
863 ARRAY_SIZE(i2c_devicelist), 32);
a6c2ba28
AM
864}
865
a6c2ba28 866/*
3acf2809 867 * em28xx_i2c_register()
a6c2ba28
AM
868 * register i2c bus
869 */
a3ea4bf9
FS
870int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
871 enum em28xx_i2c_algo_type algo_type)
a6c2ba28 872{
f2a01a00
DSL
873 int retval;
874
3acf2809
MCC
875 BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
876 BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
f2a01a00 877
aab3125c
MCC
878 if (bus >= NUM_I2C_BUSES)
879 return -ENODEV;
880
881 dev->i2c_adap[bus] = em28xx_adap_template;
882 dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
883 strcpy(dev->i2c_adap[bus].name, dev->name);
884
885 dev->i2c_bus[bus].bus = bus;
a3ea4bf9 886 dev->i2c_bus[bus].algo_type = algo_type;
aab3125c
MCC
887 dev->i2c_bus[bus].dev = dev;
888 dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
889 i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev);
890
891 retval = i2c_add_adapter(&dev->i2c_adap[bus]);
f2a01a00
DSL
892 if (retval < 0) {
893 em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
894 __func__, retval);
895 return retval;
896 }
a6c2ba28 897
aab3125c
MCC
898 dev->i2c_client[bus] = em28xx_client_template;
899 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
a6c2ba28 900
aab3125c
MCC
901 /* Up to now, all eeproms are at bus 0 */
902 if (!bus) {
903 retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
904 if ((retval < 0) && (retval != -ENODEV)) {
905 em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
906 __func__, retval);
c41109fc 907
aab3125c
MCC
908 return retval;
909 }
f2a01a00 910 }
a6c2ba28
AM
911
912 if (i2c_scan)
aab3125c 913 em28xx_do_i2c_scan(dev, bus);
c41109fc 914
a6c2ba28
AM
915 return 0;
916}
917
918/*
3acf2809 919 * em28xx_i2c_unregister()
a6c2ba28
AM
920 * unregister i2c_bus
921 */
aab3125c 922int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
a6c2ba28 923{
aab3125c
MCC
924 if (bus >= NUM_I2C_BUSES)
925 return -ENODEV;
926
927 i2c_del_adapter(&dev->i2c_adap[bus]);
a6c2ba28
AM
928 return 0;
929}