PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/media/video/usbvision/usbvision-i2c.c

https://github.com/JoeyJiao/android-huawei-kernel-common
C | 470 lines | 344 code | 73 blank | 53 comment | 69 complexity | 0991811c33d7543ae12472c971a0af88 MD5 | raw file
Possible License(s): GPL-2.0
  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/init.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/ioport.h>
  32. #include <linux/errno.h>
  33. #include <linux/usb.h>
  34. #include <linux/i2c.h>
  35. #include "usbvision.h"
  36. #define DBG_I2C 1<<0
  37. static int i2c_debug;
  38. module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
  39. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  40. #define PDEBUG(level, fmt, args...) { \
  41. if (i2c_debug & (level)) \
  42. printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
  43. __func__, __LINE__ , ## args); \
  44. }
  45. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  46. short len);
  47. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  48. short len);
  49. static inline int try_write_address(struct i2c_adapter *i2c_adap,
  50. unsigned char addr, int retries)
  51. {
  52. struct usb_usbvision *usbvision;
  53. int i, ret = -1;
  54. char buf[4];
  55. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  56. buf[0] = 0x00;
  57. for (i = 0; i <= retries; i++) {
  58. ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
  59. if (ret == 1)
  60. break; /* success! */
  61. udelay(5);
  62. if (i == retries) /* no success */
  63. break;
  64. udelay(10);
  65. }
  66. if (i) {
  67. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  68. PDEBUG(DBG_I2C,"Maybe there's no device at this address");
  69. }
  70. return ret;
  71. }
  72. static inline int try_read_address(struct i2c_adapter *i2c_adap,
  73. unsigned char addr, int retries)
  74. {
  75. struct usb_usbvision *usbvision;
  76. int i, ret = -1;
  77. char buf[4];
  78. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  79. for (i = 0; i <= retries; i++) {
  80. ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
  81. if (ret == 1)
  82. break; /* success! */
  83. udelay(5);
  84. if (i == retries) /* no success */
  85. break;
  86. udelay(10);
  87. }
  88. if (i) {
  89. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  90. PDEBUG(DBG_I2C,"Maybe there's no device at this address");
  91. }
  92. return ret;
  93. }
  94. static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  95. struct i2c_msg *msg, int retries,
  96. unsigned char *add)
  97. {
  98. unsigned short flags = msg->flags;
  99. unsigned char addr;
  100. int ret;
  101. if ((flags & I2C_M_TEN)) {
  102. /* a ten bit address */
  103. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  104. /* try extended address code... */
  105. ret = try_write_address(i2c_adap, addr, retries);
  106. if (ret != 1) {
  107. dev_err(&i2c_adap->dev,
  108. "died at extended address code, while writing\n");
  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. dev_err(&i2c_adap->dev,
  118. "died at extended address code, while reading\n");
  119. return -EREMOTEIO;
  120. }
  121. }
  122. } else { /* normal 7bit address */
  123. addr = (msg->addr << 1);
  124. if (flags & I2C_M_RD)
  125. addr |= 1;
  126. add[0] = addr;
  127. if (flags & I2C_M_RD)
  128. ret = try_read_address(i2c_adap, addr, retries);
  129. else
  130. ret = try_write_address(i2c_adap, addr, retries);
  131. if (ret != 1) {
  132. return -EREMOTEIO;
  133. }
  134. }
  135. return 0;
  136. }
  137. static int
  138. usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  139. {
  140. struct i2c_msg *pmsg;
  141. struct usb_usbvision *usbvision;
  142. int i, ret;
  143. unsigned char addr = 0;
  144. usbvision = (struct usb_usbvision *)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_I2C,"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 = (usbvision_i2c_read(usbvision, 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 = (usbvision_i2c_write(usbvision, 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 u32 functionality(struct i2c_adapter *adap)
  169. {
  170. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  171. }
  172. /* -----exported algorithm data: ------------------------------------- */
  173. static struct i2c_algorithm usbvision_algo = {
  174. .master_xfer = usbvision_i2c_xfer,
  175. .smbus_xfer = NULL,
  176. .functionality = functionality,
  177. };
  178. /* ----------------------------------------------------------------------- */
  179. /* usbvision specific I2C functions */
  180. /* ----------------------------------------------------------------------- */
  181. static struct i2c_adapter i2c_adap_template;
  182. int usbvision_i2c_register(struct usb_usbvision *usbvision)
  183. {
  184. static unsigned short saa711x_addrs[] = {
  185. 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
  186. 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
  187. I2C_CLIENT_END };
  188. memcpy(&usbvision->i2c_adap, &i2c_adap_template,
  189. sizeof(struct i2c_adapter));
  190. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  191. " #%d", usbvision->vdev->num);
  192. PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
  193. usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
  194. i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
  195. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  196. printk(KERN_ERR "usbvision_register: can't write reg\n");
  197. return -EBUSY;
  198. }
  199. PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
  200. PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
  201. /* register new adapter to i2c module... */
  202. usbvision->i2c_adap.algo = &usbvision_algo;
  203. usbvision->i2c_adap.timeout = 100; /* default values, should */
  204. usbvision->i2c_adap.retries = 3; /* be replaced by defines */
  205. i2c_add_adapter(&usbvision->i2c_adap);
  206. PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
  207. /* Request the load of the i2c modules we need */
  208. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  209. case CODEC_SAA7113:
  210. case CODEC_SAA7111:
  211. v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
  212. &usbvision->i2c_adap, "saa7115",
  213. "saa7115_auto", 0, saa711x_addrs);
  214. break;
  215. }
  216. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  217. struct v4l2_subdev *sd;
  218. enum v4l2_i2c_tuner_type type;
  219. struct tuner_setup tun_setup;
  220. sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
  221. &usbvision->i2c_adap, "tuner",
  222. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
  223. /* depending on whether we found a demod or not, select
  224. the tuner type. */
  225. type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
  226. sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
  227. &usbvision->i2c_adap, "tuner",
  228. "tuner", 0, v4l2_i2c_tuner_addrs(type));
  229. if (usbvision->tuner_type != -1) {
  230. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  231. tun_setup.type = usbvision->tuner_type;
  232. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  233. call_all(usbvision, tuner, s_type_addr, &tun_setup);
  234. }
  235. }
  236. return 0;
  237. }
  238. int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
  239. {
  240. i2c_del_adapter(&(usbvision->i2c_adap));
  241. PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
  242. return 0;
  243. }
  244. static int
  245. usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
  246. char *buf, short len)
  247. {
  248. int rc, retries;
  249. for (retries = 5;;) {
  250. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  251. if (rc < 0)
  252. return rc;
  253. /* Initiate byte read cycle */
  254. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  255. /* d3 0=Wr 1=Rd */
  256. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  257. (len & 0x07) | 0x18);
  258. if (rc < 0)
  259. return rc;
  260. /* Test for Busy and ACK */
  261. do {
  262. /* USBVISION_SER_CONT -> d4 == 0 busy */
  263. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  264. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  265. if (rc < 0)
  266. return rc;
  267. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  268. if ((rc & 0x20) == 0) /* Ack? */
  269. break;
  270. /* I2C abort */
  271. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  272. if (rc < 0)
  273. return rc;
  274. if (--retries < 0)
  275. return -1;
  276. }
  277. switch (len) {
  278. case 4:
  279. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  280. case 3:
  281. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  282. case 2:
  283. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  284. case 1:
  285. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  286. break;
  287. default:
  288. printk(KERN_ERR
  289. "usbvision_i2c_read_max4: buffer length > 4\n");
  290. }
  291. if (i2c_debug & DBG_I2C) {
  292. int idx;
  293. for (idx = 0; idx < len; idx++) {
  294. PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
  295. }
  296. }
  297. return len;
  298. }
  299. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  300. unsigned char addr, const char *buf,
  301. short len)
  302. {
  303. int rc, retries;
  304. int i;
  305. unsigned char value[6];
  306. unsigned char ser_cont;
  307. ser_cont = (len & 0x07) | 0x10;
  308. value[0] = addr;
  309. value[1] = ser_cont;
  310. for (i = 0; i < len; i++)
  311. value[i + 2] = buf[i];
  312. for (retries = 5;;) {
  313. rc = usb_control_msg(usbvision->dev,
  314. usb_sndctrlpipe(usbvision->dev, 1),
  315. USBVISION_OP_CODE,
  316. USB_DIR_OUT | USB_TYPE_VENDOR |
  317. USB_RECIP_ENDPOINT, 0,
  318. (__u16) USBVISION_SER_ADRS, value,
  319. len + 2, HZ);
  320. if (rc < 0)
  321. return rc;
  322. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  323. (len & 0x07) | 0x10);
  324. if (rc < 0)
  325. return rc;
  326. /* Test for Busy and ACK */
  327. do {
  328. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  329. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  330. if (rc < 0)
  331. return rc;
  332. if ((rc & 0x20) == 0) /* Ack? */
  333. break;
  334. /* I2C abort */
  335. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  336. if (--retries < 0)
  337. return -1;
  338. }
  339. if (i2c_debug & DBG_I2C) {
  340. int idx;
  341. for (idx = 0; idx < len; idx++) {
  342. PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
  343. }
  344. }
  345. return len;
  346. }
  347. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  348. short len)
  349. {
  350. char *bufPtr = buf;
  351. int retval;
  352. int wrcount = 0;
  353. int count;
  354. int maxLen = 4;
  355. while (len > 0) {
  356. count = (len > maxLen) ? maxLen : len;
  357. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  358. if (retval > 0) {
  359. len -= count;
  360. bufPtr += count;
  361. wrcount += count;
  362. } else
  363. return (retval < 0) ? retval : -EFAULT;
  364. }
  365. return wrcount;
  366. }
  367. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  368. short len)
  369. {
  370. char temp[4];
  371. int retval, i;
  372. int rdcount = 0;
  373. int count;
  374. while (len > 0) {
  375. count = (len > 3) ? 4 : len;
  376. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  377. if (retval > 0) {
  378. for (i = 0; i < len; i++)
  379. buf[rdcount + i] = temp[i];
  380. len -= count;
  381. rdcount += count;
  382. } else
  383. return (retval < 0) ? retval : -EFAULT;
  384. }
  385. return rdcount;
  386. }
  387. static struct i2c_adapter i2c_adap_template = {
  388. .owner = THIS_MODULE,
  389. .name = "usbvision",
  390. };
  391. /*
  392. * Overrides for Emacs so that we follow Linus's tabbing style.
  393. * ---------------------------------------------------------------------------
  394. * Local variables:
  395. * c-basic-offset: 8
  396. * End:
  397. */