xc5000.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * Driver for Xceive XC5000 "QAM/8VSB single chip tuner"
  3. *
  4. * Copyright (c) 2007 Xceive Corporation
  5. * Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. *
  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/moduleparam.h>
  24. #include <linux/delay.h>
  25. #include <linux/dvb/frontend.h>
  26. #include <linux/i2c.h>
  27. #include "dvb_frontend.h"
  28. #include "xc5000.h"
  29. #include "xc5000_priv.h"
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  33. #define dprintk(level,fmt, arg...) if (debug >= level) \
  34. printk(KERN_INFO "%s: " fmt, "xc5000", ## arg)
  35. #define XC5000_DEFAULT_FIRMWARE "dvb-fe-xc5000-1.1.fw"
  36. #define XC5000_DEFAULT_FIRMWARE_SIZE 12400
  37. /* Misc Defines */
  38. #define MAX_TV_STANDARD 23
  39. #define XC_MAX_I2C_WRITE_LENGTH 64
  40. /* Signal Types */
  41. #define XC_RF_MODE_AIR 0
  42. #define XC_RF_MODE_CABLE 1
  43. /* Result codes */
  44. #define XC_RESULT_SUCCESS 0
  45. #define XC_RESULT_RESET_FAILURE 1
  46. #define XC_RESULT_I2C_WRITE_FAILURE 2
  47. #define XC_RESULT_I2C_READ_FAILURE 3
  48. #define XC_RESULT_OUT_OF_RANGE 5
  49. /* Registers */
  50. #define XREG_INIT 0x00
  51. #define XREG_VIDEO_MODE 0x01
  52. #define XREG_AUDIO_MODE 0x02
  53. #define XREG_RF_FREQ 0x03
  54. #define XREG_D_CODE 0x04
  55. #define XREG_IF_OUT 0x05
  56. #define XREG_SEEK_MODE 0x07
  57. #define XREG_POWER_DOWN 0x0A
  58. #define XREG_SIGNALSOURCE 0x0D /* 0=Air, 1=Cable */
  59. #define XREG_SMOOTHEDCVBS 0x0E
  60. #define XREG_XTALFREQ 0x0F
  61. #define XREG_FINERFFREQ 0x10
  62. #define XREG_DDIMODE 0x11
  63. #define XREG_ADC_ENV 0x00
  64. #define XREG_QUALITY 0x01
  65. #define XREG_FRAME_LINES 0x02
  66. #define XREG_HSYNC_FREQ 0x03
  67. #define XREG_LOCK 0x04
  68. #define XREG_FREQ_ERROR 0x05
  69. #define XREG_SNR 0x06
  70. #define XREG_VERSION 0x07
  71. #define XREG_PRODUCT_ID 0x08
  72. #define XREG_BUSY 0x09
  73. /*
  74. Basic firmware description. This will remain with
  75. the driver for documentation purposes.
  76. This represents an I2C firmware file encoded as a
  77. string of unsigned char. Format is as follows:
  78. char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
  79. char[1 ]=len0_LSB -> length of first write transaction
  80. char[2 ]=data0 -> first byte to be sent
  81. char[3 ]=data1
  82. char[4 ]=data2
  83. char[ ]=...
  84. char[M ]=dataN -> last byte to be sent
  85. char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
  86. char[M+2]=len1_LSB -> length of second write transaction
  87. char[M+3]=data0
  88. char[M+4]=data1
  89. ...
  90. etc.
  91. The [len] value should be interpreted as follows:
  92. len= len_MSB _ len_LSB
  93. len=1111_1111_1111_1111 : End of I2C_SEQUENCE
  94. len=0000_0000_0000_0000 : Reset command: Do hardware reset
  95. len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
  96. len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
  97. For the RESET and WAIT commands, the two following bytes will contain
  98. immediately the length of the following transaction.
  99. */
  100. typedef struct {
  101. char *Name;
  102. u16 AudioMode;
  103. u16 VideoMode;
  104. } XC_TV_STANDARD;
  105. /* Tuner standards */
  106. #define DTV6 17
  107. XC_TV_STANDARD XC5000_Standard[MAX_TV_STANDARD] = {
  108. {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
  109. {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
  110. {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
  111. {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
  112. {"B/G-PAL-A2", 0x0A00, 0x8049},
  113. {"B/G-PAL-NICAM", 0x0C04, 0x8049},
  114. {"B/G-PAL-MONO", 0x0878, 0x8059},
  115. {"I-PAL-NICAM", 0x1080, 0x8009},
  116. {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
  117. {"D/K-PAL-A2", 0x1600, 0x8009},
  118. {"D/K-PAL-NICAM", 0x0E80, 0x8009},
  119. {"D/K-PAL-MONO", 0x1478, 0x8009},
  120. {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
  121. {"D/K-SECAM-A2 L/DK3",0x0E00, 0x8009},
  122. {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
  123. {"L-SECAM-NICAM", 0x8E82, 0x0009},
  124. {"L'-SECAM-NICAM", 0x8E82, 0x4009},
  125. {"DTV6", 0x00C0, 0x8002},
  126. {"DTV8", 0x00C0, 0x800B},
  127. {"DTV7/8", 0x00C0, 0x801B},
  128. {"DTV7", 0x00C0, 0x8007},
  129. {"FM Radio-INPUT2", 0x9802, 0x9002},
  130. {"FM Radio-INPUT1", 0x0208, 0x9002}
  131. };
  132. static int xc5000_writeregs(struct xc5000_priv *priv, u8 *buf, u8 len);
  133. static int xc5000_readregs(struct xc5000_priv *priv, u8 *buf, u8 len);
  134. static void xc5000_TunerReset(struct dvb_frontend *fe);
  135. static int xc_send_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
  136. {
  137. return xc5000_writeregs(priv, buf, len)
  138. ? XC_RESULT_I2C_WRITE_FAILURE : XC_RESULT_SUCCESS;
  139. }
  140. static int xc_read_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
  141. {
  142. return xc5000_readregs(priv, buf, len)
  143. ? XC_RESULT_I2C_READ_FAILURE : XC_RESULT_SUCCESS;
  144. }
  145. static int xc_reset(struct dvb_frontend *fe)
  146. {
  147. xc5000_TunerReset(fe);
  148. return XC_RESULT_SUCCESS;
  149. }
  150. static void xc_wait(int wait_ms)
  151. {
  152. msleep(wait_ms);
  153. }
  154. static void xc5000_TunerReset(struct dvb_frontend *fe)
  155. {
  156. struct xc5000_priv *priv = fe->tuner_priv;
  157. int ret;
  158. dprintk(1, "%s()\n", __FUNCTION__);
  159. if (priv->cfg->tuner_reset) {
  160. ret = priv->cfg->tuner_reset(fe);
  161. if (ret)
  162. printk(KERN_ERR "xc5000: reset failed\n");
  163. } else
  164. printk(KERN_ERR "xc5000: no tuner reset function, fatal\n");
  165. }
  166. static int xc_write_reg(struct xc5000_priv *priv, u16 regAddr, u16 i2cData)
  167. {
  168. u8 buf[4];
  169. int WatchDogTimer = 5;
  170. int result;
  171. buf[0] = (regAddr >> 8) & 0xFF;
  172. buf[1] = regAddr & 0xFF;
  173. buf[2] = (i2cData >> 8) & 0xFF;
  174. buf[3] = i2cData & 0xFF;
  175. result = xc_send_i2c_data(priv, buf, 4);
  176. if (result == XC_RESULT_SUCCESS) {
  177. /* wait for busy flag to clear */
  178. while ((WatchDogTimer > 0) && (result == XC_RESULT_SUCCESS)) {
  179. buf[0] = 0;
  180. buf[1] = XREG_BUSY;
  181. result = xc_send_i2c_data(priv, buf, 2);
  182. if (result == XC_RESULT_SUCCESS) {
  183. result = xc_read_i2c_data(priv, buf, 2);
  184. if (result == XC_RESULT_SUCCESS) {
  185. if ((buf[0] == 0) && (buf[1] == 0)) {
  186. /* busy flag cleared */
  187. break;
  188. } else {
  189. xc_wait(100); /* wait 5 ms */
  190. WatchDogTimer--;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. if (WatchDogTimer < 0)
  197. result = XC_RESULT_I2C_WRITE_FAILURE;
  198. return result;
  199. }
  200. static int xc_read_reg(struct xc5000_priv *priv, u16 regAddr, u16 *i2cData)
  201. {
  202. u8 buf[2];
  203. int result;
  204. buf[0] = (regAddr >> 8) & 0xFF;
  205. buf[1] = regAddr & 0xFF;
  206. result = xc_send_i2c_data(priv, buf, 2);
  207. if (result != XC_RESULT_SUCCESS)
  208. return result;
  209. result = xc_read_i2c_data(priv, buf, 2);
  210. if (result != XC_RESULT_SUCCESS)
  211. return result;
  212. *i2cData = buf[0] * 256 + buf[1];
  213. return result;
  214. }
  215. static int xc_load_i2c_sequence(struct dvb_frontend *fe, u8 i2c_sequence[])
  216. {
  217. struct xc5000_priv *priv = fe->tuner_priv;
  218. int i, nbytes_to_send, result;
  219. unsigned int len, pos, index;
  220. u8 buf[XC_MAX_I2C_WRITE_LENGTH];
  221. index=0;
  222. while ((i2c_sequence[index]!=0xFF) || (i2c_sequence[index+1]!=0xFF)) {
  223. len = i2c_sequence[index]* 256 + i2c_sequence[index+1];
  224. if (len == 0x0000) {
  225. /* RESET command */
  226. result = xc_reset(fe);
  227. index += 2;
  228. if (result != XC_RESULT_SUCCESS)
  229. return result;
  230. } else if (len & 0x8000) {
  231. /* WAIT command */
  232. xc_wait(len & 0x7FFF);
  233. index += 2;
  234. } else {
  235. /* Send i2c data whilst ensuring individual transactions
  236. * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
  237. */
  238. index += 2;
  239. buf[0] = i2c_sequence[index];
  240. buf[1] = i2c_sequence[index + 1];
  241. pos = 2;
  242. while (pos < len) {
  243. if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2) {
  244. nbytes_to_send = XC_MAX_I2C_WRITE_LENGTH;
  245. } else {
  246. nbytes_to_send = (len - pos + 2);
  247. }
  248. for (i=2; i<nbytes_to_send; i++) {
  249. buf[i] = i2c_sequence[index + pos + i - 2];
  250. }
  251. result = xc_send_i2c_data(priv, buf, nbytes_to_send);
  252. if (result != XC_RESULT_SUCCESS)
  253. return result;
  254. pos += nbytes_to_send - 2;
  255. }
  256. index += len;
  257. }
  258. }
  259. return XC_RESULT_SUCCESS;
  260. }
  261. static int xc_initialize(struct xc5000_priv *priv)
  262. {
  263. dprintk(1, "%s()\n", __FUNCTION__);
  264. return xc_write_reg(priv, XREG_INIT, 0);
  265. }
  266. static int xc_SetTVStandard(struct xc5000_priv *priv,
  267. u16 VideoMode, u16 AudioMode)
  268. {
  269. int ret;
  270. dprintk(1, "%s(%d,%d)\n", __FUNCTION__, VideoMode, AudioMode);
  271. dprintk(1, "%s() Standard = %s\n",
  272. __FUNCTION__,
  273. XC5000_Standard[priv->video_standard].Name);
  274. ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
  275. if (ret == XC_RESULT_SUCCESS)
  276. ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
  277. return ret;
  278. }
  279. static int xc_shutdown(struct xc5000_priv *priv)
  280. {
  281. return xc_write_reg(priv, XREG_POWER_DOWN, 0);
  282. }
  283. static int xc_SetSignalSource(struct xc5000_priv *priv, u16 rf_mode)
  284. {
  285. dprintk(1, "%s(%d) Source = %s\n", __FUNCTION__, rf_mode,
  286. rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
  287. if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE))
  288. {
  289. rf_mode = XC_RF_MODE_CABLE;
  290. printk(KERN_ERR
  291. "%s(), Invalid mode, defaulting to CABLE",
  292. __FUNCTION__);
  293. }
  294. return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
  295. }
  296. static const struct dvb_tuner_ops xc5000_tuner_ops;
  297. static int xc_set_RF_frequency(struct xc5000_priv *priv, u32 freq_hz)
  298. {
  299. u16 freq_code;
  300. dprintk(1, "%s(%d)\n", __FUNCTION__, freq_hz);
  301. if ((freq_hz > xc5000_tuner_ops.info.frequency_max) ||
  302. (freq_hz < xc5000_tuner_ops.info.frequency_min))
  303. return XC_RESULT_OUT_OF_RANGE;
  304. freq_code = (u16)(freq_hz / 15625);
  305. return xc_write_reg(priv, XREG_RF_FREQ, freq_code);
  306. }
  307. static int xc_set_IF_frequency(struct xc5000_priv *priv, u32 freq_khz)
  308. {
  309. u32 freq_code = (freq_khz * 1024)/1000;
  310. dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
  311. __FUNCTION__, freq_khz, freq_code);
  312. return xc_write_reg(priv, XREG_IF_OUT, freq_code);
  313. }
  314. static int xc_get_ADC_Envelope(struct xc5000_priv *priv, u16 *adc_envelope)
  315. {
  316. return xc_read_reg(priv, XREG_ADC_ENV, adc_envelope);
  317. }
  318. static int xc_get_frequency_error(struct xc5000_priv *priv, u32 *freq_error_hz)
  319. {
  320. int result;
  321. u16 regData;
  322. u32 tmp;
  323. result = xc_read_reg(priv, XREG_FREQ_ERROR, &regData);
  324. if (result)
  325. return result;
  326. tmp = (u32)regData;
  327. (*freq_error_hz) = (tmp * 15625) / 1000;
  328. return result;
  329. }
  330. static int xc_get_lock_status(struct xc5000_priv *priv, u16 *lock_status)
  331. {
  332. return xc_read_reg(priv, XREG_LOCK, lock_status);
  333. }
  334. static int xc_get_version(struct xc5000_priv *priv,
  335. u8 *hw_majorversion, u8 *hw_minorversion,
  336. u8 *fw_majorversion, u8 *fw_minorversion)
  337. {
  338. u16 data;
  339. int result;
  340. result = xc_read_reg(priv, XREG_VERSION, &data);
  341. if (result)
  342. return result;
  343. (*hw_majorversion) = (data >> 12) & 0x0F;
  344. (*hw_minorversion) = (data >> 8) & 0x0F;
  345. (*fw_majorversion) = (data >> 4) & 0x0F;
  346. (*fw_minorversion) = data & 0x0F;
  347. return 0;
  348. }
  349. static int xc_get_hsync_freq(struct xc5000_priv *priv, u32 *hsync_freq_hz)
  350. {
  351. u16 regData;
  352. int result;
  353. result = xc_read_reg(priv, XREG_HSYNC_FREQ, &regData);
  354. if (result)
  355. return result;
  356. (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
  357. return result;
  358. }
  359. static int xc_get_frame_lines(struct xc5000_priv *priv, u16 *frame_lines)
  360. {
  361. return xc_read_reg(priv, XREG_FRAME_LINES, frame_lines);
  362. }
  363. static int xc_get_quality(struct xc5000_priv *priv, u16 *quality)
  364. {
  365. return xc_read_reg(priv, XREG_QUALITY, quality);
  366. }
  367. static u16 WaitForLock(struct xc5000_priv *priv)
  368. {
  369. u16 lockState = 0;
  370. int watchDogCount = 40;
  371. while ((lockState == 0) && (watchDogCount > 0)) {
  372. xc_get_lock_status(priv, &lockState);
  373. if (lockState != 1) {
  374. xc_wait(5);
  375. watchDogCount--;
  376. }
  377. }
  378. return lockState;
  379. }
  380. static int xc_tune_channel(struct xc5000_priv *priv, u32 freq_hz)
  381. {
  382. int found = 0;
  383. dprintk(1, "%s(%d)\n", __FUNCTION__, freq_hz);
  384. if (xc_set_RF_frequency(priv, freq_hz) != XC_RESULT_SUCCESS)
  385. return 0;
  386. if (WaitForLock(priv) == 1)
  387. found = 1;
  388. return found;
  389. }
  390. static int xc5000_readreg(struct xc5000_priv *priv, u16 reg, u16 *val)
  391. {
  392. u8 buf[2] = { reg >> 8, reg & 0xff };
  393. u8 bval[2] = { 0, 0 };
  394. struct i2c_msg msg[2] = {
  395. { .addr = priv->cfg->i2c_address,
  396. .flags = 0, .buf = &buf[0], .len = 2 },
  397. { .addr = priv->cfg->i2c_address,
  398. .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
  399. };
  400. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  401. printk(KERN_WARNING "xc5000 I2C read failed\n");
  402. return -EREMOTEIO;
  403. }
  404. *val = (bval[0] << 8) | bval[1];
  405. return 0;
  406. }
  407. static int xc5000_writeregs(struct xc5000_priv *priv, u8 *buf, u8 len)
  408. {
  409. struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
  410. .flags = 0, .buf = buf, .len = len };
  411. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  412. printk(KERN_ERR "xc5000 I2C write failed (len=%i)\n",
  413. (int)len);
  414. return -EREMOTEIO;
  415. }
  416. return 0;
  417. }
  418. static int xc5000_readregs(struct xc5000_priv *priv, u8 *buf, u8 len)
  419. {
  420. struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
  421. .flags = I2C_M_RD, .buf = buf, .len = len };
  422. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  423. printk(KERN_ERR "xc5000 I2C read failed (len=%i)\n",(int)len);
  424. return -EREMOTEIO;
  425. }
  426. return 0;
  427. }
  428. static int xc5000_fwupload(struct dvb_frontend* fe)
  429. {
  430. struct xc5000_priv *priv = fe->tuner_priv;
  431. const struct firmware *fw;
  432. int ret;
  433. if (!priv->cfg->request_firmware) {
  434. printk(KERN_ERR "xc5000: no firmware callback, fatal\n");
  435. return -EIO;
  436. }
  437. /* request the firmware, this will block and timeout */
  438. printk(KERN_INFO "xc5000: waiting for firmware upload (%s)...\n",
  439. XC5000_DEFAULT_FIRMWARE);
  440. ret = priv->cfg->request_firmware(fe, &fw, XC5000_DEFAULT_FIRMWARE);
  441. if (ret) {
  442. printk(KERN_ERR "xc5000: Upload failed. (file not found?)\n");
  443. ret = XC_RESULT_RESET_FAILURE;
  444. } else {
  445. printk(KERN_INFO "xc5000: firmware read %Zu bytes.\n",
  446. fw->size);
  447. ret = XC_RESULT_SUCCESS;
  448. }
  449. if (fw->size != XC5000_DEFAULT_FIRMWARE_SIZE) {
  450. printk(KERN_ERR "xc5000: firmware incorrect size\n");
  451. ret = XC_RESULT_RESET_FAILURE;
  452. } else {
  453. printk(KERN_INFO "xc5000: firmware upload\n");
  454. ret = xc_load_i2c_sequence(fe, fw->data );
  455. }
  456. release_firmware(fw);
  457. return ret;
  458. }
  459. static void xc_debug_dump(struct xc5000_priv *priv)
  460. {
  461. u16 adc_envelope;
  462. u32 freq_error_hz = 0;
  463. u16 lock_status;
  464. u32 hsync_freq_hz = 0;
  465. u16 frame_lines;
  466. u16 quality;
  467. u8 hw_majorversion = 0, hw_minorversion = 0;
  468. u8 fw_majorversion = 0, fw_minorversion = 0;
  469. /* Wait for stats to stabilize.
  470. * Frame Lines needs two frame times after initial lock
  471. * before it is valid.
  472. */
  473. xc_wait(100);
  474. xc_get_ADC_Envelope(priv, &adc_envelope);
  475. dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
  476. xc_get_frequency_error(priv, &freq_error_hz);
  477. dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
  478. xc_get_lock_status(priv, &lock_status);
  479. dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
  480. lock_status);
  481. xc_get_version(priv, &hw_majorversion, &hw_minorversion,
  482. &fw_majorversion, &fw_minorversion);
  483. dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
  484. hw_majorversion, hw_minorversion,
  485. fw_majorversion, fw_minorversion);
  486. xc_get_hsync_freq(priv, &hsync_freq_hz);
  487. dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
  488. xc_get_frame_lines(priv, &frame_lines);
  489. dprintk(1, "*** Frame lines = %d\n", frame_lines);
  490. xc_get_quality(priv, &quality);
  491. dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
  492. }
  493. static int xc5000_set_params(struct dvb_frontend *fe,
  494. struct dvb_frontend_parameters *params)
  495. {
  496. struct xc5000_priv *priv = fe->tuner_priv;
  497. int ret;
  498. dprintk(1, "%s() frequency=%d (Hz)\n", __FUNCTION__, params->frequency);
  499. switch(params->u.vsb.modulation) {
  500. case VSB_8:
  501. case VSB_16:
  502. dprintk(1, "%s() VSB modulation\n", __FUNCTION__);
  503. priv->rf_mode = XC_RF_MODE_AIR;
  504. priv->freq_hz = params->frequency - 1750000;
  505. priv->bandwidth = BANDWIDTH_6_MHZ;
  506. priv->video_standard = DTV6;
  507. break;
  508. case QAM_64:
  509. case QAM_256:
  510. case QAM_AUTO:
  511. dprintk(1, "%s() QAM modulation\n", __FUNCTION__);
  512. priv->rf_mode = XC_RF_MODE_CABLE;
  513. priv->freq_hz = params->frequency - 1750000;
  514. priv->bandwidth = BANDWIDTH_6_MHZ;
  515. priv->video_standard = DTV6;
  516. break;
  517. default:
  518. return -EINVAL;
  519. }
  520. dprintk(1, "%s() frequency=%d (compensated)\n",
  521. __FUNCTION__, priv->freq_hz);
  522. ret = xc_SetSignalSource(priv, priv->rf_mode);
  523. if (ret != XC_RESULT_SUCCESS) {
  524. printk(KERN_ERR
  525. "xc5000: xc_SetSignalSource(%d) failed\n",
  526. priv->rf_mode);
  527. return -EREMOTEIO;
  528. }
  529. ret = xc_SetTVStandard(priv,
  530. XC5000_Standard[priv->video_standard].VideoMode,
  531. XC5000_Standard[priv->video_standard].AudioMode);
  532. if (ret != XC_RESULT_SUCCESS) {
  533. printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
  534. return -EREMOTEIO;
  535. }
  536. ret = xc_set_IF_frequency(priv, priv->cfg->if_khz);
  537. if (ret != XC_RESULT_SUCCESS) {
  538. printk(KERN_ERR "xc5000: xc_Set_IF_frequency(%d) failed\n",
  539. priv->cfg->if_khz);
  540. return -EIO;
  541. }
  542. xc_tune_channel(priv, priv->freq_hz);
  543. if (debug)
  544. xc_debug_dump(priv);
  545. return 0;
  546. }
  547. static int xc5000_get_frequency(struct dvb_frontend *fe, u32 *freq)
  548. {
  549. struct xc5000_priv *priv = fe->tuner_priv;
  550. dprintk(1, "%s()\n", __FUNCTION__);
  551. *freq = priv->freq_hz;
  552. return 0;
  553. }
  554. static int xc5000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
  555. {
  556. struct xc5000_priv *priv = fe->tuner_priv;
  557. dprintk(1, "%s()\n", __FUNCTION__);
  558. *bw = priv->bandwidth;
  559. return 0;
  560. }
  561. static int xc5000_get_status(struct dvb_frontend *fe, u32 *status)
  562. {
  563. struct xc5000_priv *priv = fe->tuner_priv;
  564. u16 lock_status = 0;
  565. xc_get_lock_status(priv, &lock_status);
  566. dprintk(1, "%s() lock_status = 0x%08x\n", __FUNCTION__, lock_status);
  567. *status = lock_status;
  568. return 0;
  569. }
  570. static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe)
  571. {
  572. struct xc5000_priv *priv = fe->tuner_priv;
  573. int ret;
  574. if (priv->fwloaded == 0) {
  575. ret = xc5000_fwupload(fe);
  576. if (ret != XC_RESULT_SUCCESS)
  577. return ret;
  578. priv->fwloaded = 1;
  579. }
  580. /* Start the tuner self-calibration process */
  581. ret |= xc_initialize(priv);
  582. /* Wait for calibration to complete.
  583. * We could continue but XC5000 will clock stretch subsequent
  584. * I2C transactions until calibration is complete. This way we
  585. * don't have to rely on clock stretching working.
  586. */
  587. xc_wait( 100 );
  588. /* Default to "CABLE" mode */
  589. ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
  590. return ret;
  591. }
  592. static int xc5000_sleep(struct dvb_frontend *fe)
  593. {
  594. struct xc5000_priv *priv = fe->tuner_priv;
  595. dprintk(1, "%s()\n", __FUNCTION__);
  596. return xc_shutdown(priv);
  597. }
  598. static int xc5000_init(struct dvb_frontend *fe)
  599. {
  600. struct xc5000_priv *priv = fe->tuner_priv;
  601. dprintk(1, "%s()\n", __FUNCTION__);
  602. if (xc_load_fw_and_init_tuner(fe) != XC_RESULT_SUCCESS) {
  603. printk(KERN_ERR "xc5000: Unable to initialise tuner\n");
  604. return -EREMOTEIO;
  605. }
  606. if (debug)
  607. xc_debug_dump(priv);
  608. return 0;
  609. }
  610. static int xc5000_release(struct dvb_frontend *fe)
  611. {
  612. dprintk(1, "%s()\n", __FUNCTION__);
  613. kfree(fe->tuner_priv);
  614. fe->tuner_priv = NULL;
  615. return 0;
  616. }
  617. static const struct dvb_tuner_ops xc5000_tuner_ops = {
  618. .info = {
  619. .name = "Xceive XC5000",
  620. .frequency_min = 1000000,
  621. .frequency_max = 1023000000,
  622. .frequency_step = 50000,
  623. },
  624. .release = xc5000_release,
  625. .init = xc5000_init,
  626. .sleep = xc5000_sleep,
  627. .set_params = xc5000_set_params,
  628. .get_frequency = xc5000_get_frequency,
  629. .get_bandwidth = xc5000_get_bandwidth,
  630. .get_status = xc5000_get_status
  631. };
  632. struct dvb_frontend * xc5000_attach(struct dvb_frontend *fe,
  633. struct i2c_adapter *i2c,
  634. struct xc5000_config *cfg)
  635. {
  636. struct xc5000_priv *priv = NULL;
  637. u16 id = 0;
  638. dprintk(1, "%s()\n", __FUNCTION__);
  639. priv = kzalloc(sizeof(struct xc5000_priv), GFP_KERNEL);
  640. if (priv == NULL)
  641. return NULL;
  642. priv->cfg = cfg;
  643. priv->bandwidth = BANDWIDTH_6_MHZ;
  644. priv->i2c = i2c;
  645. priv->fwloaded = 0;
  646. if (xc5000_readreg(priv, XREG_PRODUCT_ID, &id) != 0) {
  647. kfree(priv);
  648. return NULL;
  649. }
  650. if ((id != 0x2000) && (id != 0x1388)) {
  651. printk(KERN_ERR
  652. "xc5000: Device not found at addr 0x%02x (0x%x)\n",
  653. cfg->i2c_address, id);
  654. kfree(priv);
  655. return NULL;
  656. }
  657. printk(KERN_INFO "xc5000: successfully identified at address 0x%02x\n",
  658. cfg->i2c_address);
  659. memcpy(&fe->ops.tuner_ops, &xc5000_tuner_ops,
  660. sizeof(struct dvb_tuner_ops));
  661. fe->tuner_priv = priv;
  662. return fe;
  663. }
  664. EXPORT_SYMBOL(xc5000_attach);
  665. MODULE_AUTHOR("Steven Toth");
  666. MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver");
  667. MODULE_LICENSE("GPL");