tm6000-i2c.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * tm6000-i2c.c - driver for TM5600/TM6000/TM6010 USB video capture devices
  3. *
  4. * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
  5. *
  6. * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
  7. * - Fix SMBus Read Byte command
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation version 2
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/usb.h>
  25. #include <linux/i2c.h>
  26. #include "tm6000.h"
  27. #include "tm6000-regs.h"
  28. #include <media/v4l2-common.h>
  29. #include <media/tuner.h>
  30. #include "tuner-xc2028.h"
  31. /* ----------------------------------------------------------- */
  32. static unsigned int i2c_debug;
  33. module_param(i2c_debug, int, 0644);
  34. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  35. #define i2c_dprintk(lvl, fmt, args...) if (i2c_debug >= lvl) do { \
  36. printk(KERN_DEBUG "%s at %s: " fmt, \
  37. dev->name, __func__, ##args); } while (0)
  38. static int tm6000_i2c_send_regs(struct tm6000_core *dev, unsigned char addr,
  39. __u8 reg, char *buf, int len)
  40. {
  41. int rc;
  42. unsigned int i2c_packet_limit = 16;
  43. if (dev->dev_type == TM6010)
  44. i2c_packet_limit = 80;
  45. if (!buf)
  46. return -1;
  47. if (len < 1 || len > i2c_packet_limit) {
  48. printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
  49. len, i2c_packet_limit);
  50. return -1;
  51. }
  52. /* capture mutex */
  53. rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
  54. USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
  55. addr | reg << 8, 0, buf, len);
  56. if (rc < 0) {
  57. /* release mutex */
  58. return rc;
  59. }
  60. /* release mutex */
  61. return rc;
  62. }
  63. /* Generic read - doesn't work fine with 16bit registers */
  64. static int tm6000_i2c_recv_regs(struct tm6000_core *dev, unsigned char addr,
  65. __u8 reg, char *buf, int len)
  66. {
  67. int rc;
  68. u8 b[2];
  69. unsigned int i2c_packet_limit = 16;
  70. if (dev->dev_type == TM6010)
  71. i2c_packet_limit = 64;
  72. if (!buf)
  73. return -1;
  74. if (len < 1 || len > i2c_packet_limit) {
  75. printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
  76. len, i2c_packet_limit);
  77. return -1;
  78. }
  79. /* capture mutex */
  80. if ((dev->caps.has_zl10353) && (dev->demod_addr << 1 == addr) && (reg % 2 == 0)) {
  81. /*
  82. * Workaround an I2C bug when reading from zl10353
  83. */
  84. reg -= 1;
  85. len += 1;
  86. rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  87. REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, b, len);
  88. *buf = b[1];
  89. } else {
  90. rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  91. REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
  92. }
  93. /* release mutex */
  94. return rc;
  95. }
  96. /*
  97. * read from a 16bit register
  98. * for example xc2028, xc3028 or xc3028L
  99. */
  100. static int tm6000_i2c_recv_regs16(struct tm6000_core *dev, unsigned char addr,
  101. __u16 reg, char *buf, int len)
  102. {
  103. int rc;
  104. unsigned char ureg;
  105. if (!buf || len != 2)
  106. return -1;
  107. /* capture mutex */
  108. if (dev->dev_type == TM6010) {
  109. ureg = reg & 0xFF;
  110. rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
  111. USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
  112. addr | (reg & 0xFF00), 0, &ureg, 1);
  113. if (rc < 0) {
  114. /* release mutex */
  115. return rc;
  116. }
  117. rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
  118. USB_RECIP_DEVICE, REQ_35_AFTEK_TUNER_READ,
  119. reg, 0, buf, len);
  120. } else {
  121. rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
  122. USB_RECIP_DEVICE, REQ_14_SET_GET_I2C_WR2_RDN,
  123. addr, reg, buf, len);
  124. }
  125. /* release mutex */
  126. return rc;
  127. }
  128. static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
  129. struct i2c_msg msgs[], int num)
  130. {
  131. struct tm6000_core *dev = i2c_adap->algo_data;
  132. int addr, rc, i, byte;
  133. if (num <= 0)
  134. return 0;
  135. for (i = 0; i < num; i++) {
  136. addr = (msgs[i].addr << 1) & 0xff;
  137. i2c_dprintk(2, "%s %s addr=0x%x len=%d:",
  138. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  139. i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
  140. if (msgs[i].flags & I2C_M_RD) {
  141. /* read request without preceding register selection */
  142. /*
  143. * The TM6000 only supports a read transaction
  144. * immediately after a 1 or 2 byte write to select
  145. * a register. We cannot fulfil this request.
  146. */
  147. i2c_dprintk(2, " read without preceding write not"
  148. " supported");
  149. rc = -EOPNOTSUPP;
  150. goto err;
  151. } else if (i + 1 < num && msgs[i].len <= 2 &&
  152. (msgs[i + 1].flags & I2C_M_RD) &&
  153. msgs[i].addr == msgs[i + 1].addr) {
  154. /* 1 or 2 byte write followed by a read */
  155. if (i2c_debug >= 2)
  156. for (byte = 0; byte < msgs[i].len; byte++)
  157. printk(KERN_CONT " %02x", msgs[i].buf[byte]);
  158. i2c_dprintk(2, "; joined to read %s len=%d:",
  159. i == num - 2 ? "stop" : "nonstop",
  160. msgs[i + 1].len);
  161. if (msgs[i].len == 2) {
  162. rc = tm6000_i2c_recv_regs16(dev, addr,
  163. msgs[i].buf[0] << 8 | msgs[i].buf[1],
  164. msgs[i + 1].buf, msgs[i + 1].len);
  165. } else {
  166. rc = tm6000_i2c_recv_regs(dev, addr, msgs[i].buf[0],
  167. msgs[i + 1].buf, msgs[i + 1].len);
  168. }
  169. i++;
  170. if (addr == dev->tuner_addr << 1) {
  171. tm6000_set_reg(dev, REQ_50_SET_START, 0, 0);
  172. tm6000_set_reg(dev, REQ_51_SET_STOP, 0, 0);
  173. }
  174. if (i2c_debug >= 2)
  175. for (byte = 0; byte < msgs[i].len; byte++)
  176. printk(KERN_CONT " %02x", msgs[i].buf[byte]);
  177. } else {
  178. /* write bytes */
  179. if (i2c_debug >= 2)
  180. for (byte = 0; byte < msgs[i].len; byte++)
  181. printk(KERN_CONT " %02x", msgs[i].buf[byte]);
  182. rc = tm6000_i2c_send_regs(dev, addr, msgs[i].buf[0],
  183. msgs[i].buf + 1, msgs[i].len - 1);
  184. }
  185. if (i2c_debug >= 2)
  186. printk(KERN_CONT "\n");
  187. if (rc < 0)
  188. goto err;
  189. }
  190. return num;
  191. err:
  192. i2c_dprintk(2, " ERROR: %i\n", rc);
  193. return rc;
  194. }
  195. static int tm6000_i2c_eeprom(struct tm6000_core *dev)
  196. {
  197. int i, rc;
  198. unsigned char *p = dev->eedata;
  199. unsigned char bytes[17];
  200. dev->i2c_client.addr = 0xa0 >> 1;
  201. dev->eedata_size = 0;
  202. bytes[16] = '\0';
  203. for (i = 0; i < sizeof(dev->eedata); ) {
  204. *p = i;
  205. rc = tm6000_i2c_recv_regs(dev, 0xa0, i, p, 1);
  206. if (rc < 1) {
  207. if (p == dev->eedata)
  208. goto noeeprom;
  209. else {
  210. printk(KERN_WARNING
  211. "%s: i2c eeprom read error (err=%d)\n",
  212. dev->name, rc);
  213. }
  214. return -EINVAL;
  215. }
  216. dev->eedata_size++;
  217. p++;
  218. if (0 == (i % 16))
  219. printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
  220. printk(KERN_CONT " %02x", dev->eedata[i]);
  221. if ((dev->eedata[i] >= ' ') && (dev->eedata[i] <= 'z'))
  222. bytes[i%16] = dev->eedata[i];
  223. else
  224. bytes[i%16] = '.';
  225. i++;
  226. if (0 == (i % 16)) {
  227. bytes[16] = '\0';
  228. printk(KERN_CONT " %s\n", bytes);
  229. }
  230. }
  231. if (0 != (i%16)) {
  232. bytes[i%16] = '\0';
  233. for (i %= 16; i < 16; i++)
  234. printk(KERN_CONT " ");
  235. printk(KERN_CONT " %s\n", bytes);
  236. }
  237. return 0;
  238. noeeprom:
  239. printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
  240. dev->name, rc);
  241. return -EINVAL;
  242. }
  243. /* ----------------------------------------------------------- */
  244. /*
  245. * functionality()
  246. */
  247. static u32 functionality(struct i2c_adapter *adap)
  248. {
  249. return I2C_FUNC_SMBUS_EMUL;
  250. }
  251. static const struct i2c_algorithm tm6000_algo = {
  252. .master_xfer = tm6000_i2c_xfer,
  253. .functionality = functionality,
  254. };
  255. /* ----------------------------------------------------------- */
  256. /*
  257. * tm6000_i2c_register()
  258. * register i2c bus
  259. */
  260. int tm6000_i2c_register(struct tm6000_core *dev)
  261. {
  262. int rc;
  263. dev->i2c_adap.owner = THIS_MODULE;
  264. dev->i2c_adap.algo = &tm6000_algo;
  265. dev->i2c_adap.dev.parent = &dev->udev->dev;
  266. strlcpy(dev->i2c_adap.name, dev->name, sizeof(dev->i2c_adap.name));
  267. dev->i2c_adap.algo_data = dev;
  268. i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
  269. rc = i2c_add_adapter(&dev->i2c_adap);
  270. if (rc)
  271. return rc;
  272. dev->i2c_client.adapter = &dev->i2c_adap;
  273. strlcpy(dev->i2c_client.name, "tm6000 internal", I2C_NAME_SIZE);
  274. tm6000_i2c_eeprom(dev);
  275. return 0;
  276. }
  277. /*
  278. * tm6000_i2c_unregister()
  279. * unregister i2c_bus
  280. */
  281. int tm6000_i2c_unregister(struct tm6000_core *dev)
  282. {
  283. i2c_del_adapter(&dev->i2c_adap);
  284. return 0;
  285. }