xc4000.c 38 KB

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