tuner-xc2028.c 12 KB

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