tuner-xc2028.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* tuner-xc2028
  2. *
  3. * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
  4. * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
  5. * - frontend interface
  6. * This code is placed under the terms of the GNU General Public License v2
  7. */
  8. #include <linux/i2c.h>
  9. #include <asm/div64.h>
  10. #include <linux/firmware.h>
  11. #include <linux/videodev.h>
  12. #include <linux/delay.h>
  13. #include <media/tuner.h>
  14. #include "tuner-driver.h"
  15. #include "tuner-xc2028.h"
  16. #include <linux/dvb/frontend.h>
  17. #include "dvb_frontend.h"
  18. /* digital TV standards */
  19. #define V4L2_STD_DTV_6MHZ ((v4l2_std_id)0x04000000)
  20. #define V4L2_STD_DTV_7MHZ ((v4l2_std_id)0x08000000)
  21. #define V4L2_STD_DTV_8MHZ ((v4l2_std_id)0x10000000)
  22. /* Firmwares used on tm5600/tm6000 + xc2028/xc3028 */
  23. /* Generic firmwares */
  24. static const char *firmware_INIT0 = "tm_xc3028_MTS_init0.fw";
  25. static const char *firmware_8MHZ_INIT0 = "tm_xc3028_8M_MTS_init0.fw";
  26. static const char *firmware_INIT1 = "tm_xc3028_68M_MTS_init1.fw";
  27. /* Standard-specific firmwares */
  28. static const char *firmware_6M = "tm_xc3028_DTV_6M.fw";
  29. static const char *firmware_7M = "tm_xc3028_7M.fw";
  30. static const char *firmware_8M = "tm_xc3028_8M.fw";
  31. static const char *firmware_B = "tm_xc3028_B_PAL.fw";
  32. static const char *firmware_DK = "tm_xc3028_DK_PAL_MTS.fw";
  33. static const char *firmware_MN = "tm_xc3028_MN_BTSC.fw";
  34. struct xc2028_data {
  35. v4l2_std_id firm_type; /* video stds supported
  36. by current firmware */
  37. fe_bandwidth_t bandwidth; /* Firmware bandwidth:
  38. 6M, 7M or 8M */
  39. int need_load_generic; /* The generic firmware
  40. were loaded? */
  41. enum tuner_mode mode;
  42. struct i2c_client *i2c_client;
  43. };
  44. #define i2c_send(rc,c,buf,size) \
  45. if (size != (rc = i2c_master_send(c, buf, size))) \
  46. tuner_warn("i2c output error: rc = %d (should be %d)\n", \
  47. rc, (int)size);
  48. #define i2c_rcv(rc,c,buf,size) \
  49. if (size != (rc = i2c_master_recv(c, buf, size))) \
  50. tuner_warn("i2c input error: rc = %d (should be %d)\n", \
  51. rc, (int)size);
  52. #define send_seq(c, data...) \
  53. { int rc; \
  54. const static u8 _val[] = data; \
  55. if (sizeof(_val) != \
  56. (rc = i2c_master_send \
  57. (c, _val, sizeof(_val)))) { \
  58. printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
  59. return; \
  60. } \
  61. msleep (10); \
  62. }
  63. static int xc2028_get_reg(struct i2c_client *c, u16 reg)
  64. {
  65. int rc;
  66. unsigned char buf[1];
  67. struct tuner *t = i2c_get_clientdata(c);
  68. buf[0]= reg;
  69. i2c_send(rc, c, buf, sizeof(buf));
  70. if (rc<0)
  71. return rc;
  72. i2c_rcv(rc, c, buf, 2);
  73. if (rc<0)
  74. return rc;
  75. return (buf[1])|(buf[0]<<8);
  76. }
  77. static int load_firmware (struct i2c_client *c, const char *name)
  78. {
  79. const struct firmware *fw=NULL;
  80. struct tuner *t = i2c_get_clientdata(c);
  81. unsigned char *p, *endp;
  82. int len=0, rc=0;
  83. static const char firmware_ver[] = "tm6000/xcv v1";
  84. tuner_info("xc2028: Loading firmware %s\n", name);
  85. rc = request_firmware(&fw, name, &c->dev);
  86. if (rc < 0) {
  87. if (rc==-ENOENT)
  88. tuner_info("Error: firmware %s not found.\n", name);
  89. else
  90. tuner_info("Error %d while requesting firmware %s \n", rc, name);
  91. return rc;
  92. }
  93. p=fw->data;
  94. endp=p+fw->size;
  95. if(fw->size==0) {
  96. tuner_info("Error: firmware size is zero!\n");
  97. rc=-EINVAL;
  98. goto err;
  99. }
  100. if (fw->size<sizeof(firmware_ver)-1) {
  101. /* Firmware is incorrect */
  102. tuner_info("Error: firmware size is less than header (%d<%d)!\n",
  103. (int)fw->size,(int)sizeof(firmware_ver)-1);
  104. rc=-EINVAL;
  105. goto err;
  106. }
  107. if (memcmp(p,firmware_ver,sizeof(firmware_ver)-1)) {
  108. /* Firmware is incorrect */
  109. tuner_info("Error: firmware is not for tm5600/6000 + Xcv2028/3028!\n");
  110. rc=-EINVAL;
  111. goto err;
  112. }
  113. p+=sizeof(firmware_ver)-1;
  114. while(p<endp) {
  115. if ((*p) & 0x80) {
  116. /* Special callback command received */
  117. rc = t->tuner_callback(c->adapter->algo_data,
  118. XC2028_TUNER_RESET, (*p)&0x7f);
  119. if (rc<0) {
  120. tuner_info("Error at RESET code %d\n",
  121. (*p)&0x7f);
  122. goto err;
  123. }
  124. p++;
  125. continue;
  126. }
  127. len=*p;
  128. p++;
  129. if (p+len+1>endp) {
  130. /* Firmware is incorrect */
  131. tuner_info("Error: firmware is truncated!\n");
  132. rc=-EINVAL;
  133. goto err;
  134. }
  135. if (len<=0) {
  136. tuner_info("Error: firmware file is corrupted!\n");
  137. rc=-EINVAL;
  138. goto err;
  139. }
  140. i2c_send(rc, c, p, len);
  141. if (rc<0)
  142. goto err;
  143. p+=len;
  144. if (*p)
  145. msleep(*p);
  146. p++;
  147. }
  148. err:
  149. release_firmware(fw);
  150. return rc;
  151. }
  152. static int check_firmware(struct i2c_client *c, enum tuner_mode new_mode,
  153. fe_bandwidth_t bandwidth)
  154. {
  155. int rc, version;
  156. struct tuner *t = i2c_get_clientdata(c);
  157. struct xc2028_data *xc2028 = t->priv;
  158. const char *name;
  159. int change_digital_bandwidth;
  160. if (!t->tuner_callback) {
  161. printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
  162. return -EINVAL;
  163. }
  164. printk(KERN_INFO "xc2028: I am in mode %u and I should switch to mode %i\n",
  165. xc2028->mode, new_mode);
  166. /* first of all, determine whether we have switched the mode */
  167. if(new_mode != xc2028->mode) {
  168. xc2028->mode = new_mode;
  169. xc2028->need_load_generic = 1;
  170. }
  171. change_digital_bandwidth = (xc2028->mode == T_DIGITAL_TV
  172. && bandwidth != xc2028->bandwidth) ? 1 : 0;
  173. tuner_info("xc2028: old bandwidth %u, new bandwidth %u\n", xc2028->bandwidth,
  174. bandwidth);
  175. if (xc2028->need_load_generic) {
  176. if (xc2028->bandwidth==8)
  177. name = firmware_8MHZ_INIT0;
  178. else
  179. name = firmware_INIT0;
  180. /* Reset is needed before loading firmware */
  181. rc = t->tuner_callback(c->adapter->algo_data,
  182. XC2028_TUNER_RESET, 0);
  183. if (rc<0)
  184. return rc;
  185. rc = load_firmware(c,name);
  186. if (rc<0)
  187. return rc;
  188. xc2028->need_load_generic=0;
  189. xc2028->firm_type=0;
  190. if(xc2028->mode == T_DIGITAL_TV) {
  191. change_digital_bandwidth=1;
  192. }
  193. }
  194. tuner_info("xc2028: I should change bandwidth %u\n",
  195. change_digital_bandwidth);
  196. if (change_digital_bandwidth) {
  197. switch(bandwidth) {
  198. case BANDWIDTH_8_MHZ:
  199. t->std = V4L2_STD_DTV_8MHZ;
  200. break;
  201. case BANDWIDTH_7_MHZ:
  202. t->std = V4L2_STD_DTV_7MHZ;
  203. break;
  204. case BANDWIDTH_6_MHZ:
  205. t->std = V4L2_STD_DTV_6MHZ;
  206. break;
  207. default:
  208. tuner_info("error: bandwidth not supported.\n");
  209. };
  210. xc2028->bandwidth = bandwidth;
  211. }
  212. if (xc2028->firm_type & t->std) {
  213. tuner_info("xc3028: no need to load a std-specific firmware.\n");
  214. return 0;
  215. }
  216. rc = load_firmware(c,firmware_INIT1);
  217. if (t->std & V4L2_STD_MN)
  218. name=firmware_MN;
  219. else if (t->std & V4L2_STD_DTV_6MHZ)
  220. name=firmware_6M;
  221. else if (t->std & V4L2_STD_DTV_7MHZ)
  222. name=firmware_7M;
  223. else if (t->std & V4L2_STD_DTV_8MHZ)
  224. name=firmware_8M;
  225. else if (t->std & V4L2_STD_PAL_B)
  226. name=firmware_B;
  227. else
  228. name=firmware_DK;
  229. tuner_info("xc2028: loading firmware named %s.\n", name);
  230. rc = load_firmware(c, name);
  231. if (rc<0)
  232. return rc;
  233. version = xc2028_get_reg(c, 0x4);
  234. tuner_info("Firmware version is %d.%d\n",
  235. (version>>4)&0x0f,(version)&0x0f);
  236. xc2028->firm_type=t->std;
  237. return 0;
  238. }
  239. static int xc2028_signal(struct i2c_client *c)
  240. {
  241. int lock, signal;
  242. printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
  243. lock = xc2028_get_reg(c, 0x2);
  244. if (lock<=0)
  245. return lock;
  246. /* Frequency is locked. Return signal quality */
  247. signal = xc2028_get_reg(c, 0x40);
  248. if(signal<=0)
  249. return lock;
  250. return signal;
  251. }
  252. #define DIV 15625
  253. static void generic_set_tv_freq(struct i2c_client *c, u32 freq /* in Hz */,
  254. enum tuner_mode new_mode, fe_bandwidth_t bandwidth)
  255. {
  256. int rc;
  257. unsigned char buf[5];
  258. struct tuner *t = i2c_get_clientdata(c);
  259. u32 div, offset = 0;
  260. /* HACK: It seems that specific firmware need to be reloaded
  261. when freq is changed */
  262. struct xc2028_data *xc2028 = t->priv;
  263. xc2028->firm_type=0;
  264. /* Reset GPIO 1 */
  265. if (t->tuner_callback) {
  266. rc = t->tuner_callback( c->adapter->algo_data,
  267. XC2028_TUNER_RESET, 0);
  268. if (rc<0)
  269. return;
  270. }
  271. msleep(10);
  272. printk("xc3028: should set frequency %d kHz)\n", freq / 1000);
  273. if (check_firmware(c, new_mode, bandwidth)<0)
  274. return;
  275. if(new_mode == T_DIGITAL_TV)
  276. offset = 2750000;
  277. div = (freq - offset + DIV/2)/DIV;
  278. /* CMD= Set frequency */
  279. send_seq(c, {0x00, 0x02, 0x00, 0x00});
  280. if (t->tuner_callback) {
  281. rc = t->tuner_callback( c->adapter->algo_data,
  282. XC2028_RESET_CLK, 1);
  283. if (rc<0)
  284. return;
  285. }
  286. msleep(10);
  287. buf[0]= 0xff & (div>>24);
  288. buf[1]= 0xff & (div>>16);
  289. buf[2]= 0xff & (div>>8);
  290. buf[3]= 0xff & (div);
  291. buf[4]= 0;
  292. i2c_send(rc, c, buf, sizeof(buf));
  293. if (rc<0)
  294. return;
  295. msleep(100);
  296. printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
  297. buf[1],buf[2],buf[3],buf[4],
  298. freq / 16, freq % 16 * 100 / 16);
  299. }
  300. static void set_tv_freq(struct i2c_client *c, unsigned int freq)
  301. {
  302. printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
  303. generic_set_tv_freq(c, freq * 62500l, T_ANALOG_TV,
  304. BANDWIDTH_8_MHZ /* unimportant */);
  305. }
  306. static void xc2028_release(struct i2c_client *c)
  307. {
  308. struct tuner *t = i2c_get_clientdata(c);
  309. kfree(t->priv);
  310. t->priv = NULL;
  311. }
  312. static struct tuner_operations tea5767_tuner_ops = {
  313. .set_tv_freq = set_tv_freq,
  314. .has_signal = xc2028_signal,
  315. .release = xc2028_release,
  316. // .is_stereo = xc2028_stereo,
  317. };
  318. static int init=0;
  319. int xc2028_tuner_init(struct i2c_client *c)
  320. {
  321. struct tuner *t = i2c_get_clientdata(c);
  322. int version = xc2028_get_reg(c, 0x4);
  323. int prd_id = xc2028_get_reg(c, 0x8);
  324. struct xc2028_data *xc2028;
  325. tuner_info("Xcv2028/3028 init called!\n");
  326. if (init) {
  327. printk (KERN_ERR "Module already initialized!\n");
  328. return 0;
  329. }
  330. init++;
  331. xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
  332. if (!xc2028)
  333. return -ENOMEM;
  334. t->priv = xc2028;
  335. xc2028->bandwidth=BANDWIDTH_6_MHZ;
  336. xc2028->need_load_generic=1;
  337. xc2028->mode = T_UNINITIALIZED;
  338. /* FIXME: Check where t->priv will be freed */
  339. if (version<0)
  340. version=0;
  341. if (prd_id<0)
  342. prd_id=0;
  343. strlcpy(c->name, "xc2028", sizeof(c->name));
  344. tuner_info("type set to %d (%s, hw ver=%d.%d, fw ver=%d.%d, id=0x%04x)\n",
  345. t->type, c->name,
  346. (version>>12)&0x0f,(version>>8)&0x0f,
  347. (version>>4)&0x0f,(version)&0x0f, prd_id);
  348. memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));
  349. return 0;
  350. }
  351. static int xc3028_set_params(struct dvb_frontend *fe,
  352. struct dvb_frontend_parameters *p)
  353. {
  354. struct i2c_client *c = fe->tuner_priv;
  355. printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
  356. generic_set_tv_freq(c, p->frequency, T_DIGITAL_TV,
  357. p->u.ofdm.bandwidth);
  358. return 0;
  359. }
  360. static int xc3028_dvb_release(struct dvb_frontend *fe)
  361. {
  362. printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
  363. fe->tuner_priv = NULL;
  364. return 0;
  365. }
  366. static int xc3028_dvb_init(struct dvb_frontend *fe)
  367. {
  368. printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
  369. return 0;
  370. }
  371. static const struct dvb_tuner_ops xc3028_dvb_tuner_ops = {
  372. .info = {
  373. .name = "Xceive XC3028",
  374. .frequency_min = 42000000,
  375. .frequency_max = 864000000,
  376. .frequency_step = 50000,
  377. },
  378. .release = xc3028_dvb_release,
  379. .init = xc3028_dvb_init,
  380. // int (*sleep)(struct dvb_frontend *fe);
  381. /** This is for simple PLLs - set all parameters in one go. */
  382. .set_params = xc3028_set_params,
  383. /** This is support for demods like the mt352 - fills out the supplied buffer with what to write. */
  384. // int (*calc_regs)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p, u8 *buf, int buf_len);
  385. // int (*get_frequency)(struct dvb_frontend *fe, u32 *frequency);
  386. // int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth);
  387. // int (*get_status)(struct dvb_frontend *fe, u32 *status);
  388. };
  389. int xc2028_attach(struct i2c_client *c, struct dvb_frontend *fe)
  390. {
  391. fe->tuner_priv = c;
  392. memcpy(&fe->ops.tuner_ops, &xc3028_dvb_tuner_ops, sizeof(fe->ops.tuner_ops));
  393. return 0;
  394. }
  395. EXPORT_SYMBOL(xc2028_attach);