usbvision-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. add[0] = addr;
  124. if (flags & I2C_M_RD)
  125. ret = try_read_address(i2c_adap, addr, retries);
  126. else
  127. ret = try_write_address(i2c_adap, addr, retries);
  128. if (ret != 1) {
  129. return -EREMOTEIO;
  130. }
  131. }
  132. return 0;
  133. }
  134. static int
  135. usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  136. {
  137. struct i2c_msg *pmsg;
  138. struct usb_usbvision *usbvision;
  139. int i, ret;
  140. unsigned char addr;
  141. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  142. for (i = 0; i < num; i++) {
  143. pmsg = &msgs[i];
  144. ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
  145. if (ret != 0) {
  146. PDEBUG(DBG_I2C,"got NAK from device, message #%d", i);
  147. return (ret < 0) ? ret : -EREMOTEIO;
  148. }
  149. if (pmsg->flags & I2C_M_RD) {
  150. /* read bytes into buffer */
  151. ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
  152. if (ret < pmsg->len) {
  153. return (ret < 0) ? ret : -EREMOTEIO;
  154. }
  155. } else {
  156. /* write bytes from buffer */
  157. ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
  158. if (ret < pmsg->len) {
  159. return (ret < 0) ? ret : -EREMOTEIO;
  160. }
  161. }
  162. }
  163. return num;
  164. }
  165. static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
  166. {
  167. return 0;
  168. }
  169. static u32 functionality(struct i2c_adapter *adap)
  170. {
  171. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  172. }
  173. /* -----exported algorithm data: ------------------------------------- */
  174. static struct i2c_algorithm usbvision_algo = {
  175. .master_xfer = usbvision_i2c_xfer,
  176. .smbus_xfer = NULL,
  177. .algo_control = algo_control,
  178. .functionality = functionality,
  179. };
  180. /*
  181. * registering functions to load algorithms at runtime
  182. */
  183. static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
  184. {
  185. PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
  186. PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
  187. /* register new adapter to i2c module... */
  188. adap->algo = &usbvision_algo;
  189. adap->timeout = 100; /* default values, should */
  190. adap->retries = 3; /* be replaced by defines */
  191. i2c_add_adapter(adap);
  192. PDEBUG(DBG_I2C,"i2c bus for %s registered", adap->name);
  193. return 0;
  194. }
  195. /* ----------------------------------------------------------------------- */
  196. /* usbvision specific I2C functions */
  197. /* ----------------------------------------------------------------------- */
  198. static struct i2c_adapter i2c_adap_template;
  199. static struct i2c_client i2c_client_template;
  200. int usbvision_i2c_register(struct usb_usbvision *usbvision)
  201. {
  202. memcpy(&usbvision->i2c_adap, &i2c_adap_template,
  203. sizeof(struct i2c_adapter));
  204. memcpy(&usbvision->i2c_client, &i2c_client_template,
  205. sizeof(struct i2c_client));
  206. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  207. " #%d", usbvision->vdev->minor & 0x1f);
  208. PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
  209. usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
  210. i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
  211. i2c_set_clientdata(&usbvision->i2c_client, usbvision);
  212. usbvision->i2c_client.adapter = &usbvision->i2c_adap;
  213. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  214. printk(KERN_ERR "usbvision_register: can't write reg\n");
  215. return -EBUSY;
  216. }
  217. #ifdef CONFIG_MODULES
  218. /* Request the load of the i2c modules we need */
  219. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  220. case CODEC_SAA7113:
  221. request_module("saa7115");
  222. break;
  223. case CODEC_SAA7111:
  224. request_module("saa7115");
  225. break;
  226. }
  227. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  228. request_module("tuner");
  229. }
  230. #endif
  231. return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
  232. }
  233. int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
  234. {
  235. i2c_del_adapter(&(usbvision->i2c_adap));
  236. PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
  237. return 0;
  238. }
  239. void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
  240. void *arg)
  241. {
  242. i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
  243. }
  244. static int attach_inform(struct i2c_client *client)
  245. {
  246. struct usb_usbvision *usbvision;
  247. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  248. switch (client->addr << 1) {
  249. case 0x42 << 1:
  250. case 0x43 << 1:
  251. case 0x4a << 1:
  252. case 0x4b << 1:
  253. PDEBUG(DBG_I2C,"attach_inform: tda9887 detected.");
  254. break;
  255. case 0x42:
  256. PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
  257. break;
  258. case 0x4a:
  259. PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
  260. break;
  261. case 0x48:
  262. PDEBUG(DBG_I2C,"attach_inform: saa7111 detected.");
  263. break;
  264. case 0xa0:
  265. PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
  266. break;
  267. default:
  268. {
  269. struct tuner_setup tun_setup;
  270. PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
  271. usbvision->tuner_addr = client->addr;
  272. if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
  273. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  274. tun_setup.type = usbvision->tuner_type;
  275. tun_setup.addr = usbvision->tuner_addr;
  276. call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
  277. }
  278. }
  279. break;
  280. }
  281. return 0;
  282. }
  283. static int detach_inform(struct i2c_client *client)
  284. {
  285. struct usb_usbvision *usbvision;
  286. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  287. PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
  288. return 0;
  289. }
  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. for (retries = 5;;) {
  296. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  297. if (rc < 0)
  298. return rc;
  299. /* Initiate byte read cycle */
  300. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  301. /* d3 0=Wr 1=Rd */
  302. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  303. (len & 0x07) | 0x18);
  304. if (rc < 0)
  305. return rc;
  306. /* Test for Busy and ACK */
  307. do {
  308. /* USBVISION_SER_CONT -> d4 == 0 busy */
  309. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  310. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  311. if (rc < 0)
  312. return rc;
  313. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  314. if ((rc & 0x20) == 0) /* Ack? */
  315. break;
  316. /* I2C abort */
  317. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  318. if (rc < 0)
  319. return rc;
  320. if (--retries < 0)
  321. return -1;
  322. }
  323. switch (len) {
  324. case 4:
  325. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  326. case 3:
  327. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  328. case 2:
  329. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  330. case 1:
  331. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  332. break;
  333. default:
  334. printk(KERN_ERR
  335. "usbvision_i2c_read_max4: buffer length > 4\n");
  336. }
  337. if (i2c_debug & DBG_I2C) {
  338. int idx;
  339. for (idx = 0; idx < len; idx++) {
  340. PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
  341. }
  342. }
  343. return len;
  344. }
  345. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  346. unsigned char addr, const char *buf,
  347. short len)
  348. {
  349. int rc, retries;
  350. int i;
  351. unsigned char value[6];
  352. unsigned char ser_cont;
  353. ser_cont = (len & 0x07) | 0x10;
  354. value[0] = addr;
  355. value[1] = ser_cont;
  356. for (i = 0; i < len; i++)
  357. value[i + 2] = buf[i];
  358. for (retries = 5;;) {
  359. rc = usb_control_msg(usbvision->dev,
  360. usb_sndctrlpipe(usbvision->dev, 1),
  361. USBVISION_OP_CODE,
  362. USB_DIR_OUT | USB_TYPE_VENDOR |
  363. USB_RECIP_ENDPOINT, 0,
  364. (__u16) USBVISION_SER_ADRS, value,
  365. len + 2, HZ);
  366. if (rc < 0)
  367. return rc;
  368. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  369. (len & 0x07) | 0x10);
  370. if (rc < 0)
  371. return rc;
  372. /* Test for Busy and ACK */
  373. do {
  374. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  375. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  376. if (rc < 0)
  377. return rc;
  378. if ((rc & 0x20) == 0) /* Ack? */
  379. break;
  380. /* I2C abort */
  381. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  382. if (--retries < 0)
  383. return -1;
  384. }
  385. if (i2c_debug & DBG_I2C) {
  386. int idx;
  387. for (idx = 0; idx < len; idx++) {
  388. PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
  389. }
  390. }
  391. return len;
  392. }
  393. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  394. short len)
  395. {
  396. char *bufPtr = buf;
  397. int retval;
  398. int wrcount = 0;
  399. int count;
  400. int maxLen = 4;
  401. while (len > 0) {
  402. count = (len > maxLen) ? maxLen : len;
  403. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  404. if (retval > 0) {
  405. len -= count;
  406. bufPtr += count;
  407. wrcount += count;
  408. } else
  409. return (retval < 0) ? retval : -EFAULT;
  410. }
  411. return wrcount;
  412. }
  413. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  414. short len)
  415. {
  416. char temp[4];
  417. int retval, i;
  418. int rdcount = 0;
  419. int count;
  420. while (len > 0) {
  421. count = (len > 3) ? 4 : len;
  422. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  423. if (retval > 0) {
  424. for (i = 0; i < len; i++)
  425. buf[rdcount + i] = temp[i];
  426. len -= count;
  427. rdcount += count;
  428. } else
  429. return (retval < 0) ? retval : -EFAULT;
  430. }
  431. return rdcount;
  432. }
  433. static struct i2c_adapter i2c_adap_template = {
  434. .owner = THIS_MODULE,
  435. .name = "usbvision",
  436. .id = I2C_HW_B_BT848, /* FIXME */
  437. .client_register = attach_inform,
  438. .client_unregister = detach_inform,
  439. #ifdef I2C_ADAP_CLASS_TV_ANALOG
  440. .class = I2C_ADAP_CLASS_TV_ANALOG,
  441. #else
  442. .class = I2C_CLASS_TV_ANALOG,
  443. #endif
  444. };
  445. static struct i2c_client i2c_client_template = {
  446. .name = "usbvision internal",
  447. };
  448. /*
  449. * Overrides for Emacs so that we follow Linus's tabbing style.
  450. * ---------------------------------------------------------------------------
  451. * Local variables:
  452. * c-basic-offset: 8
  453. * End:
  454. */