usbvision-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * usbvision_i2c.c
  3. * i2c algorithm for USB-I2C Bridges
  4. *
  5. * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
  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. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/version.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/ioport.h>
  34. #include <linux/errno.h>
  35. #include <linux/usb.h>
  36. #include <linux/i2c.h>
  37. #include "usbvision.h"
  38. #define DBG_I2C 1<<0
  39. static int i2c_debug;
  40. module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
  41. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  42. #define PDEBUG(level, fmt, args...) { \
  43. if (i2c_debug & (level)) \
  44. info("[%s:%d] " fmt, __func__, __LINE__ , ## args); \
  45. }
  46. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  47. short len);
  48. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  49. short len);
  50. static inline int try_write_address(struct i2c_adapter *i2c_adap,
  51. unsigned char addr, int retries)
  52. {
  53. struct usb_usbvision *usbvision;
  54. int i, ret = -1;
  55. char buf[4];
  56. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  57. buf[0] = 0x00;
  58. for (i = 0; i <= retries; i++) {
  59. ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
  60. if (ret == 1)
  61. break; /* success! */
  62. udelay(5);
  63. if (i == retries) /* no success */
  64. break;
  65. udelay(10);
  66. }
  67. if (i) {
  68. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  69. PDEBUG(DBG_I2C,"Maybe there's no device at this address");
  70. }
  71. return ret;
  72. }
  73. static inline int try_read_address(struct i2c_adapter *i2c_adap,
  74. unsigned char addr, int retries)
  75. {
  76. struct usb_usbvision *usbvision;
  77. int i, ret = -1;
  78. char buf[4];
  79. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  80. for (i = 0; i <= retries; i++) {
  81. ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
  82. if (ret == 1)
  83. break; /* success! */
  84. udelay(5);
  85. if (i == retries) /* no success */
  86. break;
  87. udelay(10);
  88. }
  89. if (i) {
  90. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  91. PDEBUG(DBG_I2C,"Maybe there's no device at this address");
  92. }
  93. return ret;
  94. }
  95. static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  96. struct i2c_msg *msg, int retries,
  97. unsigned char *add)
  98. {
  99. unsigned short flags = msg->flags;
  100. unsigned char addr;
  101. int ret;
  102. if ((flags & I2C_M_TEN)) {
  103. /* a ten bit address */
  104. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  105. /* try extended address code... */
  106. ret = try_write_address(i2c_adap, addr, retries);
  107. if (ret != 1) {
  108. err("died at extended address code, while writing");
  109. return -EREMOTEIO;
  110. }
  111. add[0] = addr;
  112. if (flags & I2C_M_RD) {
  113. /* okay, now switch into reading mode */
  114. addr |= 0x01;
  115. ret = try_read_address(i2c_adap, addr, retries);
  116. if (ret != 1) {
  117. err("died at extended address code, while reading");
  118. return -EREMOTEIO;
  119. }
  120. }
  121. } else { /* normal 7bit address */
  122. addr = (msg->addr << 1);
  123. if (flags & I2C_M_RD)
  124. addr |= 1;
  125. add[0] = addr;
  126. if (flags & I2C_M_RD)
  127. ret = try_read_address(i2c_adap, addr, retries);
  128. else
  129. ret = try_write_address(i2c_adap, addr, retries);
  130. if (ret != 1) {
  131. return -EREMOTEIO;
  132. }
  133. }
  134. return 0;
  135. }
  136. static int
  137. usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  138. {
  139. struct i2c_msg *pmsg;
  140. struct usb_usbvision *usbvision;
  141. int i, ret;
  142. unsigned char addr;
  143. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  144. for (i = 0; i < num; i++) {
  145. pmsg = &msgs[i];
  146. ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
  147. if (ret != 0) {
  148. PDEBUG(DBG_I2C,"got NAK from device, message #%d", i);
  149. return (ret < 0) ? ret : -EREMOTEIO;
  150. }
  151. if (pmsg->flags & I2C_M_RD) {
  152. /* read bytes into buffer */
  153. ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
  154. if (ret < pmsg->len) {
  155. return (ret < 0) ? ret : -EREMOTEIO;
  156. }
  157. } else {
  158. /* write bytes from buffer */
  159. ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
  160. if (ret < pmsg->len) {
  161. return (ret < 0) ? ret : -EREMOTEIO;
  162. }
  163. }
  164. }
  165. return num;
  166. }
  167. static u32 functionality(struct i2c_adapter *adap)
  168. {
  169. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  170. }
  171. /* -----exported algorithm data: ------------------------------------- */
  172. static struct i2c_algorithm usbvision_algo = {
  173. .master_xfer = usbvision_i2c_xfer,
  174. .smbus_xfer = NULL,
  175. .functionality = functionality,
  176. };
  177. /*
  178. * registering functions to load algorithms at runtime
  179. */
  180. static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
  181. {
  182. PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
  183. PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
  184. /* register new adapter to i2c module... */
  185. adap->algo = &usbvision_algo;
  186. adap->timeout = 100; /* default values, should */
  187. adap->retries = 3; /* be replaced by defines */
  188. i2c_add_adapter(adap);
  189. PDEBUG(DBG_I2C,"i2c bus for %s registered", adap->name);
  190. return 0;
  191. }
  192. /* ----------------------------------------------------------------------- */
  193. /* usbvision specific I2C functions */
  194. /* ----------------------------------------------------------------------- */
  195. static struct i2c_adapter i2c_adap_template;
  196. static struct i2c_client i2c_client_template;
  197. int usbvision_i2c_register(struct usb_usbvision *usbvision)
  198. {
  199. memcpy(&usbvision->i2c_adap, &i2c_adap_template,
  200. sizeof(struct i2c_adapter));
  201. memcpy(&usbvision->i2c_client, &i2c_client_template,
  202. sizeof(struct i2c_client));
  203. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  204. " #%d", usbvision->vdev->minor & 0x1f);
  205. PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
  206. usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
  207. i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
  208. i2c_set_clientdata(&usbvision->i2c_client, usbvision);
  209. usbvision->i2c_client.adapter = &usbvision->i2c_adap;
  210. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  211. printk(KERN_ERR "usbvision_register: can't write reg\n");
  212. return -EBUSY;
  213. }
  214. #ifdef CONFIG_MODULES
  215. /* Request the load of the i2c modules we need */
  216. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  217. case CODEC_SAA7113:
  218. request_module("saa7115");
  219. break;
  220. case CODEC_SAA7111:
  221. request_module("saa7115");
  222. break;
  223. }
  224. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  225. request_module("tuner");
  226. }
  227. #endif
  228. return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
  229. }
  230. int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
  231. {
  232. i2c_del_adapter(&(usbvision->i2c_adap));
  233. PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
  234. return 0;
  235. }
  236. void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
  237. void *arg)
  238. {
  239. i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
  240. }
  241. static int attach_inform(struct i2c_client *client)
  242. {
  243. struct usb_usbvision *usbvision;
  244. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  245. switch (client->addr << 1) {
  246. case 0x42 << 1:
  247. case 0x43 << 1:
  248. case 0x4a << 1:
  249. case 0x4b << 1:
  250. PDEBUG(DBG_I2C,"attach_inform: tda9887 detected.");
  251. break;
  252. case 0x42:
  253. PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
  254. break;
  255. case 0x4a:
  256. PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
  257. break;
  258. case 0x48:
  259. PDEBUG(DBG_I2C,"attach_inform: saa7111 detected.");
  260. break;
  261. case 0xa0:
  262. PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
  263. break;
  264. default:
  265. {
  266. struct tuner_setup tun_setup;
  267. PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
  268. usbvision->tuner_addr = client->addr;
  269. if ((usbvision->have_tuner) && (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 = usbvision->tuner_addr;
  273. call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
  274. }
  275. }
  276. break;
  277. }
  278. return 0;
  279. }
  280. static int detach_inform(struct i2c_client *client)
  281. {
  282. struct usb_usbvision *usbvision;
  283. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  284. PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
  285. return 0;
  286. }
  287. static int
  288. usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
  289. char *buf, short len)
  290. {
  291. int rc, retries;
  292. for (retries = 5;;) {
  293. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  294. if (rc < 0)
  295. return rc;
  296. /* Initiate byte read cycle */
  297. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  298. /* d3 0=Wr 1=Rd */
  299. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  300. (len & 0x07) | 0x18);
  301. if (rc < 0)
  302. return rc;
  303. /* Test for Busy and ACK */
  304. do {
  305. /* USBVISION_SER_CONT -> d4 == 0 busy */
  306. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  307. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  308. if (rc < 0)
  309. return rc;
  310. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  311. if ((rc & 0x20) == 0) /* Ack? */
  312. break;
  313. /* I2C abort */
  314. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  315. if (rc < 0)
  316. return rc;
  317. if (--retries < 0)
  318. return -1;
  319. }
  320. switch (len) {
  321. case 4:
  322. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  323. case 3:
  324. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  325. case 2:
  326. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  327. case 1:
  328. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  329. break;
  330. default:
  331. printk(KERN_ERR
  332. "usbvision_i2c_read_max4: buffer length > 4\n");
  333. }
  334. if (i2c_debug & DBG_I2C) {
  335. int idx;
  336. for (idx = 0; idx < len; idx++) {
  337. PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
  338. }
  339. }
  340. return len;
  341. }
  342. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  343. unsigned char addr, const char *buf,
  344. short len)
  345. {
  346. int rc, retries;
  347. int i;
  348. unsigned char value[6];
  349. unsigned char ser_cont;
  350. ser_cont = (len & 0x07) | 0x10;
  351. value[0] = addr;
  352. value[1] = ser_cont;
  353. for (i = 0; i < len; i++)
  354. value[i + 2] = buf[i];
  355. for (retries = 5;;) {
  356. rc = usb_control_msg(usbvision->dev,
  357. usb_sndctrlpipe(usbvision->dev, 1),
  358. USBVISION_OP_CODE,
  359. USB_DIR_OUT | USB_TYPE_VENDOR |
  360. USB_RECIP_ENDPOINT, 0,
  361. (__u16) USBVISION_SER_ADRS, value,
  362. len + 2, HZ);
  363. if (rc < 0)
  364. return rc;
  365. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  366. (len & 0x07) | 0x10);
  367. if (rc < 0)
  368. return rc;
  369. /* Test for Busy and ACK */
  370. do {
  371. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  372. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  373. if (rc < 0)
  374. return rc;
  375. if ((rc & 0x20) == 0) /* Ack? */
  376. break;
  377. /* I2C abort */
  378. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  379. if (--retries < 0)
  380. return -1;
  381. }
  382. if (i2c_debug & DBG_I2C) {
  383. int idx;
  384. for (idx = 0; idx < len; idx++) {
  385. PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
  386. }
  387. }
  388. return len;
  389. }
  390. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  391. short len)
  392. {
  393. char *bufPtr = buf;
  394. int retval;
  395. int wrcount = 0;
  396. int count;
  397. int maxLen = 4;
  398. while (len > 0) {
  399. count = (len > maxLen) ? maxLen : len;
  400. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  401. if (retval > 0) {
  402. len -= count;
  403. bufPtr += count;
  404. wrcount += count;
  405. } else
  406. return (retval < 0) ? retval : -EFAULT;
  407. }
  408. return wrcount;
  409. }
  410. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  411. short len)
  412. {
  413. char temp[4];
  414. int retval, i;
  415. int rdcount = 0;
  416. int count;
  417. while (len > 0) {
  418. count = (len > 3) ? 4 : len;
  419. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  420. if (retval > 0) {
  421. for (i = 0; i < len; i++)
  422. buf[rdcount + i] = temp[i];
  423. len -= count;
  424. rdcount += count;
  425. } else
  426. return (retval < 0) ? retval : -EFAULT;
  427. }
  428. return rdcount;
  429. }
  430. static struct i2c_adapter i2c_adap_template = {
  431. .owner = THIS_MODULE,
  432. .name = "usbvision",
  433. .id = I2C_HW_B_BT848, /* FIXME */
  434. .client_register = attach_inform,
  435. .client_unregister = detach_inform,
  436. #ifdef I2C_ADAP_CLASS_TV_ANALOG
  437. .class = I2C_ADAP_CLASS_TV_ANALOG,
  438. #else
  439. .class = I2C_CLASS_TV_ANALOG,
  440. #endif
  441. };
  442. static struct i2c_client i2c_client_template = {
  443. .name = "usbvision internal",
  444. };
  445. /*
  446. * Overrides for Emacs so that we follow Linus's tabbing style.
  447. * ---------------------------------------------------------------------------
  448. * Local variables:
  449. * c-basic-offset: 8
  450. * End:
  451. */