xc5000.c 25 KB

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