xc5000.c 37 KB

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