xc5000.c 34 KB

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