xc4000.c 40 KB

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