tm6000-i2c.c 7.6 KB

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