usbvision-i2c.c 14 KB

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