tuner-xc2028.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* tuner-xc2028
  2. *
  3. * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
  4. * This code is placed under the terms of the GNU General Public License v2
  5. */
  6. #include <linux/i2c.h>
  7. #include <asm/div64.h>
  8. #include <linux/firmware.h>
  9. #include <linux/videodev.h>
  10. #include <linux/delay.h>
  11. #include "tuner-driver.h"
  12. #include "tuner-xc2028.h"
  13. /* Firmwares used on tm5600/tm6000 + xc2028/xc3028 */
  14. static const char *firmware_6M = "tm6000_xc3028_DTV_6M.fw";
  15. static const char *firmware_8M = "tm6000_xc3028_78M.fw";
  16. static const char *firmware_DK = "tm6000_xc3028_DK_PAL_MTS.fw";
  17. static const char *firmware_MN = "tm6000_xc3028_MN_BTSC.fw";
  18. struct xc2028_data {
  19. v4l2_std_id firm_type; /* video stds supported by current firmware */
  20. int bandwidth; /* Firmware bandwidth: 6M, 7M or 8M */
  21. int need_load_generic; /* The generic firmware were loaded? */
  22. };
  23. #define i2c_send(rc,c,buf,size) \
  24. if (size != (rc = i2c_master_send(c, buf, size))) \
  25. tuner_warn("i2c output error: rc = %d (should be %d)\n", \
  26. rc, (int)size);
  27. #define i2c_rcv(rc,c,buf,size) \
  28. if (size != (rc = i2c_master_recv(c, buf, size))) \
  29. tuner_warn("i2c input error: rc = %d (should be %d)\n", \
  30. rc, (int)size);
  31. #define send_seq(c, data...) \
  32. { int rc; \
  33. const static u8 _val[] = data; \
  34. if (sizeof(_val) != \
  35. (rc = i2c_master_send \
  36. (c, _val, sizeof(_val)))) { \
  37. printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
  38. return; \
  39. } \
  40. msleep (10); \
  41. }
  42. static int xc2028_get_reg(struct i2c_client *c, u16 reg)
  43. {
  44. int rc;
  45. unsigned char buf[1];
  46. struct tuner *t = i2c_get_clientdata(c);
  47. buf[0]= reg;
  48. i2c_send(rc, c, buf, sizeof(buf));
  49. if (rc<0)
  50. return rc;
  51. if (t->tuner_callback) {
  52. rc = t->tuner_callback( c->adapter->algo_data,
  53. XC2028_RESET_CLK, 0);
  54. if (rc<0)
  55. return rc;
  56. }
  57. i2c_rcv(rc, c, buf, 2);
  58. if (rc<0)
  59. return rc;
  60. return (buf[1])|(buf[0]<<8);
  61. }
  62. static int load_firmware (struct i2c_client *c, const char *name)
  63. {
  64. const struct firmware *fw=NULL;
  65. struct tuner *t = i2c_get_clientdata(c);
  66. unsigned char *p, *endp;
  67. int len=0, rc=0;
  68. static const char firmware_ver[] = "tm6000/xcv v1";
  69. tuner_info("Loading firmware %s\n", name);
  70. rc = request_firmware(&fw, name, &c->dev);
  71. if (rc < 0) {
  72. tuner_info("Error %d while requesting firmware\n", rc);
  73. return rc;
  74. }
  75. p=fw->data;
  76. endp=p+fw->size;
  77. if(fw->size==0) {
  78. tuner_info("Error: firmware size is zero!\n");
  79. rc=-EINVAL;
  80. goto err;
  81. }
  82. if (fw->size<sizeof(firmware_ver)-1) {
  83. /* Firmware is incorrect */
  84. tuner_info("Error: firmware size is less than header (%d<%d)!\n",
  85. (int)fw->size,(int)sizeof(firmware_ver)-1);
  86. rc=-EINVAL;
  87. goto err;
  88. }
  89. if (memcmp(p,firmware_ver,sizeof(firmware_ver)-1)) {
  90. /* Firmware is incorrect */
  91. tuner_info("Error: firmware is not for tm5600/6000 + Xcv2028/3028!\n");
  92. rc=-EINVAL;
  93. goto err;
  94. }
  95. p+=sizeof(firmware_ver)-1;
  96. while(p<endp) {
  97. if ((*p) & 0x80) {
  98. /* Special callback command received */
  99. rc = t->tuner_callback(c->adapter->algo_data,
  100. XC2028_TUNER_RESET, (*p)&0x7f);
  101. if (rc<0) {
  102. tuner_info("Error at RESET code %d\n",
  103. (*p)&0x7f);
  104. goto err;
  105. }
  106. p++;
  107. continue;
  108. }
  109. len=*p;
  110. p++;
  111. if (p+len+1>endp) {
  112. /* Firmware is incorrect */
  113. tuner_info("Error: firmware is truncated!\n");
  114. rc=-EINVAL;
  115. goto err;
  116. }
  117. if (len<=0) {
  118. tuner_info("Error: firmware file is corrupted!\n");
  119. rc=-EINVAL;
  120. goto err;
  121. }
  122. i2c_send(rc, c, p, len);
  123. if (rc<0)
  124. goto err;
  125. p+=len;
  126. if (*p)
  127. msleep(*p);
  128. p++;
  129. }
  130. err:
  131. release_firmware(fw);
  132. return rc;
  133. }
  134. static int check_firmware(struct i2c_client *c)
  135. {
  136. int rc, version;
  137. struct tuner *t = i2c_get_clientdata(c);
  138. struct xc2028_data *xc2028 = t->priv;
  139. const char *name;
  140. if (!t->tuner_callback) {
  141. printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
  142. return -EINVAL;
  143. }
  144. if (xc2028->need_load_generic) {
  145. if (xc2028->bandwidth==6)
  146. name = firmware_6M;
  147. else
  148. name = firmware_8M;
  149. /* Reset is needed before loading firmware */
  150. rc = t->tuner_callback(c->adapter->algo_data,
  151. XC2028_TUNER_RESET, 0);
  152. if (rc<0)
  153. return rc;
  154. rc = load_firmware(c,name);
  155. if (rc<0)
  156. return rc;
  157. xc2028->need_load_generic=0;
  158. xc2028->firm_type=0;
  159. }
  160. if (xc2028->firm_type & t->std)
  161. return 0;
  162. send_seq (c, {0x12, 0x39});
  163. send_seq (c, {0x0c, 0x80, 0xf0, 0xf7, 0x3e, 0x75, 0xc1, 0x8a, 0xe4});
  164. send_seq (c, {0x0c, 0x02, 0x00});
  165. send_seq (c, {0x05, 0x0f, 0xee, 0xaa, 0x5f, 0xea, 0x90});
  166. send_seq (c, {0x06, 0x00, 0x0a, 0x4d, 0x8c, 0xf2, 0xd8, 0xcf, 0x30});
  167. send_seq (c, {0x06, 0x79, 0x9f});
  168. send_seq (c, {0x0b, 0x0d, 0xa4, 0x6c});
  169. send_seq (c, {0x0a, 0x01, 0x67, 0x24, 0x40, 0x08, 0xc3, 0x20, 0x10});
  170. send_seq (c, {0x0a, 0x64, 0x3c, 0xfa, 0xf7, 0xe1, 0x0c, 0x2c});
  171. send_seq (c, {0x09, 0x0b});
  172. send_seq (c, {0x10, 0x13});
  173. send_seq (c, {0x16, 0x12});
  174. send_seq (c, {0x1f, 0x02});
  175. send_seq (c, {0x21, 0x02});
  176. send_seq (c, {0x01, 0x02});
  177. send_seq (c, {0x2b, 0x10});
  178. send_seq (c, {0x02, 0x02});
  179. send_seq (c, {0x02, 0x03});
  180. send_seq (c, {0x00, 0x8c});
  181. if (t->std & V4L2_STD_MN)
  182. name=firmware_MN;
  183. else
  184. name=firmware_DK;
  185. rc = load_firmware(c,name);
  186. if (rc<0)
  187. return rc;
  188. version = xc2028_get_reg(c, 0x4);
  189. tuner_info("Firmware version is %d.%d\n",
  190. (version>>4)&0x0f,(version)&0x0f);
  191. xc2028->firm_type=t->std;
  192. return 0;
  193. }
  194. static int xc2028_signal(struct i2c_client *c)
  195. {
  196. int lock, signal;
  197. if (check_firmware(c)<0)
  198. return 0;
  199. lock = xc2028_get_reg(c, 0x2);
  200. if (lock<=0)
  201. return lock;
  202. /* Frequency is locked. Return signal quality */
  203. signal = xc2028_get_reg(c, 0x40);
  204. if(signal<=0)
  205. return lock;
  206. return signal;
  207. }
  208. #define DIV 15625
  209. static void set_tv_freq(struct i2c_client *c, unsigned int freq)
  210. {
  211. int rc;
  212. unsigned char buf[5];
  213. struct tuner *t = i2c_get_clientdata(c);
  214. unsigned long div = (freq*62500l+DIV/2)/DIV;
  215. if (check_firmware(c)<0)
  216. return;
  217. /* Reset GPIO 1 */
  218. if (t->tuner_callback) {
  219. rc = t->tuner_callback( c->adapter->algo_data,
  220. XC2028_TUNER_RESET, 0);
  221. if (rc<0)
  222. return;
  223. }
  224. msleep(10);
  225. /* CMD= Set frequency */
  226. send_seq(c, {0x00, 0x02, 0x00, 0x00});
  227. if (t->tuner_callback) {
  228. rc = t->tuner_callback( c->adapter->algo_data,
  229. XC2028_RESET_CLK, 1);
  230. if (rc<0)
  231. return;
  232. }
  233. msleep(10);
  234. // send_seq(c, {0x00, 0x00, 0x10, 0xd0, 0x00});
  235. // msleep(100);
  236. buf[0]= 0xff & (div>>24);
  237. buf[1]= 0xff & (div>>16);
  238. buf[2]= 0xff & (div>>8);
  239. buf[3]= 0xff & (div);
  240. buf[4]= 0;
  241. i2c_send(rc, c, buf, sizeof(buf));
  242. if (rc<0)
  243. return;
  244. msleep(100);
  245. printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
  246. buf[1],buf[2],buf[3],buf[4],
  247. freq / 16, freq % 16 * 100 / 16);
  248. // printk("signal=%d\n",xc2028_signal(c));
  249. }
  250. static void xc2028_release(struct i2c_client *c)
  251. {
  252. struct tuner *t = i2c_get_clientdata(c);
  253. kfree(t->priv);
  254. t->priv = NULL;
  255. }
  256. static struct tuner_operations tea5767_tuner_ops = {
  257. .set_tv_freq = set_tv_freq,
  258. .has_signal = xc2028_signal,
  259. .release = xc2028_release,
  260. // .is_stereo = xc2028_stereo,
  261. };
  262. static int init=0;
  263. int xc2028_tuner_init(struct i2c_client *c)
  264. {
  265. struct tuner *t = i2c_get_clientdata(c);
  266. int version = xc2028_get_reg(c, 0x4);
  267. int prd_id = xc2028_get_reg(c, 0x8);
  268. struct xc2028_data *xc2028;
  269. if (init) {
  270. printk (KERN_ERR "Module already initialized!\n");
  271. return 0;
  272. }
  273. init++;
  274. xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
  275. if (!xc2028)
  276. return -ENOMEM;
  277. t->priv = xc2028;
  278. #ifdef HACK
  279. xc2028->firm_type=1;
  280. xc2028->bandwidth=6;
  281. #endif
  282. xc2028->bandwidth=6;
  283. xc2028->need_load_generic=1;
  284. /* FIXME: Check where t->priv will be freed */
  285. if (version<0)
  286. version=0;
  287. if (prd_id<0)
  288. prd_id=0;
  289. strlcpy(c->name, "xc2028", sizeof(c->name));
  290. tuner_info("type set to %d (%s, hw ver=%d.%d, fw ver=%d.%d, id=0x%04x)\n",
  291. t->type, c->name,
  292. (version>>12)&0x0f,(version>>8)&0x0f,
  293. (version>>4)&0x0f,(version)&0x0f, prd_id);
  294. memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));
  295. return 0;
  296. }