xc5000.c 24 KB

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