xc4000.c 40 KB

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