xc5000.c 26 KB

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