xc5000.c 33 KB

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