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