blob: 4851cc2e4a4d3227b59df97c8020298b41796c77 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefera3ea4bf2013-03-26 13:38:36 -03008 Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
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>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080029
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080030#include "em28xx.h"
Mauro Carvalho Chehab6c362c82007-10-29 23:36:12 -030031#include "tuner-xc2028.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020032#include <media/v4l2-common.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080033#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034
35/* ----------------------------------------------------------- */
36
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030037static unsigned int i2c_scan;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038module_param(i2c_scan, int, 0444);
39MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
40
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030041static unsigned int i2c_debug;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042module_param(i2c_debug, int, 0644);
43MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
44
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045/*
Frank Schaeferf5ae3712013-01-03 14:27:02 -030046 * em2800_i2c_send_bytes()
47 * send up to 4 bytes to the em2800 i2c device
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080048 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -030049static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080050{
51 int ret;
52 int write_timeout;
Frank Schaefera6bad042012-12-16 14:23:27 -030053 u8 b2[6];
Frank Schaeferf5ae3712013-01-03 14:27:02 -030054
55 if (len < 1 || len > 4)
56 return -EOPNOTSUPP;
57
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080058 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
Frank Schaefer2fcc82d2013-01-03 14:27:03 -030069 /* trigger write */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080070 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080071 if (ret != 2 + len) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -030072 em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
73 addr, ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -030074 return (ret < 0) ? ret : -EIO;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080075 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -030076 /* wait for completion */
77 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080078 write_timeout -= 5) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080079 ret = dev->em28xx_read_reg(dev, 0x05);
Frank Schaefer45f04e82013-01-03 14:27:05 -030080 if (ret == 0x80 + len - 1) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080081 return len;
Frank Schaefer45f04e82013-01-03 14:27:05 -030082 } else if (ret == 0x94 + len - 1) {
83 return -ENODEV;
84 } else if (ret < 0) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -030085 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
86 ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -030087 return ret;
88 }
Markus Rechbergere8e41da2006-02-07 06:49:11 -020089 msleep(5);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080090 }
Frank Schaefer45f04e82013-01-03 14:27:05 -030091 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080092 return -EIO;
93}
94
95/*
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080096 * em2800_i2c_recv_bytes()
Frank Schaefer2fcc82d2013-01-03 14:27:03 -030097 * read up to 4 bytes from the em2800 i2c device
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080098 */
Frank Schaefera6bad042012-12-16 14:23:27 -030099static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800100{
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300101 u8 buf2[4];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800102 int ret;
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300103 int read_timeout;
104 int i;
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300105
106 if (len < 1 || len > 4)
107 return -EOPNOTSUPP;
108
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300109 /* 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) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300114 em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
115 addr, ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300116 return (ret < 0) ? ret : -EIO;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800117 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300118
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) {
126 return -ENODEV;
127 } else if (ret < 0) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300128 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
129 ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300130 return ret;
131 }
132 msleep(5);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800133 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300134 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) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300140 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);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300142 return (ret < 0) ? ret : -EIO;
143 }
144 for (i = 0; i < len; i++)
145 buf[i] = buf2[len - 1 - i];
146
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800147 return ret;
148}
149
150/*
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300151 * em2800_i2c_check_for_device()
152 * check if there is an i2c device at the supplied address
153 */
154static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
155{
156 u8 buf;
157 int ret;
158
159 ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
160 if (ret == 1)
161 return 0;
162 return (ret < 0) ? ret : -EIO;
163}
164
165/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800166 * em28xx_i2c_send_bytes()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800167 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300168static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
169 u16 len, int stop)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800170{
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300171 int write_timeout, ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800172
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300173 if (len < 1 || len > 64)
174 return -EOPNOTSUPP;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300175 /*
176 * NOTE: limited by the USB ctrl message constraints
177 * Zero length reads always succeed, even if no device is connected
178 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300179
Frank Schaefer45f04e82013-01-03 14:27:05 -0300180 /* 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) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300184 em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
185 addr, ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300186 return ret;
187 } else {
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300188 em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
Frank Schaefer45f04e82013-01-03 14:27:05 -0300189 len, addr, ret);
190 return -EIO;
191 }
192 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800193
Frank Schaefer45f04e82013-01-03 14:27:05 -0300194 /* Check success of the i2c operation */
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300195 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300196 write_timeout -= 5) {
197 ret = dev->em28xx_read_reg(dev, 0x05);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300198 if (ret == 0) { /* success */
199 return len;
200 } else if (ret == 0x10) {
201 return -ENODEV;
202 } else if (ret < 0) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300203 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
204 ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300205 return ret;
206 }
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300207 msleep(5);
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300208 /*
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 */
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300213 }
Frank Schaefer45f04e82013-01-03 14:27:05 -0300214 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
215 return -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800216}
217
218/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800219 * em28xx_i2c_recv_bytes()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800220 * read a byte from the i2c device
221 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300222static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800223{
224 int ret;
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300225
226 if (len < 1 || len > 64)
227 return -EOPNOTSUPP;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300228 /*
229 * NOTE: limited by the USB ctrl message constraints
230 * Zero length reads always succeed, even if no device is connected
231 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300232
Frank Schaefer45f04e82013-01-03 14:27:05 -0300233 /* Read data from i2c device */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800234 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300235 if (ret < 0) {
236 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
237 addr, ret);
238 return ret;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300239 }
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300240 /*
241 * NOTE: some devices with two i2c busses have the bad habit to return 0
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300242 * 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 */
Frank Schaefer45f04e82013-01-03 14:27:05 -0300248
249 /* Check success of the i2c operation */
250 ret = dev->em28xx_read_reg(dev, 0x05);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800251 if (ret < 0) {
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300252 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
253 ret);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800254 return ret;
255 }
Frank Schaefer45f04e82013-01-03 14:27:05 -0300256 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;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800265}
266
267/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800268 * em28xx_i2c_check_for_device()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800269 * check if there is a i2c_device at the supplied address
270 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300271static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800272{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800273 int ret;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300274 u8 buf;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800275
Frank Schaefer45f04e82013-01-03 14:27:05 -0300276 ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
277 if (ret == 1)
278 return 0;
279 return (ret < 0) ? ret : -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800280}
281
282/*
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300283 * 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
459/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800460 * em28xx_i2c_xfer()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800461 * the main i2c transfer function
462 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800463static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800464 struct i2c_msg msgs[], int num)
465{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300466 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
467 struct em28xx *dev = i2c_bus->dev;
468 unsigned bus = i2c_bus->bus;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300469 int addr, rc, i;
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300470 u8 reg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800471
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300472 rc = rt_mutex_trylock(&dev->i2c_bus_lock);
473 if (rc < 0)
474 return rc;
475
476 /* Switch I2C bus if needed */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300477 if (bus != dev->cur_i2c_bus &&
478 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300479 if (bus == 1)
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300480 reg = EM2874_I2C_SECONDARY_BUS_SELECT;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300481 else
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300482 reg = 0;
483 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
484 EM2874_I2C_SECONDARY_BUS_SELECT);
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300485 dev->cur_i2c_bus = bus;
486 }
487
488 if (num <= 0) {
489 rt_mutex_unlock(&dev->i2c_bus_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800490 return 0;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300491 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800492 for (i = 0; i < num; i++) {
493 addr = msgs[i].addr << 1;
Frank Schaeferd90f0672013-03-03 15:37:36 -0300494 if (i2c_debug)
Frank Schaeferd7a80ea2013-03-03 15:37:35 -0300495 printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
496 dev->name, __func__ ,
497 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
498 i == num - 1 ? "stop" : "nonstop",
499 addr, msgs[i].len);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300500 if (!msgs[i].len) { /* no len: check only for device presence */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300501 rc = i2c_check_for_device(i2c_bus, addr);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300502 if (rc == -ENODEV) {
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300503 rt_mutex_unlock(&dev->i2c_bus_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800504 return rc;
505 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800506 } else if (msgs[i].flags & I2C_M_RD) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800507 /* read bytes */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300508 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800509 } else {
510 /* write bytes */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300511 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800512 }
Frank Schaefer45f04e82013-01-03 14:27:05 -0300513 if (rc < 0) {
Frank Schaeferd90f0672013-03-03 15:37:36 -0300514 if (i2c_debug)
Frank Schaefer45f04e82013-01-03 14:27:05 -0300515 printk(" ERROR: %i\n", rc);
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300516 rt_mutex_unlock(&dev->i2c_bus_lock);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300517 return rc;
518 }
Frank Schaeferd90f0672013-03-03 15:37:36 -0300519 if (i2c_debug)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800520 printk("\n");
521 }
522
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300523 rt_mutex_unlock(&dev->i2c_bus_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800524 return num;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800525}
526
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300527/*
528 * based on linux/sunrpc/svcauth.h and linux/hash.h
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300529 * The original hash function returns a different value, if arch is x86_64
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300530 * or i386.
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300531 */
532static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
533{
534 unsigned long hash = 0;
535 unsigned long l = 0;
536 int len = 0;
537 unsigned char c;
538 do {
539 if (len == length) {
540 c = (char)len;
541 len = -1;
542 } else
543 c = *buf++;
544 l = (l << 8) | c;
545 len++;
546 if ((len & (32 / 8 - 1)) == 0)
547 hash = ((hash^l) * 0x9e370001UL);
548 } while (len);
549
550 return (hash >> (32 - bits)) & 0xffffffffUL;
551}
552
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300553/*
554 * Helper function to read data blocks from i2c clients with 8 or 16 bit
555 * address width, 8 bit register width and auto incrementation been activated
556 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300557static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
558 bool addr_w16, u16 len, u8 *data)
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300559{
560 int remain = len, rsize, rsize_max, ret;
561 u8 buf[2];
562
563 /* Sanity check */
564 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
565 return -EINVAL;
566 /* Select address */
567 buf[0] = addr >> 8;
568 buf[1] = addr & 0xff;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300569 ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300570 if (ret < 0)
571 return ret;
572 /* Read data */
573 if (dev->board.is_em2800)
574 rsize_max = 4;
575 else
576 rsize_max = 64;
577 while (remain > 0) {
578 if (remain > rsize_max)
579 rsize = rsize_max;
580 else
581 rsize = remain;
582
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300583 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300584 if (ret < 0)
585 return ret;
586
587 remain -= rsize;
588 data += rsize;
589 }
590
591 return len;
592}
593
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300594static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
595 u8 **eedata, u16 *eedata_len)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800596{
Frank Schaefer510e8842013-03-03 15:37:43 -0300597 const u16 len = 256;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300598 /*
599 * FIXME common length/size for bytes to read, to display, hash
Frank Schaefer510e8842013-03-03 15:37:43 -0300600 * calculation and returned device dataset. Simplifies the code a lot,
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300601 * but we might have to deal with multiple sizes in the future !
602 */
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300603 int i, err;
Frank Schaefer510e8842013-03-03 15:37:43 -0300604 struct em28xx_eeprom *dev_config;
605 u8 buf, *data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800606
Frank Schaefera2179682013-03-03 15:37:42 -0300607 *eedata = NULL;
Frank Schaefer510e8842013-03-03 15:37:43 -0300608 *eedata_len = 0;
Frank Schaefera2179682013-03-03 15:37:42 -0300609
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300610 /* EEPROM is always on i2c bus 0 on all known devices. */
611
612 dev->i2c_client[bus].addr = 0xa0 >> 1;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800613
614 /* Check if board has eeprom */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300615 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300616 if (err < 0) {
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300617 em28xx_info("board has no eeprom\n");
Mauro Carvalho Chehabc41109fc92008-11-15 23:44:14 -0300618 return -ENODEV;
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300619 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800620
Frank Schaefera2179682013-03-03 15:37:42 -0300621 data = kzalloc(len, GFP_KERNEL);
622 if (data == NULL)
623 return -ENOMEM;
624
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300625 /* Read EEPROM content */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300626 err = em28xx_i2c_read_block(dev, bus, 0x0000,
627 dev->eeprom_addrwidth_16bit,
Frank Schaefera2179682013-03-03 15:37:42 -0300628 len, data);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300629 if (err != len) {
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300630 em28xx_errdev("failed to read eeprom (err=%d)\n", err);
Frank Schaefer510e8842013-03-03 15:37:43 -0300631 goto error;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800632 }
Frank Schaefer90271962013-01-03 14:27:06 -0300633
Frank Schaefer87b52432013-03-03 15:37:40 -0300634 /* Display eeprom content */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800635 for (i = 0; i < len; i++) {
Frank Schaefer87b52432013-03-03 15:37:40 -0300636 if (0 == (i % 16)) {
637 if (dev->eeprom_addrwidth_16bit)
638 em28xx_info("i2c eeprom %04x:", i);
639 else
640 em28xx_info("i2c eeprom %02x:", i);
641 }
Frank Schaefera2179682013-03-03 15:37:42 -0300642 printk(" %02x", data[i]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800643 if (15 == (i % 16))
644 printk("\n");
645 }
Frank Schaefer510e8842013-03-03 15:37:43 -0300646 if (dev->eeprom_addrwidth_16bit)
647 em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800648
Frank Schaefer87b52432013-03-03 15:37:40 -0300649 if (dev->eeprom_addrwidth_16bit &&
Frank Schaefera2179682013-03-03 15:37:42 -0300650 data[0] == 0x26 && data[3] == 0x00) {
Frank Schaefer87b52432013-03-03 15:37:40 -0300651 /* new eeprom format; size 4-64kb */
Frank Schaefer510e8842013-03-03 15:37:43 -0300652 u16 mc_start;
653 u16 hwconf_offset;
654
Frank Schaefera2179682013-03-03 15:37:42 -0300655 dev->hash = em28xx_hash_mem(data, len, 32);
Frank Schaefer510e8842013-03-03 15:37:43 -0300656 mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
657
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300658 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
Frank Schaefer510e8842013-03-03 15:37:43 -0300659 data[0], data[1], data[2], data[3], dev->hash);
660 em28xx_info("EEPROM info:\n");
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300661 em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
Frank Schaefer510e8842013-03-03 15:37:43 -0300662 mc_start, data[2]);
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300663 /*
664 * boot configuration (address 0x0002):
Frank Schaefer87b52432013-03-03 15:37:40 -0300665 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
666 * [1] always selects 12 kb RAM
667 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
668 * [4] 1 = force fast mode and no suspend for device testing
669 * [5:7] USB PHY tuning registers; determined by device
670 * characterization
671 */
672
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300673 /*
674 * Read hardware config dataset offset from address
675 * (microcode start + 46)
676 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300677 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
678 data);
Frank Schaefer510e8842013-03-03 15:37:43 -0300679 if (err != 2) {
680 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
681 err);
682 goto error;
683 }
Frank Schaefer87b52432013-03-03 15:37:40 -0300684
Frank Schaefer510e8842013-03-03 15:37:43 -0300685 /* Calculate hardware config dataset start address */
686 hwconf_offset = mc_start + data[0] + (data[1] << 8);
687
688 /* Read hardware config dataset */
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300689 /*
690 * NOTE: the microcode copy can be multiple pages long, but
Frank Schaefer510e8842013-03-03 15:37:43 -0300691 * we assume the hardware config dataset is the same as in
692 * the old eeprom and not longer than 256 bytes.
693 * tveeprom is currently also limited to 256 bytes.
694 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300695 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
696 data);
Frank Schaefer510e8842013-03-03 15:37:43 -0300697 if (err != len) {
698 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
699 err);
700 goto error;
701 }
702
703 /* Verify hardware config dataset */
704 /* NOTE: not all devices provide this type of dataset */
705 if (data[0] != 0x1a || data[1] != 0xeb ||
706 data[2] != 0x67 || data[3] != 0x95) {
707 em28xx_info("\tno hardware configuration dataset found in eeprom\n");
708 kfree(data);
709 return 0;
710 }
711
712 /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
713
714 } else if (!dev->eeprom_addrwidth_16bit &&
715 data[0] == 0x1a && data[1] == 0xeb &&
716 data[2] == 0x67 && data[3] == 0x95) {
717 dev->hash = em28xx_hash_mem(data, len, 32);
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300718 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
Frank Schaefer510e8842013-03-03 15:37:43 -0300719 data[0], data[1], data[2], data[3], dev->hash);
720 em28xx_info("EEPROM info:\n");
721 } else {
Frank Schaefer87b52432013-03-03 15:37:40 -0300722 em28xx_info("unknown eeprom format or eeprom corrupted !\n");
Frank Schaefer510e8842013-03-03 15:37:43 -0300723 err = -ENODEV;
724 goto error;
Frank Schaeferf55eacb2013-03-03 15:37:37 -0300725 }
726
Frank Schaefera2179682013-03-03 15:37:42 -0300727 *eedata = data;
Frank Schaefer510e8842013-03-03 15:37:43 -0300728 *eedata_len = len;
729 dev_config = (void *)eedata;
Frank Schaefera2179682013-03-03 15:37:42 -0300730
Frank Schaefer510e8842013-03-03 15:37:43 -0300731 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800732 case 0:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300733 em28xx_info("\tNo audio on board.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800734 break;
735 case 1:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300736 em28xx_info("\tAC97 audio (5 sample rates)\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800737 break;
738 case 2:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300739 em28xx_info("\tI2S audio, sample rate=32k\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800740 break;
741 case 3:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300742 em28xx_info("\tI2S audio, 3 sample rates\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800743 break;
744 }
745
Frank Schaefer510e8842013-03-03 15:37:43 -0300746 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300747 em28xx_info("\tUSB Remote wakeup capable\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800748
Frank Schaefer510e8842013-03-03 15:37:43 -0300749 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300750 em28xx_info("\tUSB Self power capable\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800751
Frank Schaefer510e8842013-03-03 15:37:43 -0300752 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800753 case 0:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300754 em28xx_info("\t500mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800755 break;
756 case 1:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300757 em28xx_info("\t400mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800758 break;
759 case 2:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300760 em28xx_info("\t300mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800761 break;
762 case 3:
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300763 em28xx_info("\t200mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800764 break;
765 }
Frank Schaefer12d7ce12013-03-03 15:37:34 -0300766 em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
Frank Schaefer510e8842013-03-03 15:37:43 -0300767 dev_config->string_idx_table,
768 le16_to_cpu(dev_config->string1),
769 le16_to_cpu(dev_config->string2),
770 le16_to_cpu(dev_config->string3));
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800771
772 return 0;
Frank Schaefer510e8842013-03-03 15:37:43 -0300773
774error:
775 kfree(data);
776 return err;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800777}
778
779/* ----------------------------------------------------------- */
780
781/*
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800782 * functionality()
783 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300784static u32 functionality(struct i2c_adapter *i2c_adap)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800785{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300786 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300787
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300788 if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
789 (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
790 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
791 } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
792 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
793 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
794 }
795
796 WARN(1, "Unknown i2c bus algorithm.\n");
797 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800798}
799
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800800static struct i2c_algorithm em28xx_algo = {
801 .master_xfer = em28xx_i2c_xfer,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800802 .functionality = functionality,
803};
804
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800805static struct i2c_adapter em28xx_adap_template = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800806 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800807 .name = "em28xx",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800808 .algo = &em28xx_algo,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800809};
810
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800811static struct i2c_client em28xx_client_template = {
812 .name = "em28xx internal",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800813};
814
815/* ----------------------------------------------------------- */
816
817/*
818 * i2c_devs
819 * incomplete list of known devices
820 */
821static char *i2c_devs[128] = {
Frank Schaefer0b3966e2013-01-13 10:15:08 -0300822 [0x3e >> 1] = "remote IR sensor",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800823 [0x4a >> 1] = "saa7113h",
Martin Blumenstingl729841e2012-06-12 18:19:27 -0300824 [0x52 >> 1] = "drxk",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800825 [0x60 >> 1] = "remote IR sensor",
Markus Rechbergerda45a2a2005-11-08 21:37:31 -0800826 [0x8e >> 1] = "remote IR sensor",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800827 [0x86 >> 1] = "tda9887",
828 [0x80 >> 1] = "msp34xx",
829 [0x88 >> 1] = "msp34xx",
830 [0xa0 >> 1] = "eeprom",
Vitaly Wool2bd1d9eb2009-03-04 08:27:52 -0300831 [0xb0 >> 1] = "tda9874",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800832 [0xb8 >> 1] = "tvp5150a",
Mauro Carvalho Chehab791a08f2009-07-03 15:36:18 -0300833 [0xba >> 1] = "webcam sensor or tvp5150a",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800834 [0xc0 >> 1] = "tuner (analog)",
835 [0xc2 >> 1] = "tuner (analog)",
836 [0xc4 >> 1] = "tuner (analog)",
837 [0xc6 >> 1] = "tuner (analog)",
838};
839
840/*
841 * do_i2c_scan()
842 * check i2c address range for devices
843 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300844void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800845{
Sascha Sommerfad7b952007-11-04 08:06:48 -0300846 u8 i2c_devicelist[128];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800847 unsigned char buf;
848 int i, rc;
849
Sascha Sommerfad7b952007-11-04 08:06:48 -0300850 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
851
Mauro Carvalho Chehab53c4e952007-03-29 08:42:30 -0300852 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300853 dev->i2c_client[bus].addr = i;
854 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800855 if (rc < 0)
856 continue;
Sascha Sommerfad7b952007-11-04 08:06:48 -0300857 i2c_devicelist[i] = i;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300858 em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
859 i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800860 }
Sascha Sommerfad7b952007-11-04 08:06:48 -0300861
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300862 if (bus == dev->def_i2c_bus)
863 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
864 ARRAY_SIZE(i2c_devicelist), 32);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800865}
866
867/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800868 * em28xx_i2c_register()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800869 * register i2c bus
870 */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300871int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
872 enum em28xx_i2c_algo_type algo_type)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800873{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300874 int retval;
875
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800876 BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
877 BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300878
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300879 if (bus >= NUM_I2C_BUSES)
880 return -ENODEV;
881
882 dev->i2c_adap[bus] = em28xx_adap_template;
883 dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
884 strcpy(dev->i2c_adap[bus].name, dev->name);
885
886 dev->i2c_bus[bus].bus = bus;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300887 dev->i2c_bus[bus].algo_type = algo_type;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300888 dev->i2c_bus[bus].dev = dev;
889 dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
890 i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev);
891
892 retval = i2c_add_adapter(&dev->i2c_adap[bus]);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300893 if (retval < 0) {
894 em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
895 __func__, retval);
896 return retval;
897 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800898
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300899 dev->i2c_client[bus] = em28xx_client_template;
900 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800901
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300902 /* Up to now, all eeproms are at bus 0 */
903 if (!bus) {
904 retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
905 if ((retval < 0) && (retval != -ENODEV)) {
906 em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
907 __func__, retval);
Mauro Carvalho Chehabc41109fc92008-11-15 23:44:14 -0300908
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300909 return retval;
910 }
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300911 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800912
913 if (i2c_scan)
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300914 em28xx_do_i2c_scan(dev, bus);
Mauro Carvalho Chehabc41109fc92008-11-15 23:44:14 -0300915
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800916 return 0;
917}
918
919/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800920 * em28xx_i2c_unregister()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800921 * unregister i2c_bus
922 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300923int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800924{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300925 if (bus >= NUM_I2C_BUSES)
926 return -ENODEV;
927
928 i2c_del_adapter(&dev->i2c_adap[bus]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800929 return 0;
930}