xc5000.c 36 KB

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