usbvision-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 inline int try_write_address(struct i2c_adapter *i2c_adap,
  47. unsigned char addr, int retries)
  48. {
  49. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  50. void *data;
  51. int i, ret = -1;
  52. char buf[4];
  53. data = i2c_get_adapdata(i2c_adap);
  54. buf[0] = 0x00;
  55. for (i = 0; i <= retries; i++) {
  56. ret = (adap->outb(data, addr, buf, 1));
  57. if (ret == 1)
  58. break; /* success! */
  59. udelay(5 /*adap->udelay */ );
  60. if (i == retries) /* no success */
  61. break;
  62. udelay(adap->udelay);
  63. }
  64. if (i) {
  65. PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
  66. PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
  67. }
  68. return ret;
  69. }
  70. static inline int try_read_address(struct i2c_adapter *i2c_adap,
  71. unsigned char addr, int retries)
  72. {
  73. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  74. void *data;
  75. int i, ret = -1;
  76. char buf[4];
  77. data = i2c_get_adapdata(i2c_adap);
  78. for (i = 0; i <= retries; i++) {
  79. ret = (adap->inb(data, addr, buf, 1));
  80. if (ret == 1)
  81. break; /* success! */
  82. udelay(5 /*adap->udelay */ );
  83. if (i == retries) /* no success */
  84. break;
  85. udelay(adap->udelay);
  86. }
  87. if (i) {
  88. PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
  89. PDEBUG(DBG_ALGO,"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. usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  138. {
  139. struct i2c_msg *pmsg;
  140. struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
  141. void *data;
  142. int i, ret;
  143. unsigned char addr;
  144. data = i2c_get_adapdata(i2c_adap);
  145. for (i = 0; i < num; i++) {
  146. pmsg = &msgs[i];
  147. ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
  148. if (ret != 0) {
  149. PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i);
  150. return (ret < 0) ? ret : -EREMOTEIO;
  151. }
  152. if (pmsg->flags & I2C_M_RD) {
  153. /* read bytes into buffer */
  154. ret = (adap->inb(data, addr, pmsg->buf, pmsg->len));
  155. if (ret < pmsg->len) {
  156. return (ret < 0) ? ret : -EREMOTEIO;
  157. }
  158. } else {
  159. /* write bytes from buffer */
  160. ret = (adap->outb(data, addr, pmsg->buf, pmsg->len));
  161. if (ret < pmsg->len) {
  162. return (ret < 0) ? ret : -EREMOTEIO;
  163. }
  164. }
  165. }
  166. return num;
  167. }
  168. static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
  169. {
  170. return 0;
  171. }
  172. static u32 usb_func(struct i2c_adapter *adap)
  173. {
  174. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  175. }
  176. /* -----exported algorithm data: ------------------------------------- */
  177. static struct i2c_algorithm i2c_usb_algo = {
  178. .master_xfer = usb_xfer,
  179. .smbus_xfer = NULL,
  180. .algo_control = algo_control,
  181. .functionality = usb_func,
  182. };
  183. /*
  184. * registering functions to load algorithms at runtime
  185. */
  186. int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
  187. {
  188. PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
  189. PDEBUG(DBG_ALGO, "ALGO debugging is enabled [i2c]");
  190. /* register new adapter to i2c module... */
  191. adap->algo = &i2c_usb_algo;
  192. adap->timeout = 100; /* default values, should */
  193. adap->retries = 3; /* be replaced by defines */
  194. i2c_add_adapter(adap);
  195. PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
  196. return 0;
  197. }
  198. int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
  199. {
  200. i2c_del_adapter(adap);
  201. PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
  202. return 0;
  203. }
  204. /* ----------------------------------------------------------------------- */
  205. /* usbvision specific I2C functions */
  206. /* ----------------------------------------------------------------------- */
  207. static struct i2c_adapter i2c_adap_template;
  208. static struct i2c_algo_usb_data i2c_algo_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_algo, &i2c_algo_template,
  215. sizeof(struct i2c_algo_usb_data));
  216. memcpy(&usbvision->i2c_client, &i2c_client_template,
  217. sizeof(struct i2c_client));
  218. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  219. " #%d", usbvision->vdev->minor & 0x1f);
  220. PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
  221. i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
  222. i2c_set_clientdata(&usbvision->i2c_client, usbvision);
  223. i2c_set_algo_usb_data(&usbvision->i2c_algo, usbvision);
  224. usbvision->i2c_adap.algo_data = &usbvision->i2c_algo;
  225. usbvision->i2c_client.adapter = &usbvision->i2c_adap;
  226. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  227. printk(KERN_ERR "usbvision_init_i2c: can't write reg\n");
  228. return -EBUSY;
  229. }
  230. #ifdef CONFIG_MODULES
  231. /* Request the load of the i2c modules we need */
  232. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  233. case CODEC_SAA7113:
  234. request_module("saa7115");
  235. break;
  236. case CODEC_SAA7111:
  237. request_module("saa7115");
  238. break;
  239. }
  240. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  241. request_module("tuner");
  242. }
  243. #endif
  244. return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
  245. }
  246. void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
  247. void *arg)
  248. {
  249. BUG_ON(NULL == usbvision->i2c_adap.algo_data);
  250. i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
  251. }
  252. static int attach_inform(struct i2c_client *client)
  253. {
  254. struct usb_usbvision *usbvision;
  255. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  256. switch (client->addr << 1) {
  257. case 0x43:
  258. case 0x4b:
  259. {
  260. struct tuner_setup tun_setup;
  261. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  262. tun_setup.type = TUNER_TDA9887;
  263. tun_setup.addr = client->addr;
  264. call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
  265. break;
  266. }
  267. case 0x42:
  268. PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
  269. break;
  270. case 0x4a:
  271. PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
  272. break;
  273. case 0xa0:
  274. PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
  275. break;
  276. default:
  277. {
  278. struct tuner_setup tun_setup;
  279. PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
  280. usbvision->tuner_addr = client->addr;
  281. if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
  282. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  283. tun_setup.type = usbvision->tuner_type;
  284. tun_setup.addr = usbvision->tuner_addr;
  285. call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
  286. }
  287. }
  288. break;
  289. }
  290. return 0;
  291. }
  292. static int detach_inform(struct i2c_client *client)
  293. {
  294. struct usb_usbvision *usbvision;
  295. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  296. usbvision = (struct usb_usbvision *)client->adapter->data;
  297. #else
  298. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  299. #endif
  300. PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
  301. return 0;
  302. }
  303. static int
  304. usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
  305. char *buf, short len)
  306. {
  307. int rc, retries;
  308. for (retries = 5;;) {
  309. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  310. if (rc < 0)
  311. return rc;
  312. /* Initiate byte read cycle */
  313. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  314. /* d3 0=Wr 1=Rd */
  315. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  316. (len & 0x07) | 0x18);
  317. if (rc < 0)
  318. return rc;
  319. /* Test for Busy and ACK */
  320. do {
  321. /* USBVISION_SER_CONT -> d4 == 0 busy */
  322. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  323. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  324. if (rc < 0)
  325. return rc;
  326. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  327. if ((rc & 0x20) == 0) /* Ack? */
  328. break;
  329. /* I2C abort */
  330. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  331. if (rc < 0)
  332. return rc;
  333. if (--retries < 0)
  334. return -1;
  335. }
  336. switch (len) {
  337. case 4:
  338. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  339. case 3:
  340. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  341. case 2:
  342. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  343. case 1:
  344. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  345. break;
  346. default:
  347. printk(KERN_ERR
  348. "usbvision_i2c_read_max4: buffer length > 4\n");
  349. }
  350. if (i2c_debug & DBG_I2C) {
  351. int idx;
  352. for (idx = 0; idx < len; idx++) {
  353. PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
  354. }
  355. }
  356. return len;
  357. }
  358. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  359. unsigned char addr, const char *buf,
  360. short len)
  361. {
  362. int rc, retries;
  363. int i;
  364. unsigned char value[6];
  365. unsigned char ser_cont;
  366. ser_cont = (len & 0x07) | 0x10;
  367. value[0] = addr;
  368. value[1] = ser_cont;
  369. for (i = 0; i < len; i++)
  370. value[i + 2] = buf[i];
  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. if (rc < 0)
  380. return rc;
  381. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  382. (len & 0x07) | 0x10);
  383. if (rc < 0)
  384. return rc;
  385. /* Test for Busy and ACK */
  386. do {
  387. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  388. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  389. if (rc < 0)
  390. return rc;
  391. if ((rc & 0x20) == 0) /* Ack? */
  392. break;
  393. /* I2C abort */
  394. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  395. if (--retries < 0)
  396. return -1;
  397. }
  398. if (i2c_debug & DBG_I2C) {
  399. int idx;
  400. for (idx = 0; idx < len; idx++) {
  401. PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
  402. }
  403. }
  404. return len;
  405. }
  406. static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
  407. short len)
  408. {
  409. char *bufPtr = buf;
  410. int retval;
  411. int wrcount = 0;
  412. int count;
  413. int maxLen = 4;
  414. struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
  415. while (len > 0) {
  416. count = (len > maxLen) ? maxLen : len;
  417. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  418. if (retval > 0) {
  419. len -= count;
  420. bufPtr += count;
  421. wrcount += count;
  422. } else
  423. return (retval < 0) ? retval : -EFAULT;
  424. }
  425. return wrcount;
  426. }
  427. static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
  428. short len)
  429. {
  430. char temp[4];
  431. int retval, i;
  432. int rdcount = 0;
  433. int count;
  434. struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
  435. while (len > 0) {
  436. count = (len > 3) ? 4 : len;
  437. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  438. if (retval > 0) {
  439. for (i = 0; i < len; i++)
  440. buf[rdcount + i] = temp[i];
  441. len -= count;
  442. rdcount += count;
  443. } else
  444. return (retval < 0) ? retval : -EFAULT;
  445. }
  446. return rdcount;
  447. }
  448. static struct i2c_algo_usb_data i2c_algo_template = {
  449. .data = NULL,
  450. .inb = usbvision_i2c_read,
  451. .outb = usbvision_i2c_write,
  452. .udelay = 10,
  453. .mdelay = 10,
  454. .timeout = 100,
  455. };
  456. static struct i2c_adapter i2c_adap_template = {
  457. .owner = THIS_MODULE,
  458. .name = "usbvision",
  459. .id = I2C_HW_B_BT848, /* FIXME */
  460. .algo = NULL,
  461. .algo_data = NULL,
  462. .client_register = attach_inform,
  463. .client_unregister = detach_inform,
  464. #if defined (I2C_ADAP_CLASS_TV_ANALOG)
  465. .class = I2C_ADAP_CLASS_TV_ANALOG,
  466. #elif defined (I2C_CLASS_TV_ANALOG)
  467. .class = I2C_CLASS_TV_ANALOG,
  468. #endif
  469. };
  470. static struct i2c_client i2c_client_template = {
  471. .name = "usbvision internal",
  472. };
  473. EXPORT_SYMBOL(usbvision_i2c_usb_add_bus);
  474. EXPORT_SYMBOL(usbvision_i2c_usb_del_bus);
  475. /*
  476. * Overrides for Emacs so that we follow Linus's tabbing style.
  477. * ---------------------------------------------------------------------------
  478. * Local variables:
  479. * c-basic-offset: 8
  480. * End:
  481. */