xc5000.c 29 KB

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