xc4000.c 44 KB

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