PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/kipill-nn/Kernel-for-Mega
C | 530 lines | 394 code | 82 blank | 54 comment | 71 complexity | 81ed1b843b28c99e2386c342d859ae59 MD5 | raw file
  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;
  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)) \
  44. printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
  45. __func__, __LINE__ , ## args); \
  46. }
  47. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  48. short len);
  49. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  50. short len);
  51. static inline int try_write_address(struct i2c_adapter *i2c_adap,
  52. unsigned char addr, int retries)
  53. {
  54. struct usb_usbvision *usbvision;
  55. int i, ret = -1;
  56. char buf[4];
  57. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  58. buf[0] = 0x00;
  59. for (i = 0; i <= retries; i++) {
  60. ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
  61. if (ret == 1)
  62. break; /* success! */
  63. udelay(5);
  64. if (i == retries) /* no success */
  65. break;
  66. udelay(10);
  67. }
  68. if (i) {
  69. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  70. PDEBUG(DBG_I2C,"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 usb_usbvision *usbvision;
  78. int i, ret = -1;
  79. char buf[4];
  80. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  81. for (i = 0; i <= retries; i++) {
  82. ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
  83. if (ret == 1)
  84. break; /* success! */
  85. udelay(5);
  86. if (i == retries) /* no success */
  87. break;
  88. udelay(10);
  89. }
  90. if (i) {
  91. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  92. PDEBUG(DBG_I2C,"Maybe there's no device at this address");
  93. }
  94. return ret;
  95. }
  96. static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  97. struct i2c_msg *msg, int retries,
  98. unsigned char *add)
  99. {
  100. unsigned short flags = msg->flags;
  101. unsigned char addr;
  102. int ret;
  103. if ((flags & I2C_M_TEN)) {
  104. /* a ten bit address */
  105. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  106. /* try extended address code... */
  107. ret = try_write_address(i2c_adap, addr, retries);
  108. if (ret != 1) {
  109. err("died at extended address code, while writing");
  110. return -EREMOTEIO;
  111. }
  112. add[0] = addr;
  113. if (flags & I2C_M_RD) {
  114. /* okay, now switch into reading mode */
  115. addr |= 0x01;
  116. ret = try_read_address(i2c_adap, addr, retries);
  117. if (ret != 1) {
  118. err("died at extended address code, while reading");
  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. * registering functions to load algorithms at runtime
  180. */
  181. static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
  182. {
  183. PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
  184. PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
  185. /* register new adapter to i2c module... */
  186. adap->algo = &usbvision_algo;
  187. adap->timeout = 100; /* default values, should */
  188. adap->retries = 3; /* be replaced by defines */
  189. i2c_add_adapter(adap);
  190. PDEBUG(DBG_I2C,"i2c bus for %s registered", adap->name);
  191. return 0;
  192. }
  193. /* ----------------------------------------------------------------------- */
  194. /* usbvision specific I2C functions */
  195. /* ----------------------------------------------------------------------- */
  196. static struct i2c_adapter i2c_adap_template;
  197. static struct i2c_client i2c_client_template;
  198. int usbvision_i2c_register(struct usb_usbvision *usbvision)
  199. {
  200. memcpy(&usbvision->i2c_adap, &i2c_adap_template,
  201. sizeof(struct i2c_adapter));
  202. memcpy(&usbvision->i2c_client, &i2c_client_template,
  203. sizeof(struct i2c_client));
  204. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  205. " #%d", usbvision->vdev->num);
  206. PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
  207. usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
  208. i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
  209. i2c_set_clientdata(&usbvision->i2c_client, usbvision);
  210. usbvision->i2c_client.adapter = &usbvision->i2c_adap;
  211. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  212. printk(KERN_ERR "usbvision_register: can't write reg\n");
  213. return -EBUSY;
  214. }
  215. #ifdef CONFIG_MODULES
  216. /* Request the load of the i2c modules we need */
  217. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  218. case CODEC_SAA7113:
  219. request_module("saa7115");
  220. break;
  221. case CODEC_SAA7111:
  222. request_module("saa7115");
  223. break;
  224. }
  225. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  226. request_module("tuner");
  227. }
  228. #endif
  229. return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
  230. }
  231. int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
  232. {
  233. i2c_del_adapter(&(usbvision->i2c_adap));
  234. PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
  235. return 0;
  236. }
  237. void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
  238. void *arg)
  239. {
  240. i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
  241. }
  242. static int attach_inform(struct i2c_client *client)
  243. {
  244. struct usb_usbvision *usbvision;
  245. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  246. switch (client->addr << 1) {
  247. case 0x42 << 1:
  248. case 0x43 << 1:
  249. case 0x4a << 1:
  250. case 0x4b << 1:
  251. PDEBUG(DBG_I2C,"attach_inform: tda9887 detected.");
  252. break;
  253. case 0x42:
  254. PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
  255. break;
  256. case 0x4a:
  257. PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
  258. break;
  259. case 0x48:
  260. PDEBUG(DBG_I2C,"attach_inform: saa7111 detected.");
  261. break;
  262. case 0xa0:
  263. PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
  264. break;
  265. default:
  266. {
  267. struct tuner_setup tun_setup;
  268. PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
  269. usbvision->tuner_addr = client->addr;
  270. if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
  271. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  272. tun_setup.type = usbvision->tuner_type;
  273. tun_setup.addr = usbvision->tuner_addr;
  274. call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
  275. }
  276. }
  277. break;
  278. }
  279. return 0;
  280. }
  281. static int detach_inform(struct i2c_client *client)
  282. {
  283. struct usb_usbvision *usbvision;
  284. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  285. PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
  286. return 0;
  287. }
  288. static int
  289. usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
  290. char *buf, short len)
  291. {
  292. int rc, retries;
  293. for (retries = 5;;) {
  294. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  295. if (rc < 0)
  296. return rc;
  297. /* Initiate byte read cycle */
  298. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  299. /* d3 0=Wr 1=Rd */
  300. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  301. (len & 0x07) | 0x18);
  302. if (rc < 0)
  303. return rc;
  304. /* Test for Busy and ACK */
  305. do {
  306. /* USBVISION_SER_CONT -> d4 == 0 busy */
  307. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  308. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  309. if (rc < 0)
  310. return rc;
  311. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  312. if ((rc & 0x20) == 0) /* Ack? */
  313. break;
  314. /* I2C abort */
  315. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  316. if (rc < 0)
  317. return rc;
  318. if (--retries < 0)
  319. return -1;
  320. }
  321. switch (len) {
  322. case 4:
  323. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  324. case 3:
  325. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  326. case 2:
  327. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  328. case 1:
  329. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  330. break;
  331. default:
  332. printk(KERN_ERR
  333. "usbvision_i2c_read_max4: buffer length > 4\n");
  334. }
  335. if (i2c_debug & DBG_I2C) {
  336. int idx;
  337. for (idx = 0; idx < len; idx++) {
  338. PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
  339. }
  340. }
  341. return len;
  342. }
  343. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  344. unsigned char addr, const char *buf,
  345. short len)
  346. {
  347. int rc, retries;
  348. int i;
  349. unsigned char value[6];
  350. unsigned char ser_cont;
  351. ser_cont = (len & 0x07) | 0x10;
  352. value[0] = addr;
  353. value[1] = ser_cont;
  354. for (i = 0; i < len; i++)
  355. value[i + 2] = buf[i];
  356. for (retries = 5;;) {
  357. rc = usb_control_msg(usbvision->dev,
  358. usb_sndctrlpipe(usbvision->dev, 1),
  359. USBVISION_OP_CODE,
  360. USB_DIR_OUT | USB_TYPE_VENDOR |
  361. USB_RECIP_ENDPOINT, 0,
  362. (__u16) USBVISION_SER_ADRS, value,
  363. len + 2, HZ);
  364. if (rc < 0)
  365. return rc;
  366. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  367. (len & 0x07) | 0x10);
  368. if (rc < 0)
  369. return rc;
  370. /* Test for Busy and ACK */
  371. do {
  372. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  373. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  374. if (rc < 0)
  375. return rc;
  376. if ((rc & 0x20) == 0) /* Ack? */
  377. break;
  378. /* I2C abort */
  379. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  380. if (--retries < 0)
  381. return -1;
  382. }
  383. if (i2c_debug & DBG_I2C) {
  384. int idx;
  385. for (idx = 0; idx < len; idx++) {
  386. PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
  387. }
  388. }
  389. return len;
  390. }
  391. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  392. short len)
  393. {
  394. char *bufPtr = buf;
  395. int retval;
  396. int wrcount = 0;
  397. int count;
  398. int maxLen = 4;
  399. while (len > 0) {
  400. count = (len > maxLen) ? maxLen : len;
  401. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  402. if (retval > 0) {
  403. len -= count;
  404. bufPtr += count;
  405. wrcount += count;
  406. } else
  407. return (retval < 0) ? retval : -EFAULT;
  408. }
  409. return wrcount;
  410. }
  411. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  412. short len)
  413. {
  414. char temp[4];
  415. int retval, i;
  416. int rdcount = 0;
  417. int count;
  418. while (len > 0) {
  419. count = (len > 3) ? 4 : len;
  420. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  421. if (retval > 0) {
  422. for (i = 0; i < len; i++)
  423. buf[rdcount + i] = temp[i];
  424. len -= count;
  425. rdcount += count;
  426. } else
  427. return (retval < 0) ? retval : -EFAULT;
  428. }
  429. return rdcount;
  430. }
  431. static struct i2c_adapter i2c_adap_template = {
  432. .owner = THIS_MODULE,
  433. .name = "usbvision",
  434. .id = I2C_HW_B_BT848, /* FIXME */
  435. .client_register = attach_inform,
  436. .client_unregister = detach_inform,
  437. .class = I2C_CLASS_TV_ANALOG,
  438. };
  439. static struct i2c_client i2c_client_template = {
  440. .name = "usbvision internal",
  441. };
  442. /*
  443. * Overrides for Emacs so that we follow Linus's tabbing style.
  444. * ---------------------------------------------------------------------------
  445. * Local variables:
  446. * c-basic-offset: 8
  447. * End:
  448. */