Commit | Line | Data |
---|---|---|
f2242ee5 | 1 | /* |
1ff16c20 | 2 | * usbvision_i2c.c |
781aa1d1 MCC |
3 | * i2c algorithm for USB-I2C Bridges |
4 | * | |
1ff16c20 | 5 | * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de> |
781aa1d1 MCC |
6 | * Dwaine Garden <dwainegarden@rogers.com> |
7 | * | |
8 | * This module is part of usbvision driver project. | |
9 | * Updates to driver completed by Dwaine P. Garden | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | */ | |
25 | ||
26 | ||
27 | #include <linux/kernel.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/delay.h> | |
30 | #include <linux/slab.h> | |
781aa1d1 MCC |
31 | #include <linux/init.h> |
32 | #include <asm/uaccess.h> | |
33 | #include <linux/ioport.h> | |
34 | #include <linux/errno.h> | |
781aa1d1 MCC |
35 | #include <linux/usb.h> |
36 | #include <linux/i2c.h> | |
483dfdb6 | 37 | #include "usbvision.h" |
781aa1d1 | 38 | |
483dfdb6 | 39 | #define DBG_I2C 1<<0 |
781aa1d1 | 40 | |
ff699e6b | 41 | static int i2c_debug; |
483dfdb6 TM |
42 | |
43 | module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver | |
44 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); | |
781aa1d1 | 45 | |
df18c319 TM |
46 | #define PDEBUG(level, fmt, args...) { \ |
47 | if (i2c_debug & (level)) \ | |
a482f327 GKH |
48 | printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \ |
49 | __func__, __LINE__ , ## args); \ | |
df18c319 | 50 | } |
781aa1d1 | 51 | |
1ff16c20 | 52 | static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
2a7e9a26 | 53 | short len); |
1ff16c20 | 54 | static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
2a7e9a26 MCC |
55 | short len); |
56 | ||
781aa1d1 MCC |
57 | static inline int try_write_address(struct i2c_adapter *i2c_adap, |
58 | unsigned char addr, int retries) | |
59 | { | |
1ff16c20 | 60 | struct usb_usbvision *usbvision; |
781aa1d1 MCC |
61 | int i, ret = -1; |
62 | char buf[4]; | |
63 | ||
bf8b20e1 | 64 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
781aa1d1 MCC |
65 | buf[0] = 0x00; |
66 | for (i = 0; i <= retries; i++) { | |
1ff16c20 | 67 | ret = (usbvision_i2c_write(usbvision, addr, buf, 1)); |
781aa1d1 MCC |
68 | if (ret == 1) |
69 | break; /* success! */ | |
c6268461 | 70 | udelay(5); |
781aa1d1 MCC |
71 | if (i == retries) /* no success */ |
72 | break; | |
c6268461 | 73 | udelay(10); |
781aa1d1 | 74 | } |
483dfdb6 | 75 | if (i) { |
1ff16c20 TM |
76 | PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr); |
77 | PDEBUG(DBG_I2C,"Maybe there's no device at this address"); | |
781aa1d1 MCC |
78 | } |
79 | return ret; | |
80 | } | |
81 | ||
82 | static inline int try_read_address(struct i2c_adapter *i2c_adap, | |
83 | unsigned char addr, int retries) | |
84 | { | |
1ff16c20 | 85 | struct usb_usbvision *usbvision; |
781aa1d1 MCC |
86 | int i, ret = -1; |
87 | char buf[4]; | |
88 | ||
bf8b20e1 | 89 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
781aa1d1 | 90 | for (i = 0; i <= retries; i++) { |
1ff16c20 | 91 | ret = (usbvision_i2c_read(usbvision, addr, buf, 1)); |
781aa1d1 MCC |
92 | if (ret == 1) |
93 | break; /* success! */ | |
c6268461 | 94 | udelay(5); |
781aa1d1 MCC |
95 | if (i == retries) /* no success */ |
96 | break; | |
c6268461 | 97 | udelay(10); |
781aa1d1 | 98 | } |
483dfdb6 | 99 | if (i) { |
1ff16c20 TM |
100 | PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr); |
101 | PDEBUG(DBG_I2C,"Maybe there's no device at this address"); | |
781aa1d1 MCC |
102 | } |
103 | return ret; | |
104 | } | |
105 | ||
106 | static inline int usb_find_address(struct i2c_adapter *i2c_adap, | |
107 | struct i2c_msg *msg, int retries, | |
108 | unsigned char *add) | |
109 | { | |
110 | unsigned short flags = msg->flags; | |
f2242ee5 | 111 | |
781aa1d1 MCC |
112 | unsigned char addr; |
113 | int ret; | |
114 | if ((flags & I2C_M_TEN)) { | |
115 | /* a ten bit address */ | |
116 | addr = 0xf0 | ((msg->addr >> 7) & 0x03); | |
117 | /* try extended address code... */ | |
118 | ret = try_write_address(i2c_adap, addr, retries); | |
119 | if (ret != 1) { | |
be9ed511 MCC |
120 | dev_err(&i2c_adap->dev, |
121 | "died at extended address code, while writing\n"); | |
781aa1d1 MCC |
122 | return -EREMOTEIO; |
123 | } | |
124 | add[0] = addr; | |
125 | if (flags & I2C_M_RD) { | |
126 | /* okay, now switch into reading mode */ | |
127 | addr |= 0x01; | |
128 | ret = try_read_address(i2c_adap, addr, retries); | |
129 | if (ret != 1) { | |
be9ed511 MCC |
130 | dev_err(&i2c_adap->dev, |
131 | "died at extended address code, while reading\n"); | |
781aa1d1 MCC |
132 | return -EREMOTEIO; |
133 | } | |
134 | } | |
135 | ||
136 | } else { /* normal 7bit address */ | |
137 | addr = (msg->addr << 1); | |
138 | if (flags & I2C_M_RD) | |
139 | addr |= 1; | |
781aa1d1 MCC |
140 | |
141 | add[0] = addr; | |
142 | if (flags & I2C_M_RD) | |
143 | ret = try_read_address(i2c_adap, addr, retries); | |
144 | else | |
145 | ret = try_write_address(i2c_adap, addr, retries); | |
146 | ||
147 | if (ret != 1) { | |
148 | return -EREMOTEIO; | |
149 | } | |
150 | } | |
151 | return 0; | |
152 | } | |
153 | ||
154 | static int | |
1ff16c20 | 155 | usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) |
781aa1d1 MCC |
156 | { |
157 | struct i2c_msg *pmsg; | |
1ff16c20 | 158 | struct usb_usbvision *usbvision; |
781aa1d1 | 159 | int i, ret; |
cf8e193a | 160 | unsigned char addr = 0; |
781aa1d1 | 161 | |
bf8b20e1 TM |
162 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
163 | ||
781aa1d1 MCC |
164 | for (i = 0; i < num; i++) { |
165 | pmsg = &msgs[i]; | |
166 | ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr); | |
167 | if (ret != 0) { | |
1ff16c20 | 168 | PDEBUG(DBG_I2C,"got NAK from device, message #%d", i); |
781aa1d1 MCC |
169 | return (ret < 0) ? ret : -EREMOTEIO; |
170 | } | |
171 | ||
172 | if (pmsg->flags & I2C_M_RD) { | |
173 | /* read bytes into buffer */ | |
1ff16c20 | 174 | ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len)); |
781aa1d1 MCC |
175 | if (ret < pmsg->len) { |
176 | return (ret < 0) ? ret : -EREMOTEIO; | |
177 | } | |
178 | } else { | |
179 | /* write bytes from buffer */ | |
1ff16c20 | 180 | ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len)); |
781aa1d1 MCC |
181 | if (ret < pmsg->len) { |
182 | return (ret < 0) ? ret : -EREMOTEIO; | |
183 | } | |
184 | } | |
185 | } | |
186 | return num; | |
187 | } | |
188 | ||
1ff16c20 | 189 | static u32 functionality(struct i2c_adapter *adap) |
781aa1d1 | 190 | { |
39f46ade | 191 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR; |
781aa1d1 MCC |
192 | } |
193 | ||
781aa1d1 MCC |
194 | /* -----exported algorithm data: ------------------------------------- */ |
195 | ||
1ff16c20 TM |
196 | static struct i2c_algorithm usbvision_algo = { |
197 | .master_xfer = usbvision_i2c_xfer, | |
781aa1d1 | 198 | .smbus_xfer = NULL, |
1ff16c20 | 199 | .functionality = functionality, |
781aa1d1 MCC |
200 | }; |
201 | ||
202 | ||
483dfdb6 TM |
203 | /* ----------------------------------------------------------------------- */ |
204 | /* usbvision specific I2C functions */ | |
205 | /* ----------------------------------------------------------------------- */ | |
206 | static struct i2c_adapter i2c_adap_template; | |
483dfdb6 | 207 | |
1ff16c20 | 208 | int usbvision_i2c_register(struct usb_usbvision *usbvision) |
483dfdb6 | 209 | { |
1df79537 HV |
210 | static unsigned short saa711x_addrs[] = { |
211 | 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */ | |
212 | 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */ | |
213 | I2C_CLIENT_END }; | |
214 | ||
bf8b20e1 TM |
215 | memcpy(&usbvision->i2c_adap, &i2c_adap_template, |
216 | sizeof(struct i2c_adapter)); | |
483dfdb6 | 217 | |
1ff16c20 | 218 | sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name), |
c6330fb8 | 219 | " #%d", usbvision->vdev->num); |
bf8b20e1 TM |
220 | PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name); |
221 | usbvision->i2c_adap.dev.parent = &usbvision->dev->dev; | |
222 | ||
1df79537 | 223 | i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev); |
483dfdb6 TM |
224 | |
225 | if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) { | |
bf8b20e1 | 226 | printk(KERN_ERR "usbvision_register: can't write reg\n"); |
483dfdb6 TM |
227 | return -EBUSY; |
228 | } | |
229 | ||
1df79537 HV |
230 | PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]"); |
231 | PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]"); | |
232 | ||
233 | /* register new adapter to i2c module... */ | |
234 | ||
235 | usbvision->i2c_adap.algo = &usbvision_algo; | |
236 | ||
237 | usbvision->i2c_adap.timeout = 100; /* default values, should */ | |
238 | usbvision->i2c_adap.retries = 3; /* be replaced by defines */ | |
239 | ||
240 | i2c_add_adapter(&usbvision->i2c_adap); | |
241 | ||
242 | PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name); | |
243 | ||
483dfdb6 TM |
244 | /* Request the load of the i2c modules we need */ |
245 | switch (usbvision_device_data[usbvision->DevModel].Codec) { | |
246 | case CODEC_SAA7113: | |
483dfdb6 | 247 | case CODEC_SAA7111: |
53dacb15 | 248 | v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
e6574f2f | 249 | &usbvision->i2c_adap, "saa7115", |
53dacb15 | 250 | "saa7115_auto", 0, saa711x_addrs); |
483dfdb6 TM |
251 | break; |
252 | } | |
253 | if (usbvision_device_data[usbvision->DevModel].Tuner == 1) { | |
1df79537 HV |
254 | struct v4l2_subdev *sd; |
255 | enum v4l2_i2c_tuner_type type; | |
256 | struct tuner_setup tun_setup; | |
257 | ||
53dacb15 | 258 | sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
e6574f2f | 259 | &usbvision->i2c_adap, "tuner", |
53dacb15 | 260 | "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD)); |
1df79537 HV |
261 | /* depending on whether we found a demod or not, select |
262 | the tuner type. */ | |
263 | type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV; | |
264 | ||
53dacb15 | 265 | sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
e6574f2f | 266 | &usbvision->i2c_adap, "tuner", |
53dacb15 | 267 | "tuner", 0, v4l2_i2c_tuner_addrs(type)); |
1df79537 HV |
268 | |
269 | if (usbvision->tuner_type != -1) { | |
270 | tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; | |
271 | tun_setup.type = usbvision->tuner_type; | |
272 | tun_setup.addr = v4l2_i2c_subdev_addr(sd); | |
273 | call_all(usbvision, tuner, s_type_addr, &tun_setup); | |
274 | } | |
781aa1d1 | 275 | } |
781aa1d1 | 276 | |
1df79537 | 277 | return 0; |
1ff16c20 TM |
278 | } |
279 | ||
280 | int usbvision_i2c_unregister(struct usb_usbvision *usbvision) | |
281 | { | |
282 | ||
283 | i2c_del_adapter(&(usbvision->i2c_adap)); | |
284 | ||
285 | PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name); | |
286 | ||
287 | return 0; | |
483dfdb6 TM |
288 | } |
289 | ||
483dfdb6 TM |
290 | static int |
291 | usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr, | |
292 | char *buf, short len) | |
293 | { | |
294 | int rc, retries; | |
295 | ||
296 | for (retries = 5;;) { | |
297 | rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr); | |
298 | if (rc < 0) | |
299 | return rc; | |
300 | ||
301 | /* Initiate byte read cycle */ | |
302 | /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */ | |
303 | /* d3 0=Wr 1=Rd */ | |
304 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, | |
305 | (len & 0x07) | 0x18); | |
306 | if (rc < 0) | |
307 | return rc; | |
308 | ||
309 | /* Test for Busy and ACK */ | |
310 | do { | |
311 | /* USBVISION_SER_CONT -> d4 == 0 busy */ | |
312 | rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); | |
313 | } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ | |
314 | if (rc < 0) | |
315 | return rc; | |
316 | ||
317 | /* USBVISION_SER_CONT -> d5 == 1 Not ack */ | |
318 | if ((rc & 0x20) == 0) /* Ack? */ | |
319 | break; | |
320 | ||
321 | /* I2C abort */ | |
322 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); | |
323 | if (rc < 0) | |
324 | return rc; | |
325 | ||
326 | if (--retries < 0) | |
327 | return -1; | |
328 | } | |
329 | ||
330 | switch (len) { | |
331 | case 4: | |
332 | buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4); | |
333 | case 3: | |
334 | buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3); | |
335 | case 2: | |
336 | buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2); | |
337 | case 1: | |
338 | buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1); | |
339 | break; | |
340 | default: | |
341 | printk(KERN_ERR | |
342 | "usbvision_i2c_read_max4: buffer length > 4\n"); | |
343 | } | |
344 | ||
345 | if (i2c_debug & DBG_I2C) { | |
346 | int idx; | |
347 | for (idx = 0; idx < len; idx++) { | |
348 | PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr); | |
349 | } | |
350 | } | |
351 | return len; | |
352 | } | |
353 | ||
354 | ||
355 | static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision, | |
356 | unsigned char addr, const char *buf, | |
357 | short len) | |
358 | { | |
359 | int rc, retries; | |
360 | int i; | |
361 | unsigned char value[6]; | |
362 | unsigned char ser_cont; | |
363 | ||
364 | ser_cont = (len & 0x07) | 0x10; | |
365 | ||
366 | value[0] = addr; | |
367 | value[1] = ser_cont; | |
368 | for (i = 0; i < len; i++) | |
369 | value[i + 2] = buf[i]; | |
370 | ||
371 | for (retries = 5;;) { | |
372 | rc = usb_control_msg(usbvision->dev, | |
373 | usb_sndctrlpipe(usbvision->dev, 1), | |
374 | USBVISION_OP_CODE, | |
375 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
376 | USB_RECIP_ENDPOINT, 0, | |
377 | (__u16) USBVISION_SER_ADRS, value, | |
378 | len + 2, HZ); | |
379 | ||
380 | if (rc < 0) | |
381 | return rc; | |
382 | ||
383 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, | |
384 | (len & 0x07) | 0x10); | |
385 | if (rc < 0) | |
386 | return rc; | |
387 | ||
388 | /* Test for Busy and ACK */ | |
389 | do { | |
390 | rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); | |
391 | } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ | |
392 | if (rc < 0) | |
393 | return rc; | |
394 | ||
395 | if ((rc & 0x20) == 0) /* Ack? */ | |
396 | break; | |
397 | ||
398 | /* I2C abort */ | |
399 | usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); | |
400 | ||
401 | if (--retries < 0) | |
402 | return -1; | |
403 | ||
404 | } | |
405 | ||
406 | if (i2c_debug & DBG_I2C) { | |
407 | int idx; | |
408 | for (idx = 0; idx < len; idx++) { | |
409 | PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr); | |
410 | } | |
411 | } | |
412 | return len; | |
413 | } | |
414 | ||
1ff16c20 | 415 | static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
483dfdb6 TM |
416 | short len) |
417 | { | |
418 | char *bufPtr = buf; | |
419 | int retval; | |
420 | int wrcount = 0; | |
421 | int count; | |
422 | int maxLen = 4; | |
483dfdb6 TM |
423 | |
424 | while (len > 0) { | |
425 | count = (len > maxLen) ? maxLen : len; | |
426 | retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count); | |
427 | if (retval > 0) { | |
428 | len -= count; | |
429 | bufPtr += count; | |
430 | wrcount += count; | |
431 | } else | |
432 | return (retval < 0) ? retval : -EFAULT; | |
433 | } | |
434 | return wrcount; | |
435 | } | |
436 | ||
1ff16c20 | 437 | static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
483dfdb6 TM |
438 | short len) |
439 | { | |
440 | char temp[4]; | |
441 | int retval, i; | |
442 | int rdcount = 0; | |
443 | int count; | |
483dfdb6 TM |
444 | |
445 | while (len > 0) { | |
446 | count = (len > 3) ? 4 : len; | |
447 | retval = usbvision_i2c_read_max4(usbvision, addr, temp, count); | |
448 | if (retval > 0) { | |
449 | for (i = 0; i < len; i++) | |
450 | buf[rdcount + i] = temp[i]; | |
451 | len -= count; | |
452 | rdcount += count; | |
453 | } else | |
454 | return (retval < 0) ? retval : -EFAULT; | |
455 | } | |
456 | return rdcount; | |
457 | } | |
458 | ||
483dfdb6 TM |
459 | static struct i2c_adapter i2c_adap_template = { |
460 | .owner = THIS_MODULE, | |
461 | .name = "usbvision", | |
483dfdb6 TM |
462 | }; |
463 | ||
483dfdb6 TM |
464 | /* |
465 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
466 | * --------------------------------------------------------------------------- | |
467 | * Local variables: | |
468 | * c-basic-offset: 8 | |
469 | * End: | |
470 | */ |