xc4000.c 43 KB

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