usbvision-i2c.c 14 KB

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