xc4000.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. /*
  2. * Driver for Xceive XC4000 "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. * Copyright (c) 2009 Davide Ferri <d.ferri@zero11.it>
  8. * Copyright (c) 2010 Istvan Varga <istvan_v@mailbox.hu>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/videodev2.h>
  27. #include <linux/delay.h>
  28. #include <linux/dvb/frontend.h>
  29. #include <linux/i2c.h>
  30. #include <linux/mutex.h>
  31. #include <asm/unaligned.h>
  32. #include "dvb_frontend.h"
  33. #include "xc4000.h"
  34. #include "tuner-i2c.h"
  35. #include "tuner-xc2028-types.h"
  36. static int debug;
  37. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug, "Debugging level (0 to 2, default: 0 (off)).");
  39. static int no_poweroff;
  40. module_param(no_poweroff, int, 0644);
  41. MODULE_PARM_DESC(no_poweroff, "Power management (1: disabled, 2: enabled, "
  42. "0 (default): use device-specific default mode).");
  43. static int audio_std;
  44. module_param(audio_std, int, 0644);
  45. MODULE_PARM_DESC(audio_std, "Audio standard. XC4000 audio decoder explicitly "
  46. "needs to know what audio standard is needed for some video standards "
  47. "with audio A2 or NICAM. The valid settings are a sum of:\n"
  48. " 1: use NICAM/B or A2/B instead of NICAM/A or A2/A\n"
  49. " 2: use A2 instead of NICAM or BTSC\n"
  50. " 4: use SECAM/K3 instead of K1\n"
  51. " 8: use PAL-D/K audio for SECAM-D/K\n"
  52. "16: use FM radio input 1 instead of input 2\n"
  53. "32: use mono audio (the lower three bits are ignored)");
  54. static char firmware_name[30];
  55. module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
  56. MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
  57. "default firmware name.");
  58. static DEFINE_MUTEX(xc4000_list_mutex);
  59. static LIST_HEAD(hybrid_tuner_instance_list);
  60. #define dprintk(level, fmt, arg...) if (debug >= level) \
  61. printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
  62. /* struct for storing firmware table */
  63. struct firmware_description {
  64. unsigned int type;
  65. v4l2_std_id id;
  66. __u16 int_freq;
  67. unsigned char *ptr;
  68. unsigned int size;
  69. };
  70. struct firmware_properties {
  71. unsigned int type;
  72. v4l2_std_id id;
  73. v4l2_std_id std_req;
  74. __u16 int_freq;
  75. unsigned int scode_table;
  76. int scode_nr;
  77. };
  78. struct xc4000_priv {
  79. struct tuner_i2c_props i2c_props;
  80. struct list_head hybrid_tuner_instance_list;
  81. struct firmware_description *firm;
  82. int firm_size;
  83. u32 if_khz;
  84. u32 freq_hz;
  85. u32 bandwidth;
  86. u8 video_standard;
  87. u8 rf_mode;
  88. u8 default_pm;
  89. u8 dvb_amplitude;
  90. u8 set_smoothedcvbs;
  91. u8 ignore_i2c_write_errors;
  92. __u16 firm_version;
  93. struct firmware_properties cur_fw;
  94. __u16 hwmodel;
  95. __u16 hwvers;
  96. struct mutex lock;
  97. };
  98. #define XC4000_AUDIO_STD_B 1
  99. #define XC4000_AUDIO_STD_A2 2
  100. #define XC4000_AUDIO_STD_K3 4
  101. #define XC4000_AUDIO_STD_L 8
  102. #define XC4000_AUDIO_STD_INPUT1 16
  103. #define XC4000_AUDIO_STD_MONO 32
  104. #define XC4000_DEFAULT_FIRMWARE "dvb-fe-xc4000-1.4.fw"
  105. /* Misc Defines */
  106. #define MAX_TV_STANDARD 24
  107. #define XC_MAX_I2C_WRITE_LENGTH 64
  108. #define XC_POWERED_DOWN 0x80000000U
  109. /* Signal Types */
  110. #define XC_RF_MODE_AIR 0
  111. #define XC_RF_MODE_CABLE 1
  112. /* Product id */
  113. #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
  114. #define XC_PRODUCT_ID_XC4000 0x0FA0
  115. #define XC_PRODUCT_ID_XC4100 0x1004
  116. /* Registers (Write-only) */
  117. #define XREG_INIT 0x00
  118. #define XREG_VIDEO_MODE 0x01
  119. #define XREG_AUDIO_MODE 0x02
  120. #define XREG_RF_FREQ 0x03
  121. #define XREG_D_CODE 0x04
  122. #define XREG_DIRECTSITTING_MODE 0x05
  123. #define XREG_SEEK_MODE 0x06
  124. #define XREG_POWER_DOWN 0x08
  125. #define XREG_SIGNALSOURCE 0x0A
  126. #define XREG_SMOOTHEDCVBS 0x0E
  127. #define XREG_AMPLITUDE 0x10
  128. /* Registers (Read-only) */
  129. #define XREG_ADC_ENV 0x00
  130. #define XREG_QUALITY 0x01
  131. #define XREG_FRAME_LINES 0x02
  132. #define XREG_HSYNC_FREQ 0x03
  133. #define XREG_LOCK 0x04
  134. #define XREG_FREQ_ERROR 0x05
  135. #define XREG_SNR 0x06
  136. #define XREG_VERSION 0x07
  137. #define XREG_PRODUCT_ID 0x08
  138. /*
  139. Basic firmware description. This will remain with
  140. the driver for documentation purposes.
  141. This represents an I2C firmware file encoded as a
  142. string of unsigned char. Format is as follows:
  143. char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
  144. char[1 ]=len0_LSB -> length of first write transaction
  145. char[2 ]=data0 -> first byte to be sent
  146. char[3 ]=data1
  147. char[4 ]=data2
  148. char[ ]=...
  149. char[M ]=dataN -> last byte to be sent
  150. char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
  151. char[M+2]=len1_LSB -> length of second write transaction
  152. char[M+3]=data0
  153. char[M+4]=data1
  154. ...
  155. etc.
  156. The [len] value should be interpreted as follows:
  157. len= len_MSB _ len_LSB
  158. len=1111_1111_1111_1111 : End of I2C_SEQUENCE
  159. len=0000_0000_0000_0000 : Reset command: Do hardware reset
  160. len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
  161. len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
  162. For the RESET and WAIT commands, the two following bytes will contain
  163. immediately the length of the following transaction.
  164. */
  165. struct XC_TV_STANDARD {
  166. const char *Name;
  167. u16 audio_mode;
  168. u16 video_mode;
  169. u16 int_freq;
  170. };
  171. /* Tuner standards */
  172. #define XC4000_MN_NTSC_PAL_BTSC 0
  173. #define XC4000_MN_NTSC_PAL_A2 1
  174. #define XC4000_MN_NTSC_PAL_EIAJ 2
  175. #define XC4000_MN_NTSC_PAL_Mono 3
  176. #define XC4000_BG_PAL_A2 4
  177. #define XC4000_BG_PAL_NICAM 5
  178. #define XC4000_BG_PAL_MONO 6
  179. #define XC4000_I_PAL_NICAM 7
  180. #define XC4000_I_PAL_NICAM_MONO 8
  181. #define XC4000_DK_PAL_A2 9
  182. #define XC4000_DK_PAL_NICAM 10
  183. #define XC4000_DK_PAL_MONO 11
  184. #define XC4000_DK_SECAM_A2DK1 12
  185. #define XC4000_DK_SECAM_A2LDK3 13
  186. #define XC4000_DK_SECAM_A2MONO 14
  187. #define XC4000_DK_SECAM_NICAM 15
  188. #define XC4000_L_SECAM_NICAM 16
  189. #define XC4000_LC_SECAM_NICAM 17
  190. #define XC4000_DTV6 18
  191. #define XC4000_DTV8 19
  192. #define XC4000_DTV7_8 20
  193. #define XC4000_DTV7 21
  194. #define XC4000_FM_Radio_INPUT2 22
  195. #define XC4000_FM_Radio_INPUT1 23
  196. static struct XC_TV_STANDARD xc4000_standard[MAX_TV_STANDARD] = {
  197. {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
  198. {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
  199. {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
  200. {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
  201. {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
  202. {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
  203. {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
  204. {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
  205. {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
  206. {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
  207. {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
  208. {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
  209. {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
  210. {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
  211. {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
  212. {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
  213. {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
  214. {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
  215. {"DTV6", 0x00C0, 0x8002, 0},
  216. {"DTV8", 0x00C0, 0x800B, 0},
  217. {"DTV7/8", 0x00C0, 0x801B, 0},
  218. {"DTV7", 0x00C0, 0x8007, 0},
  219. {"FM Radio-INPUT2", 0x0008, 0x9800, 10700},
  220. {"FM Radio-INPUT1", 0x0008, 0x9000, 10700}
  221. };
  222. static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
  223. static int xc4000_tuner_reset(struct dvb_frontend *fe);
  224. static void xc_debug_dump(struct xc4000_priv *priv);
  225. static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
  226. {
  227. struct i2c_msg msg = { .addr = priv->i2c_props.addr,
  228. .flags = 0, .buf = buf, .len = len };
  229. if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
  230. if (priv->ignore_i2c_write_errors == 0) {
  231. printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
  232. len);
  233. if (len == 4) {
  234. printk(KERN_ERR "bytes %02x %02x %02x %02x\n", buf[0],
  235. buf[1], buf[2], buf[3]);
  236. }
  237. return -EREMOTEIO;
  238. }
  239. }
  240. return 0;
  241. }
  242. static int xc4000_tuner_reset(struct dvb_frontend *fe)
  243. {
  244. struct xc4000_priv *priv = fe->tuner_priv;
  245. int ret;
  246. dprintk(1, "%s()\n", __func__);
  247. if (fe->callback) {
  248. ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
  249. fe->dvb->priv :
  250. priv->i2c_props.adap->algo_data,
  251. DVB_FRONTEND_COMPONENT_TUNER,
  252. XC4000_TUNER_RESET, 0);
  253. if (ret) {
  254. printk(KERN_ERR "xc4000: reset failed\n");
  255. return -EREMOTEIO;
  256. }
  257. } else {
  258. printk(KERN_ERR "xc4000: no tuner reset callback function, "
  259. "fatal\n");
  260. return -EINVAL;
  261. }
  262. return 0;
  263. }
  264. static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
  265. {
  266. u8 buf[4];
  267. int result;
  268. buf[0] = (regAddr >> 8) & 0xFF;
  269. buf[1] = regAddr & 0xFF;
  270. buf[2] = (i2cData >> 8) & 0xFF;
  271. buf[3] = i2cData & 0xFF;
  272. result = xc_send_i2c_data(priv, buf, 4);
  273. return result;
  274. }
  275. static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
  276. {
  277. struct xc4000_priv *priv = fe->tuner_priv;
  278. int i, nbytes_to_send, result;
  279. unsigned int len, pos, index;
  280. u8 buf[XC_MAX_I2C_WRITE_LENGTH];
  281. index = 0;
  282. while ((i2c_sequence[index] != 0xFF) ||
  283. (i2c_sequence[index + 1] != 0xFF)) {
  284. len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
  285. if (len == 0x0000) {
  286. /* RESET command */
  287. /* NOTE: this is ignored, as the reset callback was */
  288. /* already called by check_firmware() */
  289. index += 2;
  290. } else if (len & 0x8000) {
  291. /* WAIT command */
  292. msleep(len & 0x7FFF);
  293. index += 2;
  294. } else {
  295. /* Send i2c data whilst ensuring individual transactions
  296. * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
  297. */
  298. index += 2;
  299. buf[0] = i2c_sequence[index];
  300. buf[1] = i2c_sequence[index + 1];
  301. pos = 2;
  302. while (pos < len) {
  303. if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
  304. nbytes_to_send =
  305. XC_MAX_I2C_WRITE_LENGTH;
  306. else
  307. nbytes_to_send = (len - pos + 2);
  308. for (i = 2; i < nbytes_to_send; i++) {
  309. buf[i] = i2c_sequence[index + pos +
  310. i - 2];
  311. }
  312. result = xc_send_i2c_data(priv, buf,
  313. nbytes_to_send);
  314. if (result != 0)
  315. return result;
  316. pos += nbytes_to_send - 2;
  317. }
  318. index += len;
  319. }
  320. }
  321. return 0;
  322. }
  323. static int xc_set_tv_standard(struct xc4000_priv *priv,
  324. u16 video_mode, u16 audio_mode)
  325. {
  326. int ret;
  327. dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, video_mode, audio_mode);
  328. dprintk(1, "%s() Standard = %s\n",
  329. __func__,
  330. xc4000_standard[priv->video_standard].Name);
  331. /* Don't complain when the request fails because of i2c stretching */
  332. priv->ignore_i2c_write_errors = 1;
  333. ret = xc_write_reg(priv, XREG_VIDEO_MODE, video_mode);
  334. if (ret == 0)
  335. ret = xc_write_reg(priv, XREG_AUDIO_MODE, audio_mode);
  336. priv->ignore_i2c_write_errors = 0;
  337. return ret;
  338. }
  339. static int xc_set_signal_source(struct xc4000_priv *priv, u16 rf_mode)
  340. {
  341. dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
  342. rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
  343. if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
  344. rf_mode = XC_RF_MODE_CABLE;
  345. printk(KERN_ERR
  346. "%s(), Invalid mode, defaulting to CABLE",
  347. __func__);
  348. }
  349. return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
  350. }
  351. static const struct dvb_tuner_ops xc4000_tuner_ops;
  352. static int xc_set_rf_frequency(struct xc4000_priv *priv, u32 freq_hz)
  353. {
  354. u16 freq_code;
  355. dprintk(1, "%s(%u)\n", __func__, freq_hz);
  356. if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
  357. (freq_hz < xc4000_tuner_ops.info.frequency_min))
  358. return -EINVAL;
  359. freq_code = (u16)(freq_hz / 15625);
  360. /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
  361. FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
  362. only be used for fast scanning for channel lock) */
  363. /* WAS: XREG_FINERFREQ */
  364. return xc_write_reg(priv, XREG_RF_FREQ, freq_code);
  365. }
  366. static int xc_get_adc_envelope(struct xc4000_priv *priv, u16 *adc_envelope)
  367. {
  368. return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
  369. }
  370. static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
  371. {
  372. int result;
  373. u16 regData;
  374. u32 tmp;
  375. result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
  376. if (result != 0)
  377. return result;
  378. tmp = (u32)regData & 0xFFFFU;
  379. tmp = (tmp < 0x8000U ? tmp : 0x10000U - tmp);
  380. (*freq_error_hz) = tmp * 15625;
  381. return result;
  382. }
  383. static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
  384. {
  385. return xc4000_readreg(priv, XREG_LOCK, lock_status);
  386. }
  387. static int xc_get_version(struct xc4000_priv *priv,
  388. u8 *hw_majorversion, u8 *hw_minorversion,
  389. u8 *fw_majorversion, u8 *fw_minorversion)
  390. {
  391. u16 data;
  392. int result;
  393. result = xc4000_readreg(priv, XREG_VERSION, &data);
  394. if (result != 0)
  395. return result;
  396. (*hw_majorversion) = (data >> 12) & 0x0F;
  397. (*hw_minorversion) = (data >> 8) & 0x0F;
  398. (*fw_majorversion) = (data >> 4) & 0x0F;
  399. (*fw_minorversion) = data & 0x0F;
  400. return 0;
  401. }
  402. static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
  403. {
  404. u16 regData;
  405. int result;
  406. result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
  407. if (result != 0)
  408. return result;
  409. (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
  410. return result;
  411. }
  412. static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
  413. {
  414. return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
  415. }
  416. static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
  417. {
  418. return xc4000_readreg(priv, XREG_QUALITY, quality);
  419. }
  420. static u16 xc_wait_for_lock(struct xc4000_priv *priv)
  421. {
  422. u16 lock_state = 0;
  423. int watchdog_count = 40;
  424. while ((lock_state == 0) && (watchdog_count > 0)) {
  425. xc_get_lock_status(priv, &lock_state);
  426. if (lock_state != 1) {
  427. msleep(5);
  428. watchdog_count--;
  429. }
  430. }
  431. return lock_state;
  432. }
  433. static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz)
  434. {
  435. int found = 1;
  436. int result;
  437. dprintk(1, "%s(%u)\n", __func__, freq_hz);
  438. /* Don't complain when the request fails because of i2c stretching */
  439. priv->ignore_i2c_write_errors = 1;
  440. result = xc_set_rf_frequency(priv, freq_hz);
  441. priv->ignore_i2c_write_errors = 0;
  442. if (result != 0)
  443. return 0;
  444. /* wait for lock only in analog TV mode */
  445. if ((priv->cur_fw.type & (FM | DTV6 | DTV7 | DTV78 | DTV8)) == 0) {
  446. if (xc_wait_for_lock(priv) != 1)
  447. found = 0;
  448. }
  449. /* Wait for stats to stabilize.
  450. * Frame Lines needs two frame times after initial lock
  451. * before it is valid.
  452. */
  453. msleep(debug ? 100 : 10);
  454. if (debug)
  455. xc_debug_dump(priv);
  456. return found;
  457. }
  458. static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
  459. {
  460. u8 buf[2] = { reg >> 8, reg & 0xff };
  461. u8 bval[2] = { 0, 0 };
  462. struct i2c_msg msg[2] = {
  463. { .addr = priv->i2c_props.addr,
  464. .flags = 0, .buf = &buf[0], .len = 2 },
  465. { .addr = priv->i2c_props.addr,
  466. .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
  467. };
  468. if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
  469. printk(KERN_ERR "xc4000: I2C read failed\n");
  470. return -EREMOTEIO;
  471. }
  472. *val = (bval[0] << 8) | bval[1];
  473. return 0;
  474. }
  475. #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
  476. static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
  477. {
  478. if (type & BASE)
  479. printk(KERN_CONT "BASE ");
  480. if (type & INIT1)
  481. printk(KERN_CONT "INIT1 ");
  482. if (type & F8MHZ)
  483. printk(KERN_CONT "F8MHZ ");
  484. if (type & MTS)
  485. printk(KERN_CONT "MTS ");
  486. if (type & D2620)
  487. printk(KERN_CONT "D2620 ");
  488. if (type & D2633)
  489. printk(KERN_CONT "D2633 ");
  490. if (type & DTV6)
  491. printk(KERN_CONT "DTV6 ");
  492. if (type & QAM)
  493. printk(KERN_CONT "QAM ");
  494. if (type & DTV7)
  495. printk(KERN_CONT "DTV7 ");
  496. if (type & DTV78)
  497. printk(KERN_CONT "DTV78 ");
  498. if (type & DTV8)
  499. printk(KERN_CONT "DTV8 ");
  500. if (type & FM)
  501. printk(KERN_CONT "FM ");
  502. if (type & INPUT1)
  503. printk(KERN_CONT "INPUT1 ");
  504. if (type & LCD)
  505. printk(KERN_CONT "LCD ");
  506. if (type & NOGD)
  507. printk(KERN_CONT "NOGD ");
  508. if (type & MONO)
  509. printk(KERN_CONT "MONO ");
  510. if (type & ATSC)
  511. printk(KERN_CONT "ATSC ");
  512. if (type & IF)
  513. printk(KERN_CONT "IF ");
  514. if (type & LG60)
  515. printk(KERN_CONT "LG60 ");
  516. if (type & ATI638)
  517. printk(KERN_CONT "ATI638 ");
  518. if (type & OREN538)
  519. printk(KERN_CONT "OREN538 ");
  520. if (type & OREN36)
  521. printk(KERN_CONT "OREN36 ");
  522. if (type & TOYOTA388)
  523. printk(KERN_CONT "TOYOTA388 ");
  524. if (type & TOYOTA794)
  525. printk(KERN_CONT "TOYOTA794 ");
  526. if (type & DIBCOM52)
  527. printk(KERN_CONT "DIBCOM52 ");
  528. if (type & ZARLINK456)
  529. printk(KERN_CONT "ZARLINK456 ");
  530. if (type & CHINA)
  531. printk(KERN_CONT "CHINA ");
  532. if (type & F6MHZ)
  533. printk(KERN_CONT "F6MHZ ");
  534. if (type & INPUT2)
  535. printk(KERN_CONT "INPUT2 ");
  536. if (type & SCODE)
  537. printk(KERN_CONT "SCODE ");
  538. if (type & HAS_IF)
  539. printk(KERN_CONT "HAS_IF_%d ", int_freq);
  540. }
  541. static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
  542. v4l2_std_id *id)
  543. {
  544. struct xc4000_priv *priv = fe->tuner_priv;
  545. int i, best_i = -1;
  546. unsigned int best_nr_diffs = 255U;
  547. if (!priv->firm) {
  548. printk(KERN_ERR "Error! firmware not loaded\n");
  549. return -EINVAL;
  550. }
  551. if (((type & ~SCODE) == 0) && (*id == 0))
  552. *id = V4L2_STD_PAL;
  553. /* Seek for generic video standard match */
  554. for (i = 0; i < priv->firm_size; i++) {
  555. v4l2_std_id id_diff_mask =
  556. (priv->firm[i].id ^ (*id)) & (*id);
  557. unsigned int type_diff_mask =
  558. (priv->firm[i].type ^ type)
  559. & (BASE_TYPES | DTV_TYPES | LCD | NOGD | MONO | SCODE);
  560. unsigned int nr_diffs;
  561. if (type_diff_mask
  562. & (BASE | INIT1 | FM | DTV6 | DTV7 | DTV78 | DTV8 | SCODE))
  563. continue;
  564. nr_diffs = hweight64(id_diff_mask) + hweight32(type_diff_mask);
  565. if (!nr_diffs) /* Supports all the requested standards */
  566. goto found;
  567. if (nr_diffs < best_nr_diffs) {
  568. best_nr_diffs = nr_diffs;
  569. best_i = i;
  570. }
  571. }
  572. /* FIXME: Would make sense to seek for type "hint" match ? */
  573. if (best_i < 0) {
  574. i = -ENOENT;
  575. goto ret;
  576. }
  577. if (best_nr_diffs > 0U) {
  578. printk(KERN_WARNING
  579. "Selecting best matching firmware (%u bits differ) for "
  580. "type=(%x), id %016llx:\n",
  581. best_nr_diffs, type, (unsigned long long)*id);
  582. i = best_i;
  583. }
  584. found:
  585. *id = priv->firm[i].id;
  586. ret:
  587. if (debug) {
  588. printk(KERN_DEBUG "%s firmware for type=",
  589. (i < 0) ? "Can't find" : "Found");
  590. dump_firm_type(type);
  591. printk(KERN_DEBUG "(%x), id %016llx.\n", type, (unsigned long long)*id);
  592. }
  593. return i;
  594. }
  595. static int load_firmware(struct dvb_frontend *fe, unsigned int type,
  596. v4l2_std_id *id)
  597. {
  598. struct xc4000_priv *priv = fe->tuner_priv;
  599. int pos, rc;
  600. unsigned char *p;
  601. pos = seek_firmware(fe, type, id);
  602. if (pos < 0)
  603. return pos;
  604. p = priv->firm[pos].ptr;
  605. /* Don't complain when the request fails because of i2c stretching */
  606. priv->ignore_i2c_write_errors = 1;
  607. rc = xc_load_i2c_sequence(fe, p);
  608. priv->ignore_i2c_write_errors = 0;
  609. return rc;
  610. }
  611. static int xc4000_fwupload(struct dvb_frontend *fe)
  612. {
  613. struct xc4000_priv *priv = fe->tuner_priv;
  614. const struct firmware *fw = NULL;
  615. const unsigned char *p, *endp;
  616. int rc = 0;
  617. int n, n_array;
  618. char name[33];
  619. const char *fname;
  620. if (firmware_name[0] != '\0')
  621. fname = firmware_name;
  622. else
  623. fname = XC4000_DEFAULT_FIRMWARE;
  624. dprintk(1, "Reading firmware %s\n", fname);
  625. rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
  626. if (rc < 0) {
  627. if (rc == -ENOENT)
  628. printk(KERN_ERR "Error: firmware %s not found.\n", fname);
  629. else
  630. printk(KERN_ERR "Error %d while requesting firmware %s\n",
  631. rc, fname);
  632. return rc;
  633. }
  634. p = fw->data;
  635. endp = p + fw->size;
  636. if (fw->size < sizeof(name) - 1 + 2 + 2) {
  637. printk(KERN_ERR "Error: firmware file %s has invalid size!\n",
  638. fname);
  639. goto corrupt;
  640. }
  641. memcpy(name, p, sizeof(name) - 1);
  642. name[sizeof(name) - 1] = '\0';
  643. p += sizeof(name) - 1;
  644. priv->firm_version = get_unaligned_le16(p);
  645. p += 2;
  646. n_array = get_unaligned_le16(p);
  647. p += 2;
  648. dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
  649. n_array, fname, name,
  650. priv->firm_version >> 8, priv->firm_version & 0xff);
  651. priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
  652. if (priv->firm == NULL) {
  653. printk(KERN_ERR "Not enough memory to load firmware file.\n");
  654. rc = -ENOMEM;
  655. goto done;
  656. }
  657. priv->firm_size = n_array;
  658. n = -1;
  659. while (p < endp) {
  660. __u32 type, size;
  661. v4l2_std_id id;
  662. __u16 int_freq = 0;
  663. n++;
  664. if (n >= n_array) {
  665. printk(KERN_ERR "More firmware images in file than "
  666. "were expected!\n");
  667. goto corrupt;
  668. }
  669. /* Checks if there's enough bytes to read */
  670. if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
  671. goto header;
  672. type = get_unaligned_le32(p);
  673. p += sizeof(type);
  674. id = get_unaligned_le64(p);
  675. p += sizeof(id);
  676. if (type & HAS_IF) {
  677. int_freq = get_unaligned_le16(p);
  678. p += sizeof(int_freq);
  679. if (endp - p < sizeof(size))
  680. goto header;
  681. }
  682. size = get_unaligned_le32(p);
  683. p += sizeof(size);
  684. if (!size || size > endp - p) {
  685. printk(KERN_ERR "Firmware type (%x), id %llx is corrupted (size=%d, expected %d)\n",
  686. type, (unsigned long long)id,
  687. (unsigned)(endp - p), size);
  688. goto corrupt;
  689. }
  690. priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
  691. if (priv->firm[n].ptr == NULL) {
  692. printk(KERN_ERR "Not enough memory to load firmware file.\n");
  693. rc = -ENOMEM;
  694. goto done;
  695. }
  696. if (debug) {
  697. printk(KERN_DEBUG "Reading firmware type ");
  698. dump_firm_type_and_int_freq(type, int_freq);
  699. printk(KERN_DEBUG "(%x), id %llx, size=%d.\n",
  700. type, (unsigned long long)id, size);
  701. }
  702. memcpy(priv->firm[n].ptr, p, size);
  703. priv->firm[n].type = type;
  704. priv->firm[n].id = id;
  705. priv->firm[n].size = size;
  706. priv->firm[n].int_freq = int_freq;
  707. p += size;
  708. }
  709. if (n + 1 != priv->firm_size) {
  710. printk(KERN_ERR "Firmware file is incomplete!\n");
  711. goto corrupt;
  712. }
  713. goto done;
  714. header:
  715. printk(KERN_ERR "Firmware header is incomplete!\n");
  716. corrupt:
  717. rc = -EINVAL;
  718. printk(KERN_ERR "Error: firmware file is corrupted!\n");
  719. done:
  720. release_firmware(fw);
  721. if (rc == 0)
  722. dprintk(1, "Firmware files loaded.\n");
  723. return rc;
  724. }
  725. static int load_scode(struct dvb_frontend *fe, unsigned int type,
  726. v4l2_std_id *id, __u16 int_freq, int scode)
  727. {
  728. struct xc4000_priv *priv = fe->tuner_priv;
  729. int pos, rc;
  730. unsigned char *p;
  731. u8 scode_buf[13];
  732. u8 indirect_mode[5];
  733. dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
  734. if (!int_freq) {
  735. pos = seek_firmware(fe, type, id);
  736. if (pos < 0)
  737. return pos;
  738. } else {
  739. for (pos = 0; pos < priv->firm_size; pos++) {
  740. if ((priv->firm[pos].int_freq == int_freq) &&
  741. (priv->firm[pos].type & HAS_IF))
  742. break;
  743. }
  744. if (pos == priv->firm_size)
  745. return -ENOENT;
  746. }
  747. p = priv->firm[pos].ptr;
  748. if (priv->firm[pos].size != 12 * 16 || scode >= 16)
  749. return -EINVAL;
  750. p += 12 * scode;
  751. if (debug) {
  752. tuner_info("Loading SCODE for type=");
  753. dump_firm_type_and_int_freq(priv->firm[pos].type,
  754. priv->firm[pos].int_freq);
  755. printk(KERN_CONT "(%x), id %016llx.\n", priv->firm[pos].type,
  756. (unsigned long long)*id);
  757. }
  758. scode_buf[0] = 0x00;
  759. memcpy(&scode_buf[1], p, 12);
  760. /* Enter direct-mode */
  761. rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
  762. if (rc < 0) {
  763. printk(KERN_ERR "failed to put device into direct mode!\n");
  764. return -EIO;
  765. }
  766. rc = xc_send_i2c_data(priv, scode_buf, 13);
  767. if (rc != 0) {
  768. /* Even if the send failed, make sure we set back to indirect
  769. mode */
  770. printk(KERN_ERR "Failed to set scode %d\n", rc);
  771. }
  772. /* Switch back to indirect-mode */
  773. memset(indirect_mode, 0, sizeof(indirect_mode));
  774. indirect_mode[4] = 0x88;
  775. xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
  776. msleep(10);
  777. return 0;
  778. }
  779. static int check_firmware(struct dvb_frontend *fe, unsigned int type,
  780. v4l2_std_id std, __u16 int_freq)
  781. {
  782. struct xc4000_priv *priv = fe->tuner_priv;
  783. struct firmware_properties new_fw;
  784. int rc = 0, is_retry = 0;
  785. u16 hwmodel;
  786. v4l2_std_id std0;
  787. u8 hw_major, hw_minor, fw_major, fw_minor;
  788. dprintk(1, "%s called\n", __func__);
  789. if (!priv->firm) {
  790. rc = xc4000_fwupload(fe);
  791. if (rc < 0)
  792. return rc;
  793. }
  794. retry:
  795. new_fw.type = type;
  796. new_fw.id = std;
  797. new_fw.std_req = std;
  798. new_fw.scode_table = SCODE;
  799. new_fw.scode_nr = 0;
  800. new_fw.int_freq = int_freq;
  801. dprintk(1, "checking firmware, user requested type=");
  802. if (debug) {
  803. dump_firm_type(new_fw.type);
  804. printk(KERN_CONT "(%x), id %016llx, ", new_fw.type,
  805. (unsigned long long)new_fw.std_req);
  806. if (!int_freq)
  807. printk(KERN_CONT "scode_tbl ");
  808. else
  809. printk(KERN_CONT "int_freq %d, ", new_fw.int_freq);
  810. printk(KERN_CONT "scode_nr %d\n", new_fw.scode_nr);
  811. }
  812. /* No need to reload base firmware if it matches */
  813. if (priv->cur_fw.type & BASE) {
  814. dprintk(1, "BASE firmware not changed.\n");
  815. goto skip_base;
  816. }
  817. /* Updating BASE - forget about all currently loaded firmware */
  818. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  819. /* Reset is needed before loading firmware */
  820. rc = xc4000_tuner_reset(fe);
  821. if (rc < 0)
  822. goto fail;
  823. /* BASE firmwares are all std0 */
  824. std0 = 0;
  825. rc = load_firmware(fe, BASE, &std0);
  826. if (rc < 0) {
  827. printk(KERN_ERR "Error %d while loading base firmware\n", rc);
  828. goto fail;
  829. }
  830. /* Load INIT1, if needed */
  831. dprintk(1, "Load init1 firmware, if exists\n");
  832. rc = load_firmware(fe, BASE | INIT1, &std0);
  833. if (rc == -ENOENT)
  834. rc = load_firmware(fe, BASE | INIT1, &std0);
  835. if (rc < 0 && rc != -ENOENT) {
  836. tuner_err("Error %d while loading init1 firmware\n",
  837. rc);
  838. goto fail;
  839. }
  840. skip_base:
  841. /*
  842. * No need to reload standard specific firmware if base firmware
  843. * was not reloaded and requested video standards have not changed.
  844. */
  845. if (priv->cur_fw.type == (BASE | new_fw.type) &&
  846. priv->cur_fw.std_req == std) {
  847. dprintk(1, "Std-specific firmware already loaded.\n");
  848. goto skip_std_specific;
  849. }
  850. /* Reloading std-specific firmware forces a SCODE update */
  851. priv->cur_fw.scode_table = 0;
  852. /* Load the standard firmware */
  853. rc = load_firmware(fe, new_fw.type, &new_fw.id);
  854. if (rc < 0)
  855. goto fail;
  856. skip_std_specific:
  857. if (priv->cur_fw.scode_table == new_fw.scode_table &&
  858. priv->cur_fw.scode_nr == new_fw.scode_nr) {
  859. dprintk(1, "SCODE firmware already loaded.\n");
  860. goto check_device;
  861. }
  862. /* Load SCODE firmware, if exists */
  863. rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
  864. new_fw.int_freq, new_fw.scode_nr);
  865. if (rc != 0)
  866. dprintk(1, "load scode failed %d\n", rc);
  867. check_device:
  868. rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
  869. if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
  870. &fw_minor) != 0) {
  871. printk(KERN_ERR "Unable to read tuner registers.\n");
  872. goto fail;
  873. }
  874. dprintk(1, "Device is Xceive %d version %d.%d, "
  875. "firmware version %d.%d\n",
  876. hwmodel, hw_major, hw_minor, fw_major, fw_minor);
  877. /* Check firmware version against what we downloaded. */
  878. if (priv->firm_version != ((fw_major << 8) | fw_minor)) {
  879. printk(KERN_WARNING
  880. "Incorrect readback of firmware version %d.%d.\n",
  881. fw_major, fw_minor);
  882. goto fail;
  883. }
  884. /* Check that the tuner hardware model remains consistent over time. */
  885. if (priv->hwmodel == 0 &&
  886. (hwmodel == XC_PRODUCT_ID_XC4000 ||
  887. hwmodel == XC_PRODUCT_ID_XC4100)) {
  888. priv->hwmodel = hwmodel;
  889. priv->hwvers = (hw_major << 8) | hw_minor;
  890. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  891. priv->hwvers != ((hw_major << 8) | hw_minor)) {
  892. printk(KERN_WARNING
  893. "Read invalid device hardware information - tuner "
  894. "hung?\n");
  895. goto fail;
  896. }
  897. memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
  898. /*
  899. * By setting BASE in cur_fw.type only after successfully loading all
  900. * firmwares, we can:
  901. * 1. Identify that BASE firmware with type=0 has been loaded;
  902. * 2. Tell whether BASE firmware was just changed the next time through.
  903. */
  904. priv->cur_fw.type |= BASE;
  905. return 0;
  906. fail:
  907. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  908. if (!is_retry) {
  909. msleep(50);
  910. is_retry = 1;
  911. dprintk(1, "Retrying firmware load\n");
  912. goto retry;
  913. }
  914. if (rc == -ENOENT)
  915. rc = -EINVAL;
  916. return rc;
  917. }
  918. static void xc_debug_dump(struct xc4000_priv *priv)
  919. {
  920. u16 adc_envelope;
  921. u32 freq_error_hz = 0;
  922. u16 lock_status;
  923. u32 hsync_freq_hz = 0;
  924. u16 frame_lines;
  925. u16 quality;
  926. u8 hw_majorversion = 0, hw_minorversion = 0;
  927. u8 fw_majorversion = 0, fw_minorversion = 0;
  928. xc_get_adc_envelope(priv, &adc_envelope);
  929. dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
  930. xc_get_frequency_error(priv, &freq_error_hz);
  931. dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
  932. xc_get_lock_status(priv, &lock_status);
  933. dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
  934. lock_status);
  935. xc_get_version(priv, &hw_majorversion, &hw_minorversion,
  936. &fw_majorversion, &fw_minorversion);
  937. dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
  938. hw_majorversion, hw_minorversion,
  939. fw_majorversion, fw_minorversion);
  940. if (priv->video_standard < XC4000_DTV6) {
  941. xc_get_hsync_freq(priv, &hsync_freq_hz);
  942. dprintk(1, "*** Horizontal sync frequency = %d Hz\n",
  943. hsync_freq_hz);
  944. xc_get_frame_lines(priv, &frame_lines);
  945. dprintk(1, "*** Frame lines = %d\n", frame_lines);
  946. }
  947. xc_get_quality(priv, &quality);
  948. dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
  949. }
  950. static int xc4000_set_params(struct dvb_frontend *fe,
  951. struct dvb_frontend_parameters *params)
  952. {
  953. struct xc4000_priv *priv = fe->tuner_priv;
  954. unsigned int type;
  955. int ret = -EREMOTEIO;
  956. dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
  957. mutex_lock(&priv->lock);
  958. if (fe->ops.info.type == FE_ATSC) {
  959. dprintk(1, "%s() ATSC\n", __func__);
  960. switch (params->u.vsb.modulation) {
  961. case VSB_8:
  962. case VSB_16:
  963. dprintk(1, "%s() VSB modulation\n", __func__);
  964. priv->rf_mode = XC_RF_MODE_AIR;
  965. priv->freq_hz = params->frequency - 1750000;
  966. priv->bandwidth = BANDWIDTH_6_MHZ;
  967. priv->video_standard = XC4000_DTV6;
  968. type = DTV6;
  969. break;
  970. case QAM_64:
  971. case QAM_256:
  972. case QAM_AUTO:
  973. dprintk(1, "%s() QAM modulation\n", __func__);
  974. priv->rf_mode = XC_RF_MODE_CABLE;
  975. priv->freq_hz = params->frequency - 1750000;
  976. priv->bandwidth = BANDWIDTH_6_MHZ;
  977. priv->video_standard = XC4000_DTV6;
  978. type = DTV6;
  979. break;
  980. default:
  981. ret = -EINVAL;
  982. goto fail;
  983. }
  984. } else if (fe->ops.info.type == FE_OFDM) {
  985. dprintk(1, "%s() OFDM\n", __func__);
  986. switch (params->u.ofdm.bandwidth) {
  987. case BANDWIDTH_6_MHZ:
  988. priv->bandwidth = BANDWIDTH_6_MHZ;
  989. priv->video_standard = XC4000_DTV6;
  990. priv->freq_hz = params->frequency - 1750000;
  991. type = DTV6;
  992. break;
  993. case BANDWIDTH_7_MHZ:
  994. priv->bandwidth = BANDWIDTH_7_MHZ;
  995. priv->video_standard = XC4000_DTV7;
  996. priv->freq_hz = params->frequency - 2250000;
  997. type = DTV7;
  998. break;
  999. case BANDWIDTH_8_MHZ:
  1000. priv->bandwidth = BANDWIDTH_8_MHZ;
  1001. priv->video_standard = XC4000_DTV8;
  1002. priv->freq_hz = params->frequency - 2750000;
  1003. type = DTV8;
  1004. break;
  1005. case BANDWIDTH_AUTO:
  1006. if (params->frequency < 400000000) {
  1007. priv->bandwidth = BANDWIDTH_7_MHZ;
  1008. priv->freq_hz = params->frequency - 2250000;
  1009. } else {
  1010. priv->bandwidth = BANDWIDTH_8_MHZ;
  1011. priv->freq_hz = params->frequency - 2750000;
  1012. }
  1013. priv->video_standard = XC4000_DTV7_8;
  1014. type = DTV78;
  1015. break;
  1016. default:
  1017. printk(KERN_ERR "xc4000 bandwidth not set!\n");
  1018. ret = -EINVAL;
  1019. goto fail;
  1020. }
  1021. priv->rf_mode = XC_RF_MODE_AIR;
  1022. } else {
  1023. printk(KERN_ERR "xc4000 modulation type not supported!\n");
  1024. ret = -EINVAL;
  1025. goto fail;
  1026. }
  1027. dprintk(1, "%s() frequency=%d (compensated)\n",
  1028. __func__, priv->freq_hz);
  1029. /* Make sure the correct firmware type is loaded */
  1030. if (check_firmware(fe, type, 0, priv->if_khz) != 0)
  1031. goto fail;
  1032. ret = xc_set_signal_source(priv, priv->rf_mode);
  1033. if (ret != 0) {
  1034. printk(KERN_ERR "xc4000: xc_set_signal_source(%d) failed\n",
  1035. priv->rf_mode);
  1036. goto fail;
  1037. } else {
  1038. u16 video_mode, audio_mode;
  1039. video_mode = xc4000_standard[priv->video_standard].video_mode;
  1040. audio_mode = xc4000_standard[priv->video_standard].audio_mode;
  1041. if (type == DTV6 && priv->firm_version != 0x0102)
  1042. video_mode |= 0x0001;
  1043. ret = xc_set_tv_standard(priv, video_mode, audio_mode);
  1044. if (ret != 0) {
  1045. printk(KERN_ERR "xc4000: xc_set_tv_standard failed\n");
  1046. /* DJH - do not return when it fails... */
  1047. /* goto fail; */
  1048. }
  1049. }
  1050. if (xc_write_reg(priv, XREG_D_CODE, 0) == 0)
  1051. ret = 0;
  1052. if (priv->dvb_amplitude != 0) {
  1053. if (xc_write_reg(priv, XREG_AMPLITUDE,
  1054. (priv->firm_version != 0x0102 ||
  1055. priv->dvb_amplitude != 134 ?
  1056. priv->dvb_amplitude : 132)) != 0)
  1057. ret = -EREMOTEIO;
  1058. }
  1059. if (priv->set_smoothedcvbs != 0) {
  1060. if (xc_write_reg(priv, XREG_SMOOTHEDCVBS, 1) != 0)
  1061. ret = -EREMOTEIO;
  1062. }
  1063. if (ret != 0) {
  1064. printk(KERN_ERR "xc4000: setting registers failed\n");
  1065. /* goto fail; */
  1066. }
  1067. xc_tune_channel(priv, priv->freq_hz);
  1068. ret = 0;
  1069. fail:
  1070. mutex_unlock(&priv->lock);
  1071. return ret;
  1072. }
  1073. static int xc4000_set_analog_params(struct dvb_frontend *fe,
  1074. struct analog_parameters *params)
  1075. {
  1076. struct xc4000_priv *priv = fe->tuner_priv;
  1077. unsigned int type = 0;
  1078. int ret = -EREMOTEIO;
  1079. if (params->mode == V4L2_TUNER_RADIO) {
  1080. dprintk(1, "%s() frequency=%d (in units of 62.5Hz)\n",
  1081. __func__, params->frequency);
  1082. mutex_lock(&priv->lock);
  1083. params->std = 0;
  1084. priv->freq_hz = params->frequency * 125L / 2;
  1085. if (audio_std & XC4000_AUDIO_STD_INPUT1) {
  1086. priv->video_standard = XC4000_FM_Radio_INPUT1;
  1087. type = FM | INPUT1;
  1088. } else {
  1089. priv->video_standard = XC4000_FM_Radio_INPUT2;
  1090. type = FM | INPUT2;
  1091. }
  1092. goto tune_channel;
  1093. }
  1094. dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
  1095. __func__, params->frequency);
  1096. mutex_lock(&priv->lock);
  1097. /* params->frequency is in units of 62.5khz */
  1098. priv->freq_hz = params->frequency * 62500;
  1099. params->std &= V4L2_STD_ALL;
  1100. /* if std is not defined, choose one */
  1101. if (!params->std)
  1102. params->std = V4L2_STD_PAL_BG;
  1103. if (audio_std & XC4000_AUDIO_STD_MONO)
  1104. type = MONO;
  1105. if (params->std & V4L2_STD_MN) {
  1106. params->std = V4L2_STD_MN;
  1107. if (audio_std & XC4000_AUDIO_STD_MONO) {
  1108. priv->video_standard = XC4000_MN_NTSC_PAL_Mono;
  1109. } else if (audio_std & XC4000_AUDIO_STD_A2) {
  1110. params->std |= V4L2_STD_A2;
  1111. priv->video_standard = XC4000_MN_NTSC_PAL_A2;
  1112. } else {
  1113. params->std |= V4L2_STD_BTSC;
  1114. priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
  1115. }
  1116. goto tune_channel;
  1117. }
  1118. if (params->std & V4L2_STD_PAL_BG) {
  1119. params->std = V4L2_STD_PAL_BG;
  1120. if (audio_std & XC4000_AUDIO_STD_MONO) {
  1121. priv->video_standard = XC4000_BG_PAL_MONO;
  1122. } else if (!(audio_std & XC4000_AUDIO_STD_A2)) {
  1123. if (!(audio_std & XC4000_AUDIO_STD_B)) {
  1124. params->std |= V4L2_STD_NICAM_A;
  1125. priv->video_standard = XC4000_BG_PAL_NICAM;
  1126. } else {
  1127. params->std |= V4L2_STD_NICAM_B;
  1128. priv->video_standard = XC4000_BG_PAL_NICAM;
  1129. }
  1130. } else {
  1131. if (!(audio_std & XC4000_AUDIO_STD_B)) {
  1132. params->std |= V4L2_STD_A2_A;
  1133. priv->video_standard = XC4000_BG_PAL_A2;
  1134. } else {
  1135. params->std |= V4L2_STD_A2_B;
  1136. priv->video_standard = XC4000_BG_PAL_A2;
  1137. }
  1138. }
  1139. goto tune_channel;
  1140. }
  1141. if (params->std & V4L2_STD_PAL_I) {
  1142. /* default to NICAM audio standard */
  1143. params->std = V4L2_STD_PAL_I | V4L2_STD_NICAM;
  1144. if (audio_std & XC4000_AUDIO_STD_MONO)
  1145. priv->video_standard = XC4000_I_PAL_NICAM_MONO;
  1146. else
  1147. priv->video_standard = XC4000_I_PAL_NICAM;
  1148. goto tune_channel;
  1149. }
  1150. if (params->std & V4L2_STD_PAL_DK) {
  1151. params->std = V4L2_STD_PAL_DK;
  1152. if (audio_std & XC4000_AUDIO_STD_MONO) {
  1153. priv->video_standard = XC4000_DK_PAL_MONO;
  1154. } else if (audio_std & XC4000_AUDIO_STD_A2) {
  1155. params->std |= V4L2_STD_A2;
  1156. priv->video_standard = XC4000_DK_PAL_A2;
  1157. } else {
  1158. params->std |= V4L2_STD_NICAM;
  1159. priv->video_standard = XC4000_DK_PAL_NICAM;
  1160. }
  1161. goto tune_channel;
  1162. }
  1163. if (params->std & V4L2_STD_SECAM_DK) {
  1164. /* default to A2 audio standard */
  1165. params->std = V4L2_STD_SECAM_DK | V4L2_STD_A2;
  1166. if (audio_std & XC4000_AUDIO_STD_L) {
  1167. type = 0;
  1168. priv->video_standard = XC4000_DK_SECAM_NICAM;
  1169. } else if (audio_std & XC4000_AUDIO_STD_MONO) {
  1170. priv->video_standard = XC4000_DK_SECAM_A2MONO;
  1171. } else if (audio_std & XC4000_AUDIO_STD_K3) {
  1172. params->std |= V4L2_STD_SECAM_K3;
  1173. priv->video_standard = XC4000_DK_SECAM_A2LDK3;
  1174. } else {
  1175. priv->video_standard = XC4000_DK_SECAM_A2DK1;
  1176. }
  1177. goto tune_channel;
  1178. }
  1179. if (params->std & V4L2_STD_SECAM_L) {
  1180. /* default to NICAM audio standard */
  1181. type = 0;
  1182. params->std = V4L2_STD_SECAM_L | V4L2_STD_NICAM;
  1183. priv->video_standard = XC4000_L_SECAM_NICAM;
  1184. goto tune_channel;
  1185. }
  1186. if (params->std & V4L2_STD_SECAM_LC) {
  1187. /* default to NICAM audio standard */
  1188. type = 0;
  1189. params->std = V4L2_STD_SECAM_LC | V4L2_STD_NICAM;
  1190. priv->video_standard = XC4000_LC_SECAM_NICAM;
  1191. goto tune_channel;
  1192. }
  1193. tune_channel:
  1194. /* FIXME: it could be air. */
  1195. priv->rf_mode = XC_RF_MODE_CABLE;
  1196. if (check_firmware(fe, type, params->std,
  1197. xc4000_standard[priv->video_standard].int_freq) != 0)
  1198. goto fail;
  1199. ret = xc_set_signal_source(priv, priv->rf_mode);
  1200. if (ret != 0) {
  1201. printk(KERN_ERR
  1202. "xc4000: xc_set_signal_source(%d) failed\n",
  1203. priv->rf_mode);
  1204. goto fail;
  1205. } else {
  1206. u16 video_mode, audio_mode;
  1207. video_mode = xc4000_standard[priv->video_standard].video_mode;
  1208. audio_mode = xc4000_standard[priv->video_standard].audio_mode;
  1209. if (priv->video_standard < XC4000_BG_PAL_A2) {
  1210. if (type & NOGD)
  1211. video_mode &= 0xFF7F;
  1212. } else if (priv->video_standard < XC4000_I_PAL_NICAM) {
  1213. if (priv->firm_version == 0x0102)
  1214. video_mode &= 0xFEFF;
  1215. if (audio_std & XC4000_AUDIO_STD_B)
  1216. video_mode |= 0x0080;
  1217. }
  1218. ret = xc_set_tv_standard(priv, video_mode, audio_mode);
  1219. if (ret != 0) {
  1220. printk(KERN_ERR "xc4000: xc_set_tv_standard failed\n");
  1221. goto fail;
  1222. }
  1223. }
  1224. if (xc_write_reg(priv, XREG_D_CODE, 0) == 0)
  1225. ret = 0;
  1226. if (xc_write_reg(priv, XREG_AMPLITUDE, 1) != 0)
  1227. ret = -EREMOTEIO;
  1228. if (priv->set_smoothedcvbs != 0) {
  1229. if (xc_write_reg(priv, XREG_SMOOTHEDCVBS, 1) != 0)
  1230. ret = -EREMOTEIO;
  1231. }
  1232. if (ret != 0) {
  1233. printk(KERN_ERR "xc4000: setting registers failed\n");
  1234. goto fail;
  1235. }
  1236. xc_tune_channel(priv, priv->freq_hz);
  1237. ret = 0;
  1238. fail:
  1239. mutex_unlock(&priv->lock);
  1240. return ret;
  1241. }
  1242. static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
  1243. {
  1244. struct xc4000_priv *priv = fe->tuner_priv;
  1245. *freq = priv->freq_hz;
  1246. if (debug) {
  1247. mutex_lock(&priv->lock);
  1248. if ((priv->cur_fw.type
  1249. & (BASE | FM | DTV6 | DTV7 | DTV78 | DTV8)) == BASE) {
  1250. u16 snr = 0;
  1251. if (xc4000_readreg(priv, XREG_SNR, &snr) == 0) {
  1252. mutex_unlock(&priv->lock);
  1253. dprintk(1, "%s() freq = %u, SNR = %d\n",
  1254. __func__, *freq, snr);
  1255. return 0;
  1256. }
  1257. }
  1258. mutex_unlock(&priv->lock);
  1259. }
  1260. dprintk(1, "%s()\n", __func__);
  1261. return 0;
  1262. }
  1263. static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
  1264. {
  1265. struct xc4000_priv *priv = fe->tuner_priv;
  1266. dprintk(1, "%s()\n", __func__);
  1267. *bw = priv->bandwidth;
  1268. return 0;
  1269. }
  1270. static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
  1271. {
  1272. struct xc4000_priv *priv = fe->tuner_priv;
  1273. u16 lock_status = 0;
  1274. mutex_lock(&priv->lock);
  1275. if (priv->cur_fw.type & BASE)
  1276. xc_get_lock_status(priv, &lock_status);
  1277. *status = (lock_status == 1 ?
  1278. TUNER_STATUS_LOCKED | TUNER_STATUS_STEREO : 0);
  1279. if (priv->cur_fw.type & (DTV6 | DTV7 | DTV78 | DTV8))
  1280. *status &= (~TUNER_STATUS_STEREO);
  1281. mutex_unlock(&priv->lock);
  1282. dprintk(2, "%s() lock_status = %d\n", __func__, lock_status);
  1283. return 0;
  1284. }
  1285. static int xc4000_sleep(struct dvb_frontend *fe)
  1286. {
  1287. struct xc4000_priv *priv = fe->tuner_priv;
  1288. int ret = 0;
  1289. dprintk(1, "%s()\n", __func__);
  1290. mutex_lock(&priv->lock);
  1291. /* Avoid firmware reload on slow devices */
  1292. if ((no_poweroff == 2 ||
  1293. (no_poweroff == 0 && priv->default_pm != 0)) &&
  1294. (priv->cur_fw.type & BASE) != 0) {
  1295. /* force reset and firmware reload */
  1296. priv->cur_fw.type = XC_POWERED_DOWN;
  1297. if (xc_write_reg(priv, XREG_POWER_DOWN, 0) != 0) {
  1298. printk(KERN_ERR
  1299. "xc4000: %s() unable to shutdown tuner\n",
  1300. __func__);
  1301. ret = -EREMOTEIO;
  1302. }
  1303. msleep(20);
  1304. }
  1305. mutex_unlock(&priv->lock);
  1306. return ret;
  1307. }
  1308. static int xc4000_init(struct dvb_frontend *fe)
  1309. {
  1310. dprintk(1, "%s()\n", __func__);
  1311. return 0;
  1312. }
  1313. static int xc4000_release(struct dvb_frontend *fe)
  1314. {
  1315. struct xc4000_priv *priv = fe->tuner_priv;
  1316. dprintk(1, "%s()\n", __func__);
  1317. mutex_lock(&xc4000_list_mutex);
  1318. if (priv)
  1319. hybrid_tuner_release_state(priv);
  1320. mutex_unlock(&xc4000_list_mutex);
  1321. fe->tuner_priv = NULL;
  1322. return 0;
  1323. }
  1324. static const struct dvb_tuner_ops xc4000_tuner_ops = {
  1325. .info = {
  1326. .name = "Xceive XC4000",
  1327. .frequency_min = 1000000,
  1328. .frequency_max = 1023000000,
  1329. .frequency_step = 50000,
  1330. },
  1331. .release = xc4000_release,
  1332. .init = xc4000_init,
  1333. .sleep = xc4000_sleep,
  1334. .set_params = xc4000_set_params,
  1335. .set_analog_params = xc4000_set_analog_params,
  1336. .get_frequency = xc4000_get_frequency,
  1337. .get_bandwidth = xc4000_get_bandwidth,
  1338. .get_status = xc4000_get_status
  1339. };
  1340. struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
  1341. struct i2c_adapter *i2c,
  1342. struct xc4000_config *cfg)
  1343. {
  1344. struct xc4000_priv *priv = NULL;
  1345. int instance;
  1346. u16 id = 0;
  1347. dprintk(1, "%s(%d-%04x)\n", __func__,
  1348. i2c ? i2c_adapter_id(i2c) : -1,
  1349. cfg ? cfg->i2c_address : -1);
  1350. mutex_lock(&xc4000_list_mutex);
  1351. instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
  1352. hybrid_tuner_instance_list,
  1353. i2c, cfg->i2c_address, "xc4000");
  1354. switch (instance) {
  1355. case 0:
  1356. goto fail;
  1357. break;
  1358. case 1:
  1359. /* new tuner instance */
  1360. priv->bandwidth = BANDWIDTH_6_MHZ;
  1361. /* set default configuration */
  1362. priv->if_khz = 4560;
  1363. priv->default_pm = 0;
  1364. priv->dvb_amplitude = 134;
  1365. priv->set_smoothedcvbs = 1;
  1366. mutex_init(&priv->lock);
  1367. fe->tuner_priv = priv;
  1368. break;
  1369. default:
  1370. /* existing tuner instance */
  1371. fe->tuner_priv = priv;
  1372. break;
  1373. }
  1374. if (cfg->if_khz != 0) {
  1375. /* copy configuration if provided by the caller */
  1376. priv->if_khz = cfg->if_khz;
  1377. priv->default_pm = cfg->default_pm;
  1378. priv->dvb_amplitude = cfg->dvb_amplitude;
  1379. priv->set_smoothedcvbs = cfg->set_smoothedcvbs;
  1380. }
  1381. /* Check if firmware has been loaded. It is possible that another
  1382. instance of the driver has loaded the firmware.
  1383. */
  1384. if (instance == 1) {
  1385. if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != 0)
  1386. goto fail;
  1387. } else {
  1388. id = ((priv->cur_fw.type & BASE) != 0 ?
  1389. priv->hwmodel : XC_PRODUCT_ID_FW_NOT_LOADED);
  1390. }
  1391. switch (id) {
  1392. case XC_PRODUCT_ID_XC4000:
  1393. case XC_PRODUCT_ID_XC4100:
  1394. printk(KERN_INFO
  1395. "xc4000: Successfully identified at address 0x%02x\n",
  1396. cfg->i2c_address);
  1397. printk(KERN_INFO
  1398. "xc4000: Firmware has been loaded previously\n");
  1399. break;
  1400. case XC_PRODUCT_ID_FW_NOT_LOADED:
  1401. printk(KERN_INFO
  1402. "xc4000: Successfully identified at address 0x%02x\n",
  1403. cfg->i2c_address);
  1404. printk(KERN_INFO
  1405. "xc4000: Firmware has not been loaded previously\n");
  1406. break;
  1407. default:
  1408. printk(KERN_ERR
  1409. "xc4000: Device not found at addr 0x%02x (0x%x)\n",
  1410. cfg->i2c_address, id);
  1411. goto fail;
  1412. }
  1413. mutex_unlock(&xc4000_list_mutex);
  1414. memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
  1415. sizeof(struct dvb_tuner_ops));
  1416. if (instance == 1) {
  1417. int ret;
  1418. mutex_lock(&priv->lock);
  1419. ret = xc4000_fwupload(fe);
  1420. mutex_unlock(&priv->lock);
  1421. if (ret != 0)
  1422. goto fail2;
  1423. }
  1424. return fe;
  1425. fail:
  1426. mutex_unlock(&xc4000_list_mutex);
  1427. fail2:
  1428. xc4000_release(fe);
  1429. return NULL;
  1430. }
  1431. EXPORT_SYMBOL(xc4000_attach);
  1432. MODULE_AUTHOR("Steven Toth, Davide Ferri");
  1433. MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
  1434. MODULE_LICENSE("GPL");