r820t.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  1. /*
  2. * Rafael Micro R820T driver
  3. *
  4. * Copyright (C) 2013 Mauro Carvalho Chehab
  5. *
  6. * This driver was written from scratch, based on an existing driver
  7. * that it is part of rtl-sdr git tree, released under GPLv2:
  8. * https://groups.google.com/forum/#!topic/ultra-cheap-sdr/Y3rBEOFtHug
  9. * https://github.com/n1gp/gr-baz
  10. *
  11. * From what I understood from the threads, the original driver was converted
  12. * to userspace from a Realtek tree. I couldn't find the original tree.
  13. * However, the original driver look awkward on my eyes. So, I decided to
  14. * write a new version from it from the scratch, while trying to reproduce
  15. * everything found there.
  16. *
  17. * TODO:
  18. * After locking, the original driver seems to have some routines to
  19. * improve reception. This was not implemented here yet.
  20. *
  21. * RF Gain set/get is not implemented.
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. */
  34. #include <linux/videodev2.h>
  35. #include <linux/mutex.h>
  36. #include <linux/slab.h>
  37. #include <linux/bitrev.h>
  38. #include "tuner-i2c.h"
  39. #include "r820t.h"
  40. /*
  41. * FIXME: I think that there are only 32 registers, but better safe than
  42. * sorry. After finishing the driver, we may review it.
  43. */
  44. #define REG_SHADOW_START 5
  45. #define NUM_REGS 27
  46. #define NUM_IMR 5
  47. #define IMR_TRIAL 9
  48. #define VER_NUM 49
  49. static int debug;
  50. module_param(debug, int, 0644);
  51. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  52. static int no_imr_cal;
  53. module_param(no_imr_cal, int, 0444);
  54. MODULE_PARM_DESC(no_imr_cal, "Disable IMR calibration at module init");
  55. /*
  56. * enums and structures
  57. */
  58. enum xtal_cap_value {
  59. XTAL_LOW_CAP_30P = 0,
  60. XTAL_LOW_CAP_20P,
  61. XTAL_LOW_CAP_10P,
  62. XTAL_LOW_CAP_0P,
  63. XTAL_HIGH_CAP_0P
  64. };
  65. struct r820t_sect_type {
  66. u8 phase_y;
  67. u8 gain_x;
  68. u16 value;
  69. };
  70. struct r820t_priv {
  71. struct list_head hybrid_tuner_instance_list;
  72. const struct r820t_config *cfg;
  73. struct tuner_i2c_props i2c_props;
  74. struct mutex lock;
  75. u8 regs[NUM_REGS];
  76. u8 buf[NUM_REGS + 1];
  77. enum xtal_cap_value xtal_cap_sel;
  78. u16 pll; /* kHz */
  79. u32 int_freq;
  80. u8 fil_cal_code;
  81. bool imr_done;
  82. bool has_lock;
  83. bool init_done;
  84. struct r820t_sect_type imr_data[NUM_IMR];
  85. /* Store current mode */
  86. u32 delsys;
  87. enum v4l2_tuner_type type;
  88. v4l2_std_id std;
  89. u32 bw; /* in MHz */
  90. };
  91. struct r820t_freq_range {
  92. u32 freq;
  93. u8 open_d;
  94. u8 rf_mux_ploy;
  95. u8 tf_c;
  96. u8 xtal_cap20p;
  97. u8 xtal_cap10p;
  98. u8 xtal_cap0p;
  99. u8 imr_mem; /* Not used, currently */
  100. };
  101. #define VCO_POWER_REF 0x02
  102. #define DIP_FREQ 32000000
  103. /*
  104. * Static constants
  105. */
  106. static LIST_HEAD(hybrid_tuner_instance_list);
  107. static DEFINE_MUTEX(r820t_list_mutex);
  108. /* Those initial values start from REG_SHADOW_START */
  109. static const u8 r820t_init_array[NUM_REGS] = {
  110. 0x83, 0x32, 0x75, /* 05 to 07 */
  111. 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */
  112. 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */
  113. 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */
  114. 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */
  115. 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */
  116. 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */
  117. };
  118. /* Tuner frequency ranges */
  119. static const struct r820t_freq_range freq_ranges[] = {
  120. {
  121. .freq = 0,
  122. .open_d = 0x08, /* low */
  123. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  124. .tf_c = 0xdf, /* R27[7:0] band2,band0 */
  125. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  126. .xtal_cap10p = 0x01,
  127. .xtal_cap0p = 0x00,
  128. .imr_mem = 0,
  129. }, {
  130. .freq = 50, /* Start freq, in MHz */
  131. .open_d = 0x08, /* low */
  132. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  133. .tf_c = 0xbe, /* R27[7:0] band4,band1 */
  134. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  135. .xtal_cap10p = 0x01,
  136. .xtal_cap0p = 0x00,
  137. .imr_mem = 0,
  138. }, {
  139. .freq = 55, /* Start freq, in MHz */
  140. .open_d = 0x08, /* low */
  141. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  142. .tf_c = 0x8b, /* R27[7:0] band7,band4 */
  143. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  144. .xtal_cap10p = 0x01,
  145. .xtal_cap0p = 0x00,
  146. .imr_mem = 0,
  147. }, {
  148. .freq = 60, /* Start freq, in MHz */
  149. .open_d = 0x08, /* low */
  150. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  151. .tf_c = 0x7b, /* R27[7:0] band8,band4 */
  152. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  153. .xtal_cap10p = 0x01,
  154. .xtal_cap0p = 0x00,
  155. .imr_mem = 0,
  156. }, {
  157. .freq = 65, /* Start freq, in MHz */
  158. .open_d = 0x08, /* low */
  159. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  160. .tf_c = 0x69, /* R27[7:0] band9,band6 */
  161. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  162. .xtal_cap10p = 0x01,
  163. .xtal_cap0p = 0x00,
  164. .imr_mem = 0,
  165. }, {
  166. .freq = 70, /* Start freq, in MHz */
  167. .open_d = 0x08, /* low */
  168. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  169. .tf_c = 0x58, /* R27[7:0] band10,band7 */
  170. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  171. .xtal_cap10p = 0x01,
  172. .xtal_cap0p = 0x00,
  173. .imr_mem = 0,
  174. }, {
  175. .freq = 75, /* Start freq, in MHz */
  176. .open_d = 0x00, /* high */
  177. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  178. .tf_c = 0x44, /* R27[7:0] band11,band11 */
  179. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  180. .xtal_cap10p = 0x01,
  181. .xtal_cap0p = 0x00,
  182. .imr_mem = 0,
  183. }, {
  184. .freq = 80, /* Start freq, in MHz */
  185. .open_d = 0x00, /* high */
  186. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  187. .tf_c = 0x44, /* R27[7:0] band11,band11 */
  188. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  189. .xtal_cap10p = 0x01,
  190. .xtal_cap0p = 0x00,
  191. .imr_mem = 0,
  192. }, {
  193. .freq = 90, /* Start freq, in MHz */
  194. .open_d = 0x00, /* high */
  195. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  196. .tf_c = 0x34, /* R27[7:0] band12,band11 */
  197. .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
  198. .xtal_cap10p = 0x01,
  199. .xtal_cap0p = 0x00,
  200. .imr_mem = 0,
  201. }, {
  202. .freq = 100, /* Start freq, in MHz */
  203. .open_d = 0x00, /* high */
  204. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  205. .tf_c = 0x34, /* R27[7:0] band12,band11 */
  206. .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
  207. .xtal_cap10p = 0x01,
  208. .xtal_cap0p = 0x00,
  209. .imr_mem = 0,
  210. }, {
  211. .freq = 110, /* Start freq, in MHz */
  212. .open_d = 0x00, /* high */
  213. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  214. .tf_c = 0x24, /* R27[7:0] band13,band11 */
  215. .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
  216. .xtal_cap10p = 0x01,
  217. .xtal_cap0p = 0x00,
  218. .imr_mem = 1,
  219. }, {
  220. .freq = 120, /* Start freq, in MHz */
  221. .open_d = 0x00, /* high */
  222. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  223. .tf_c = 0x24, /* R27[7:0] band13,band11 */
  224. .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
  225. .xtal_cap10p = 0x01,
  226. .xtal_cap0p = 0x00,
  227. .imr_mem = 1,
  228. }, {
  229. .freq = 140, /* Start freq, in MHz */
  230. .open_d = 0x00, /* high */
  231. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  232. .tf_c = 0x14, /* R27[7:0] band14,band11 */
  233. .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
  234. .xtal_cap10p = 0x01,
  235. .xtal_cap0p = 0x00,
  236. .imr_mem = 1,
  237. }, {
  238. .freq = 180, /* Start freq, in MHz */
  239. .open_d = 0x00, /* high */
  240. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  241. .tf_c = 0x13, /* R27[7:0] band14,band12 */
  242. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  243. .xtal_cap10p = 0x00,
  244. .xtal_cap0p = 0x00,
  245. .imr_mem = 1,
  246. }, {
  247. .freq = 220, /* Start freq, in MHz */
  248. .open_d = 0x00, /* high */
  249. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  250. .tf_c = 0x13, /* R27[7:0] band14,band12 */
  251. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  252. .xtal_cap10p = 0x00,
  253. .xtal_cap0p = 0x00,
  254. .imr_mem = 2,
  255. }, {
  256. .freq = 250, /* Start freq, in MHz */
  257. .open_d = 0x00, /* high */
  258. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  259. .tf_c = 0x11, /* R27[7:0] highest,highest */
  260. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  261. .xtal_cap10p = 0x00,
  262. .xtal_cap0p = 0x00,
  263. .imr_mem = 2,
  264. }, {
  265. .freq = 280, /* Start freq, in MHz */
  266. .open_d = 0x00, /* high */
  267. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  268. .tf_c = 0x00, /* R27[7:0] highest,highest */
  269. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  270. .xtal_cap10p = 0x00,
  271. .xtal_cap0p = 0x00,
  272. .imr_mem = 2,
  273. }, {
  274. .freq = 310, /* Start freq, in MHz */
  275. .open_d = 0x00, /* high */
  276. .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
  277. .tf_c = 0x00, /* R27[7:0] highest,highest */
  278. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  279. .xtal_cap10p = 0x00,
  280. .xtal_cap0p = 0x00,
  281. .imr_mem = 2,
  282. }, {
  283. .freq = 450, /* Start freq, in MHz */
  284. .open_d = 0x00, /* high */
  285. .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
  286. .tf_c = 0x00, /* R27[7:0] highest,highest */
  287. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  288. .xtal_cap10p = 0x00,
  289. .xtal_cap0p = 0x00,
  290. .imr_mem = 3,
  291. }, {
  292. .freq = 588, /* Start freq, in MHz */
  293. .open_d = 0x00, /* high */
  294. .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
  295. .tf_c = 0x00, /* R27[7:0] highest,highest */
  296. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  297. .xtal_cap10p = 0x00,
  298. .xtal_cap0p = 0x00,
  299. .imr_mem = 3,
  300. }, {
  301. .freq = 650, /* Start freq, in MHz */
  302. .open_d = 0x00, /* high */
  303. .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
  304. .tf_c = 0x00, /* R27[7:0] highest,highest */
  305. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  306. .xtal_cap10p = 0x00,
  307. .xtal_cap0p = 0x00,
  308. .imr_mem = 4,
  309. }
  310. };
  311. static int r820t_xtal_capacitor[][2] = {
  312. { 0x0b, XTAL_LOW_CAP_30P },
  313. { 0x02, XTAL_LOW_CAP_20P },
  314. { 0x01, XTAL_LOW_CAP_10P },
  315. { 0x00, XTAL_LOW_CAP_0P },
  316. { 0x10, XTAL_HIGH_CAP_0P },
  317. };
  318. /*
  319. * I2C read/write code and shadow registers logic
  320. */
  321. static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val,
  322. int len)
  323. {
  324. int r = reg - REG_SHADOW_START;
  325. if (r < 0) {
  326. len += r;
  327. r = 0;
  328. }
  329. if (len <= 0)
  330. return;
  331. if (len > NUM_REGS - r)
  332. len = NUM_REGS - r;
  333. tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
  334. __func__, r + REG_SHADOW_START, len, len, val);
  335. memcpy(&priv->regs[r], val, len);
  336. }
  337. static int r820t_write(struct r820t_priv *priv, u8 reg, const u8 *val,
  338. int len)
  339. {
  340. int rc, size, pos = 0;
  341. /* Store the shadow registers */
  342. shadow_store(priv, reg, val, len);
  343. do {
  344. if (len > priv->cfg->max_i2c_msg_len - 1)
  345. size = priv->cfg->max_i2c_msg_len - 1;
  346. else
  347. size = len;
  348. /* Fill I2C buffer */
  349. priv->buf[0] = reg;
  350. memcpy(&priv->buf[1], &val[pos], size);
  351. rc = tuner_i2c_xfer_send(&priv->i2c_props, priv->buf, size + 1);
  352. if (rc != size + 1) {
  353. tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
  354. __func__, rc, reg, size, size, &priv->buf[1]);
  355. if (rc < 0)
  356. return rc;
  357. return -EREMOTEIO;
  358. }
  359. tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
  360. __func__, reg, size, size, &priv->buf[1]);
  361. reg += size;
  362. len -= size;
  363. pos += size;
  364. } while (len > 0);
  365. return 0;
  366. }
  367. static inline int r820t_write_reg(struct r820t_priv *priv, u8 reg, u8 val)
  368. {
  369. u8 tmp = val; /* work around GCC PR81715 with asan-stack=1 */
  370. return r820t_write(priv, reg, &tmp, 1);
  371. }
  372. static int r820t_read_cache_reg(struct r820t_priv *priv, int reg)
  373. {
  374. reg -= REG_SHADOW_START;
  375. if (reg >= 0 && reg < NUM_REGS)
  376. return priv->regs[reg];
  377. else
  378. return -EINVAL;
  379. }
  380. static inline int r820t_write_reg_mask(struct r820t_priv *priv, u8 reg, u8 val,
  381. u8 bit_mask)
  382. {
  383. u8 tmp = val;
  384. int rc = r820t_read_cache_reg(priv, reg);
  385. if (rc < 0)
  386. return rc;
  387. tmp = (rc & ~bit_mask) | (tmp & bit_mask);
  388. return r820t_write(priv, reg, &tmp, 1);
  389. }
  390. static int r820t_read(struct r820t_priv *priv, u8 reg, u8 *val, int len)
  391. {
  392. int rc, i;
  393. u8 *p = &priv->buf[1];
  394. priv->buf[0] = reg;
  395. rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, priv->buf, 1, p, len);
  396. if (rc != len) {
  397. tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
  398. __func__, rc, reg, len, len, p);
  399. if (rc < 0)
  400. return rc;
  401. return -EREMOTEIO;
  402. }
  403. /* Copy data to the output buffer */
  404. for (i = 0; i < len; i++)
  405. val[i] = bitrev8(p[i]);
  406. tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
  407. __func__, reg, len, len, val);
  408. return 0;
  409. }
  410. /*
  411. * r820t tuning logic
  412. */
  413. static int r820t_set_mux(struct r820t_priv *priv, u32 freq)
  414. {
  415. const struct r820t_freq_range *range;
  416. int i, rc;
  417. u8 val, reg08, reg09;
  418. /* Get the proper frequency range */
  419. freq = freq / 1000000;
  420. for (i = 0; i < ARRAY_SIZE(freq_ranges) - 1; i++) {
  421. if (freq < freq_ranges[i + 1].freq)
  422. break;
  423. }
  424. range = &freq_ranges[i];
  425. tuner_dbg("set r820t range#%d for frequency %d MHz\n", i, freq);
  426. /* Open Drain */
  427. rc = r820t_write_reg_mask(priv, 0x17, range->open_d, 0x08);
  428. if (rc < 0)
  429. return rc;
  430. /* RF_MUX,Polymux */
  431. rc = r820t_write_reg_mask(priv, 0x1a, range->rf_mux_ploy, 0xc3);
  432. if (rc < 0)
  433. return rc;
  434. /* TF BAND */
  435. rc = r820t_write_reg(priv, 0x1b, range->tf_c);
  436. if (rc < 0)
  437. return rc;
  438. /* XTAL CAP & Drive */
  439. switch (priv->xtal_cap_sel) {
  440. case XTAL_LOW_CAP_30P:
  441. case XTAL_LOW_CAP_20P:
  442. val = range->xtal_cap20p | 0x08;
  443. break;
  444. case XTAL_LOW_CAP_10P:
  445. val = range->xtal_cap10p | 0x08;
  446. break;
  447. case XTAL_HIGH_CAP_0P:
  448. val = range->xtal_cap0p | 0x00;
  449. break;
  450. default:
  451. case XTAL_LOW_CAP_0P:
  452. val = range->xtal_cap0p | 0x08;
  453. break;
  454. }
  455. rc = r820t_write_reg_mask(priv, 0x10, val, 0x0b);
  456. if (rc < 0)
  457. return rc;
  458. if (priv->imr_done) {
  459. reg08 = priv->imr_data[range->imr_mem].gain_x;
  460. reg09 = priv->imr_data[range->imr_mem].phase_y;
  461. } else {
  462. reg08 = 0;
  463. reg09 = 0;
  464. }
  465. rc = r820t_write_reg_mask(priv, 0x08, reg08, 0x3f);
  466. if (rc < 0)
  467. return rc;
  468. rc = r820t_write_reg_mask(priv, 0x09, reg09, 0x3f);
  469. return rc;
  470. }
  471. static int r820t_set_pll(struct r820t_priv *priv, enum v4l2_tuner_type type,
  472. u32 freq)
  473. {
  474. u32 vco_freq;
  475. int rc, i;
  476. unsigned sleep_time = 10000;
  477. u32 vco_fra; /* VCO contribution by SDM (kHz) */
  478. u32 vco_min = 1770000;
  479. u32 vco_max = vco_min * 2;
  480. u32 pll_ref;
  481. u16 n_sdm = 2;
  482. u16 sdm = 0;
  483. u8 mix_div = 2;
  484. u8 div_buf = 0;
  485. u8 div_num = 0;
  486. u8 refdiv2 = 0;
  487. u8 ni, si, nint, vco_fine_tune, val;
  488. u8 data[5];
  489. /* Frequency in kHz */
  490. freq = freq / 1000;
  491. pll_ref = priv->cfg->xtal / 1000;
  492. #if 0
  493. /* Doesn't exist on rtl-sdk, and on field tests, caused troubles */
  494. if ((priv->cfg->rafael_chip == CHIP_R620D) ||
  495. (priv->cfg->rafael_chip == CHIP_R828D) ||
  496. (priv->cfg->rafael_chip == CHIP_R828)) {
  497. /* ref set refdiv2, reffreq = Xtal/2 on ATV application */
  498. if (type != V4L2_TUNER_DIGITAL_TV) {
  499. pll_ref /= 2;
  500. refdiv2 = 0x10;
  501. sleep_time = 20000;
  502. }
  503. } else {
  504. if (priv->cfg->xtal > 24000000) {
  505. pll_ref /= 2;
  506. refdiv2 = 0x10;
  507. }
  508. }
  509. #endif
  510. rc = r820t_write_reg_mask(priv, 0x10, refdiv2, 0x10);
  511. if (rc < 0)
  512. return rc;
  513. /* set pll autotune = 128kHz */
  514. rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
  515. if (rc < 0)
  516. return rc;
  517. /* set VCO current = 100 */
  518. rc = r820t_write_reg_mask(priv, 0x12, 0x80, 0xe0);
  519. if (rc < 0)
  520. return rc;
  521. /* Calculate divider */
  522. while (mix_div <= 64) {
  523. if (((freq * mix_div) >= vco_min) &&
  524. ((freq * mix_div) < vco_max)) {
  525. div_buf = mix_div;
  526. while (div_buf > 2) {
  527. div_buf = div_buf >> 1;
  528. div_num++;
  529. }
  530. break;
  531. }
  532. mix_div = mix_div << 1;
  533. }
  534. rc = r820t_read(priv, 0x00, data, sizeof(data));
  535. if (rc < 0)
  536. return rc;
  537. vco_fine_tune = (data[4] & 0x30) >> 4;
  538. tuner_dbg("mix_div=%d div_num=%d vco_fine_tune=%d\n",
  539. mix_div, div_num, vco_fine_tune);
  540. /*
  541. * XXX: R828D/16MHz seems to have always vco_fine_tune=1.
  542. * Due to that, this calculation goes wrong.
  543. */
  544. if (priv->cfg->rafael_chip != CHIP_R828D) {
  545. if (vco_fine_tune > VCO_POWER_REF)
  546. div_num = div_num - 1;
  547. else if (vco_fine_tune < VCO_POWER_REF)
  548. div_num = div_num + 1;
  549. }
  550. rc = r820t_write_reg_mask(priv, 0x10, div_num << 5, 0xe0);
  551. if (rc < 0)
  552. return rc;
  553. vco_freq = freq * mix_div;
  554. nint = vco_freq / (2 * pll_ref);
  555. vco_fra = vco_freq - 2 * pll_ref * nint;
  556. /* boundary spur prevention */
  557. if (vco_fra < pll_ref / 64) {
  558. vco_fra = 0;
  559. } else if (vco_fra > pll_ref * 127 / 64) {
  560. vco_fra = 0;
  561. nint++;
  562. } else if ((vco_fra > pll_ref * 127 / 128) && (vco_fra < pll_ref)) {
  563. vco_fra = pll_ref * 127 / 128;
  564. } else if ((vco_fra > pll_ref) && (vco_fra < pll_ref * 129 / 128)) {
  565. vco_fra = pll_ref * 129 / 128;
  566. }
  567. ni = (nint - 13) / 4;
  568. si = nint - 4 * ni - 13;
  569. rc = r820t_write_reg(priv, 0x14, ni + (si << 6));
  570. if (rc < 0)
  571. return rc;
  572. /* pw_sdm */
  573. if (!vco_fra)
  574. val = 0x08;
  575. else
  576. val = 0x00;
  577. rc = r820t_write_reg_mask(priv, 0x12, val, 0x08);
  578. if (rc < 0)
  579. return rc;
  580. /* sdm calculator */
  581. while (vco_fra > 1) {
  582. if (vco_fra > (2 * pll_ref / n_sdm)) {
  583. sdm = sdm + 32768 / (n_sdm / 2);
  584. vco_fra = vco_fra - 2 * pll_ref / n_sdm;
  585. if (n_sdm >= 0x8000)
  586. break;
  587. }
  588. n_sdm = n_sdm << 1;
  589. }
  590. tuner_dbg("freq %d kHz, pll ref %d%s, sdm=0x%04x\n",
  591. freq, pll_ref, refdiv2 ? " / 2" : "", sdm);
  592. rc = r820t_write_reg(priv, 0x16, sdm >> 8);
  593. if (rc < 0)
  594. return rc;
  595. rc = r820t_write_reg(priv, 0x15, sdm & 0xff);
  596. if (rc < 0)
  597. return rc;
  598. for (i = 0; i < 2; i++) {
  599. usleep_range(sleep_time, sleep_time + 1000);
  600. /* Check if PLL has locked */
  601. rc = r820t_read(priv, 0x00, data, 3);
  602. if (rc < 0)
  603. return rc;
  604. if (data[2] & 0x40)
  605. break;
  606. if (!i) {
  607. /* Didn't lock. Increase VCO current */
  608. rc = r820t_write_reg_mask(priv, 0x12, 0x60, 0xe0);
  609. if (rc < 0)
  610. return rc;
  611. }
  612. }
  613. if (!(data[2] & 0x40)) {
  614. priv->has_lock = false;
  615. return 0;
  616. }
  617. priv->has_lock = true;
  618. tuner_dbg("tuner has lock at frequency %d kHz\n", freq);
  619. /* set pll autotune = 8kHz */
  620. rc = r820t_write_reg_mask(priv, 0x1a, 0x08, 0x08);
  621. return rc;
  622. }
  623. static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq,
  624. enum v4l2_tuner_type type,
  625. v4l2_std_id std,
  626. u32 delsys)
  627. {
  628. int rc;
  629. u8 mixer_top, lna_top, cp_cur, div_buf_cur, lna_vth_l, mixer_vth_l;
  630. u8 air_cable1_in, cable2_in, pre_dect, lna_discharge, filter_cur;
  631. tuner_dbg("adjusting tuner parameters for the standard\n");
  632. switch (delsys) {
  633. case SYS_DVBT:
  634. if ((freq == 506000000) || (freq == 666000000) ||
  635. (freq == 818000000)) {
  636. mixer_top = 0x14; /* mixer top:14 , top-1, low-discharge */
  637. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  638. cp_cur = 0x28; /* 101, 0.2 */
  639. div_buf_cur = 0x20; /* 10, 200u */
  640. } else {
  641. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  642. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  643. cp_cur = 0x38; /* 111, auto */
  644. div_buf_cur = 0x30; /* 11, 150u */
  645. }
  646. lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
  647. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  648. air_cable1_in = 0x00;
  649. cable2_in = 0x00;
  650. pre_dect = 0x40;
  651. lna_discharge = 14;
  652. filter_cur = 0x40; /* 10, low */
  653. break;
  654. case SYS_DVBT2:
  655. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  656. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  657. lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
  658. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  659. air_cable1_in = 0x00;
  660. cable2_in = 0x00;
  661. pre_dect = 0x40;
  662. lna_discharge = 14;
  663. cp_cur = 0x38; /* 111, auto */
  664. div_buf_cur = 0x30; /* 11, 150u */
  665. filter_cur = 0x40; /* 10, low */
  666. break;
  667. case SYS_ISDBT:
  668. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  669. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  670. lna_vth_l = 0x75; /* lna vth 1.04 , vtl 0.84 */
  671. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  672. air_cable1_in = 0x00;
  673. cable2_in = 0x00;
  674. pre_dect = 0x40;
  675. lna_discharge = 14;
  676. cp_cur = 0x38; /* 111, auto */
  677. div_buf_cur = 0x30; /* 11, 150u */
  678. filter_cur = 0x40; /* 10, low */
  679. break;
  680. case SYS_DVBC_ANNEX_A:
  681. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  682. lna_top = 0xe5;
  683. lna_vth_l = 0x62;
  684. mixer_vth_l = 0x75;
  685. air_cable1_in = 0x60;
  686. cable2_in = 0x00;
  687. pre_dect = 0x40;
  688. lna_discharge = 14;
  689. cp_cur = 0x38; /* 111, auto */
  690. div_buf_cur = 0x30; /* 11, 150u */
  691. filter_cur = 0x40; /* 10, low */
  692. break;
  693. default: /* DVB-T 8M */
  694. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  695. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  696. lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
  697. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  698. air_cable1_in = 0x00;
  699. cable2_in = 0x00;
  700. pre_dect = 0x40;
  701. lna_discharge = 14;
  702. cp_cur = 0x38; /* 111, auto */
  703. div_buf_cur = 0x30; /* 11, 150u */
  704. filter_cur = 0x40; /* 10, low */
  705. break;
  706. }
  707. if (priv->cfg->use_diplexer &&
  708. ((priv->cfg->rafael_chip == CHIP_R820T) ||
  709. (priv->cfg->rafael_chip == CHIP_R828S) ||
  710. (priv->cfg->rafael_chip == CHIP_R820C))) {
  711. if (freq > DIP_FREQ)
  712. air_cable1_in = 0x00;
  713. else
  714. air_cable1_in = 0x60;
  715. cable2_in = 0x00;
  716. }
  717. if (priv->cfg->use_predetect) {
  718. rc = r820t_write_reg_mask(priv, 0x06, pre_dect, 0x40);
  719. if (rc < 0)
  720. return rc;
  721. }
  722. rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7);
  723. if (rc < 0)
  724. return rc;
  725. rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0xf8);
  726. if (rc < 0)
  727. return rc;
  728. rc = r820t_write_reg(priv, 0x0d, lna_vth_l);
  729. if (rc < 0)
  730. return rc;
  731. rc = r820t_write_reg(priv, 0x0e, mixer_vth_l);
  732. if (rc < 0)
  733. return rc;
  734. /* Air-IN only for Astrometa */
  735. rc = r820t_write_reg_mask(priv, 0x05, air_cable1_in, 0x60);
  736. if (rc < 0)
  737. return rc;
  738. rc = r820t_write_reg_mask(priv, 0x06, cable2_in, 0x08);
  739. if (rc < 0)
  740. return rc;
  741. rc = r820t_write_reg_mask(priv, 0x11, cp_cur, 0x38);
  742. if (rc < 0)
  743. return rc;
  744. rc = r820t_write_reg_mask(priv, 0x17, div_buf_cur, 0x30);
  745. if (rc < 0)
  746. return rc;
  747. rc = r820t_write_reg_mask(priv, 0x0a, filter_cur, 0x60);
  748. if (rc < 0)
  749. return rc;
  750. /*
  751. * Original driver initializes regs 0x05 and 0x06 with the
  752. * same value again on this point. Probably, it is just an
  753. * error there
  754. */
  755. /*
  756. * Set LNA
  757. */
  758. tuner_dbg("adjusting LNA parameters\n");
  759. if (type != V4L2_TUNER_ANALOG_TV) {
  760. /* LNA TOP: lowest */
  761. rc = r820t_write_reg_mask(priv, 0x1d, 0, 0x38);
  762. if (rc < 0)
  763. return rc;
  764. /* 0: normal mode */
  765. rc = r820t_write_reg_mask(priv, 0x1c, 0, 0x04);
  766. if (rc < 0)
  767. return rc;
  768. /* 0: PRE_DECT off */
  769. rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
  770. if (rc < 0)
  771. return rc;
  772. /* agc clk 250hz */
  773. rc = r820t_write_reg_mask(priv, 0x1a, 0x30, 0x30);
  774. if (rc < 0)
  775. return rc;
  776. msleep(250);
  777. /* write LNA TOP = 3 */
  778. rc = r820t_write_reg_mask(priv, 0x1d, 0x18, 0x38);
  779. if (rc < 0)
  780. return rc;
  781. /*
  782. * write discharge mode
  783. * FIXME: IMHO, the mask here is wrong, but it matches
  784. * what's there at the original driver
  785. */
  786. rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
  787. if (rc < 0)
  788. return rc;
  789. /* LNA discharge current */
  790. rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
  791. if (rc < 0)
  792. return rc;
  793. /* agc clk 60hz */
  794. rc = r820t_write_reg_mask(priv, 0x1a, 0x20, 0x30);
  795. if (rc < 0)
  796. return rc;
  797. } else {
  798. /* PRE_DECT off */
  799. rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
  800. if (rc < 0)
  801. return rc;
  802. /* write LNA TOP */
  803. rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0x38);
  804. if (rc < 0)
  805. return rc;
  806. /*
  807. * write discharge mode
  808. * FIXME: IMHO, the mask here is wrong, but it matches
  809. * what's there at the original driver
  810. */
  811. rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
  812. if (rc < 0)
  813. return rc;
  814. /* LNA discharge current */
  815. rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
  816. if (rc < 0)
  817. return rc;
  818. /* agc clk 1Khz, external det1 cap 1u */
  819. rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x30);
  820. if (rc < 0)
  821. return rc;
  822. rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x04);
  823. if (rc < 0)
  824. return rc;
  825. }
  826. return 0;
  827. }
  828. static int r820t_set_tv_standard(struct r820t_priv *priv,
  829. unsigned bw,
  830. enum v4l2_tuner_type type,
  831. v4l2_std_id std, u32 delsys)
  832. {
  833. int rc, i;
  834. u32 if_khz, filt_cal_lo;
  835. u8 data[5], val;
  836. u8 filt_gain, img_r, filt_q, hp_cor, ext_enable, loop_through;
  837. u8 lt_att, flt_ext_widest, polyfil_cur;
  838. bool need_calibration;
  839. tuner_dbg("selecting the delivery system\n");
  840. if (delsys == SYS_ISDBT) {
  841. if_khz = 4063;
  842. filt_cal_lo = 59000;
  843. filt_gain = 0x10; /* +3db, 6mhz on */
  844. img_r = 0x00; /* image negative */
  845. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  846. hp_cor = 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
  847. ext_enable = 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
  848. loop_through = 0x00; /* r5[7], lt on */
  849. lt_att = 0x00; /* r31[7], lt att enable */
  850. flt_ext_widest = 0x80; /* r15[7]: flt_ext_wide on */
  851. polyfil_cur = 0x60; /* r25[6:5]:min */
  852. } else if (delsys == SYS_DVBC_ANNEX_A) {
  853. if_khz = 5070;
  854. filt_cal_lo = 73500;
  855. filt_gain = 0x10; /* +3db, 6mhz on */
  856. img_r = 0x00; /* image negative */
  857. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  858. hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
  859. ext_enable = 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  860. loop_through = 0x00; /* r5[7], lt on */
  861. lt_att = 0x00; /* r31[7], lt att enable */
  862. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  863. polyfil_cur = 0x60; /* r25[6:5]:min */
  864. } else if (delsys == SYS_DVBC_ANNEX_C) {
  865. if_khz = 4063;
  866. filt_cal_lo = 55000;
  867. filt_gain = 0x10; /* +3db, 6mhz on */
  868. img_r = 0x00; /* image negative */
  869. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  870. hp_cor = 0x6a; /* 1.7m disable, +0cap, 1.0mhz */
  871. ext_enable = 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  872. loop_through = 0x00; /* r5[7], lt on */
  873. lt_att = 0x00; /* r31[7], lt att enable */
  874. flt_ext_widest = 0x80; /* r15[7]: flt_ext_wide on */
  875. polyfil_cur = 0x60; /* r25[6:5]:min */
  876. } else {
  877. if (bw <= 6) {
  878. if_khz = 3570;
  879. filt_cal_lo = 56000; /* 52000->56000 */
  880. filt_gain = 0x10; /* +3db, 6mhz on */
  881. img_r = 0x00; /* image negative */
  882. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  883. hp_cor = 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
  884. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  885. loop_through = 0x00; /* r5[7], lt on */
  886. lt_att = 0x00; /* r31[7], lt att enable */
  887. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  888. polyfil_cur = 0x60; /* r25[6:5]:min */
  889. } else if (bw == 7) {
  890. #if 0
  891. /*
  892. * There are two 7 MHz tables defined on the original
  893. * driver, but just the second one seems to be visible
  894. * by rtl2832. Keep this one here commented, as it
  895. * might be needed in the future
  896. */
  897. if_khz = 4070;
  898. filt_cal_lo = 60000;
  899. filt_gain = 0x10; /* +3db, 6mhz on */
  900. img_r = 0x00; /* image negative */
  901. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  902. hp_cor = 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
  903. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  904. loop_through = 0x00; /* r5[7], lt on */
  905. lt_att = 0x00; /* r31[7], lt att enable */
  906. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  907. polyfil_cur = 0x60; /* r25[6:5]:min */
  908. #endif
  909. /* 7 MHz, second table */
  910. if_khz = 4570;
  911. filt_cal_lo = 63000;
  912. filt_gain = 0x10; /* +3db, 6mhz on */
  913. img_r = 0x00; /* image negative */
  914. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  915. hp_cor = 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
  916. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  917. loop_through = 0x00; /* r5[7], lt on */
  918. lt_att = 0x00; /* r31[7], lt att enable */
  919. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  920. polyfil_cur = 0x60; /* r25[6:5]:min */
  921. } else {
  922. if_khz = 4570;
  923. filt_cal_lo = 68500;
  924. filt_gain = 0x10; /* +3db, 6mhz on */
  925. img_r = 0x00; /* image negative */
  926. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  927. hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
  928. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  929. loop_through = 0x00; /* r5[7], lt on */
  930. lt_att = 0x00; /* r31[7], lt att enable */
  931. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  932. polyfil_cur = 0x60; /* r25[6:5]:min */
  933. }
  934. }
  935. /* Initialize the shadow registers */
  936. memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
  937. /* Init Flag & Xtal_check Result */
  938. if (priv->imr_done)
  939. val = 1 | priv->xtal_cap_sel << 1;
  940. else
  941. val = 0;
  942. rc = r820t_write_reg_mask(priv, 0x0c, val, 0x0f);
  943. if (rc < 0)
  944. return rc;
  945. /* version */
  946. rc = r820t_write_reg_mask(priv, 0x13, VER_NUM, 0x3f);
  947. if (rc < 0)
  948. return rc;
  949. /* for LT Gain test */
  950. if (type != V4L2_TUNER_ANALOG_TV) {
  951. rc = r820t_write_reg_mask(priv, 0x1d, 0x00, 0x38);
  952. if (rc < 0)
  953. return rc;
  954. usleep_range(1000, 2000);
  955. }
  956. priv->int_freq = if_khz * 1000;
  957. /* Check if standard changed. If so, filter calibration is needed */
  958. if (type != priv->type)
  959. need_calibration = true;
  960. else if ((type == V4L2_TUNER_ANALOG_TV) && (std != priv->std))
  961. need_calibration = true;
  962. else if ((type == V4L2_TUNER_DIGITAL_TV) &&
  963. ((delsys != priv->delsys) || bw != priv->bw))
  964. need_calibration = true;
  965. else
  966. need_calibration = false;
  967. if (need_calibration) {
  968. tuner_dbg("calibrating the tuner\n");
  969. for (i = 0; i < 2; i++) {
  970. /* Set filt_cap */
  971. rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x60);
  972. if (rc < 0)
  973. return rc;
  974. /* set cali clk =on */
  975. rc = r820t_write_reg_mask(priv, 0x0f, 0x04, 0x04);
  976. if (rc < 0)
  977. return rc;
  978. /* X'tal cap 0pF for PLL */
  979. rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x03);
  980. if (rc < 0)
  981. return rc;
  982. rc = r820t_set_pll(priv, type, filt_cal_lo * 1000);
  983. if (rc < 0 || !priv->has_lock)
  984. return rc;
  985. /* Start Trigger */
  986. rc = r820t_write_reg_mask(priv, 0x0b, 0x10, 0x10);
  987. if (rc < 0)
  988. return rc;
  989. usleep_range(1000, 2000);
  990. /* Stop Trigger */
  991. rc = r820t_write_reg_mask(priv, 0x0b, 0x00, 0x10);
  992. if (rc < 0)
  993. return rc;
  994. /* set cali clk =off */
  995. rc = r820t_write_reg_mask(priv, 0x0f, 0x00, 0x04);
  996. if (rc < 0)
  997. return rc;
  998. /* Check if calibration worked */
  999. rc = r820t_read(priv, 0x00, data, sizeof(data));
  1000. if (rc < 0)
  1001. return rc;
  1002. priv->fil_cal_code = data[4] & 0x0f;
  1003. if (priv->fil_cal_code && priv->fil_cal_code != 0x0f)
  1004. break;
  1005. }
  1006. /* narrowest */
  1007. if (priv->fil_cal_code == 0x0f)
  1008. priv->fil_cal_code = 0;
  1009. }
  1010. rc = r820t_write_reg_mask(priv, 0x0a,
  1011. filt_q | priv->fil_cal_code, 0x1f);
  1012. if (rc < 0)
  1013. return rc;
  1014. /* Set BW, Filter_gain, & HP corner */
  1015. rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0xef);
  1016. if (rc < 0)
  1017. return rc;
  1018. /* Set Img_R */
  1019. rc = r820t_write_reg_mask(priv, 0x07, img_r, 0x80);
  1020. if (rc < 0)
  1021. return rc;
  1022. /* Set filt_3dB, V6MHz */
  1023. rc = r820t_write_reg_mask(priv, 0x06, filt_gain, 0x30);
  1024. if (rc < 0)
  1025. return rc;
  1026. /* channel filter extension */
  1027. rc = r820t_write_reg_mask(priv, 0x1e, ext_enable, 0x60);
  1028. if (rc < 0)
  1029. return rc;
  1030. /* Loop through */
  1031. rc = r820t_write_reg_mask(priv, 0x05, loop_through, 0x80);
  1032. if (rc < 0)
  1033. return rc;
  1034. /* Loop through attenuation */
  1035. rc = r820t_write_reg_mask(priv, 0x1f, lt_att, 0x80);
  1036. if (rc < 0)
  1037. return rc;
  1038. /* filter extension widest */
  1039. rc = r820t_write_reg_mask(priv, 0x0f, flt_ext_widest, 0x80);
  1040. if (rc < 0)
  1041. return rc;
  1042. /* RF poly filter current */
  1043. rc = r820t_write_reg_mask(priv, 0x19, polyfil_cur, 0x60);
  1044. if (rc < 0)
  1045. return rc;
  1046. /* Store current standard. If it changes, re-calibrate the tuner */
  1047. priv->delsys = delsys;
  1048. priv->type = type;
  1049. priv->std = std;
  1050. priv->bw = bw;
  1051. return 0;
  1052. }
  1053. static int r820t_read_gain(struct r820t_priv *priv)
  1054. {
  1055. u8 data[4];
  1056. int rc;
  1057. rc = r820t_read(priv, 0x00, data, sizeof(data));
  1058. if (rc < 0)
  1059. return rc;
  1060. return ((data[3] & 0x08) << 1) + ((data[3] & 0xf0) >> 4);
  1061. }
  1062. #if 0
  1063. /* FIXME: This routine requires more testing */
  1064. /*
  1065. * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
  1066. * input power, for raw results see:
  1067. * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
  1068. */
  1069. static const int r820t_lna_gain_steps[] = {
  1070. 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
  1071. };
  1072. static const int r820t_mixer_gain_steps[] = {
  1073. 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
  1074. };
  1075. static int r820t_set_gain_mode(struct r820t_priv *priv,
  1076. bool set_manual_gain,
  1077. int gain)
  1078. {
  1079. int rc;
  1080. if (set_manual_gain) {
  1081. int i, total_gain = 0;
  1082. uint8_t mix_index = 0, lna_index = 0;
  1083. u8 data[4];
  1084. /* LNA auto off */
  1085. rc = r820t_write_reg_mask(priv, 0x05, 0x10, 0x10);
  1086. if (rc < 0)
  1087. return rc;
  1088. /* Mixer auto off */
  1089. rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
  1090. if (rc < 0)
  1091. return rc;
  1092. rc = r820t_read(priv, 0x00, data, sizeof(data));
  1093. if (rc < 0)
  1094. return rc;
  1095. /* set fixed VGA gain for now (16.3 dB) */
  1096. rc = r820t_write_reg_mask(priv, 0x0c, 0x08, 0x9f);
  1097. if (rc < 0)
  1098. return rc;
  1099. for (i = 0; i < 15; i++) {
  1100. if (total_gain >= gain)
  1101. break;
  1102. total_gain += r820t_lna_gain_steps[++lna_index];
  1103. if (total_gain >= gain)
  1104. break;
  1105. total_gain += r820t_mixer_gain_steps[++mix_index];
  1106. }
  1107. /* set LNA gain */
  1108. rc = r820t_write_reg_mask(priv, 0x05, lna_index, 0x0f);
  1109. if (rc < 0)
  1110. return rc;
  1111. /* set Mixer gain */
  1112. rc = r820t_write_reg_mask(priv, 0x07, mix_index, 0x0f);
  1113. if (rc < 0)
  1114. return rc;
  1115. } else {
  1116. /* LNA */
  1117. rc = r820t_write_reg_mask(priv, 0x05, 0, 0x10);
  1118. if (rc < 0)
  1119. return rc;
  1120. /* Mixer */
  1121. rc = r820t_write_reg_mask(priv, 0x07, 0x10, 0x10);
  1122. if (rc < 0)
  1123. return rc;
  1124. /* set fixed VGA gain for now (26.5 dB) */
  1125. rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
  1126. if (rc < 0)
  1127. return rc;
  1128. }
  1129. return 0;
  1130. }
  1131. #endif
  1132. static int generic_set_freq(struct dvb_frontend *fe,
  1133. u32 freq /* in HZ */,
  1134. unsigned bw,
  1135. enum v4l2_tuner_type type,
  1136. v4l2_std_id std, u32 delsys)
  1137. {
  1138. struct r820t_priv *priv = fe->tuner_priv;
  1139. int rc;
  1140. u32 lo_freq;
  1141. tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
  1142. freq / 1000, bw);
  1143. rc = r820t_set_tv_standard(priv, bw, type, std, delsys);
  1144. if (rc < 0)
  1145. goto err;
  1146. if ((type == V4L2_TUNER_ANALOG_TV) && (std == V4L2_STD_SECAM_LC))
  1147. lo_freq = freq - priv->int_freq;
  1148. else
  1149. lo_freq = freq + priv->int_freq;
  1150. rc = r820t_set_mux(priv, lo_freq);
  1151. if (rc < 0)
  1152. goto err;
  1153. rc = r820t_set_pll(priv, type, lo_freq);
  1154. if (rc < 0 || !priv->has_lock)
  1155. goto err;
  1156. rc = r820t_sysfreq_sel(priv, freq, type, std, delsys);
  1157. if (rc < 0)
  1158. goto err;
  1159. tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
  1160. __func__, freq, r820t_read_gain(priv));
  1161. err:
  1162. if (rc < 0)
  1163. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1164. return rc;
  1165. }
  1166. /*
  1167. * r820t standby logic
  1168. */
  1169. static int r820t_standby(struct r820t_priv *priv)
  1170. {
  1171. int rc;
  1172. /* If device was not initialized yet, don't need to standby */
  1173. if (!priv->init_done)
  1174. return 0;
  1175. rc = r820t_write_reg(priv, 0x06, 0xb1);
  1176. if (rc < 0)
  1177. return rc;
  1178. rc = r820t_write_reg(priv, 0x05, 0x03);
  1179. if (rc < 0)
  1180. return rc;
  1181. rc = r820t_write_reg(priv, 0x07, 0x3a);
  1182. if (rc < 0)
  1183. return rc;
  1184. rc = r820t_write_reg(priv, 0x08, 0x40);
  1185. if (rc < 0)
  1186. return rc;
  1187. rc = r820t_write_reg(priv, 0x09, 0xc0);
  1188. if (rc < 0)
  1189. return rc;
  1190. rc = r820t_write_reg(priv, 0x0a, 0x36);
  1191. if (rc < 0)
  1192. return rc;
  1193. rc = r820t_write_reg(priv, 0x0c, 0x35);
  1194. if (rc < 0)
  1195. return rc;
  1196. rc = r820t_write_reg(priv, 0x0f, 0x68);
  1197. if (rc < 0)
  1198. return rc;
  1199. rc = r820t_write_reg(priv, 0x11, 0x03);
  1200. if (rc < 0)
  1201. return rc;
  1202. rc = r820t_write_reg(priv, 0x17, 0xf4);
  1203. if (rc < 0)
  1204. return rc;
  1205. rc = r820t_write_reg(priv, 0x19, 0x0c);
  1206. /* Force initial calibration */
  1207. priv->type = -1;
  1208. return rc;
  1209. }
  1210. /*
  1211. * r820t device init logic
  1212. */
  1213. static int r820t_xtal_check(struct r820t_priv *priv)
  1214. {
  1215. int rc, i;
  1216. u8 data[3], val;
  1217. /* Initialize the shadow registers */
  1218. memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
  1219. /* cap 30pF & Drive Low */
  1220. rc = r820t_write_reg_mask(priv, 0x10, 0x0b, 0x0b);
  1221. if (rc < 0)
  1222. return rc;
  1223. /* set pll autotune = 128kHz */
  1224. rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
  1225. if (rc < 0)
  1226. return rc;
  1227. /* set manual initial reg = 111111; */
  1228. rc = r820t_write_reg_mask(priv, 0x13, 0x7f, 0x7f);
  1229. if (rc < 0)
  1230. return rc;
  1231. /* set auto */
  1232. rc = r820t_write_reg_mask(priv, 0x13, 0x00, 0x40);
  1233. if (rc < 0)
  1234. return rc;
  1235. /* Try several xtal capacitor alternatives */
  1236. for (i = 0; i < ARRAY_SIZE(r820t_xtal_capacitor); i++) {
  1237. rc = r820t_write_reg_mask(priv, 0x10,
  1238. r820t_xtal_capacitor[i][0], 0x1b);
  1239. if (rc < 0)
  1240. return rc;
  1241. usleep_range(5000, 6000);
  1242. rc = r820t_read(priv, 0x00, data, sizeof(data));
  1243. if (rc < 0)
  1244. return rc;
  1245. if (!(data[2] & 0x40))
  1246. continue;
  1247. val = data[2] & 0x3f;
  1248. if (priv->cfg->xtal == 16000000 && (val > 29 || val < 23))
  1249. break;
  1250. if (val != 0x3f)
  1251. break;
  1252. }
  1253. if (i == ARRAY_SIZE(r820t_xtal_capacitor))
  1254. return -EINVAL;
  1255. return r820t_xtal_capacitor[i][1];
  1256. }
  1257. static int r820t_imr_prepare(struct r820t_priv *priv)
  1258. {
  1259. int rc;
  1260. /* Initialize the shadow registers */
  1261. memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
  1262. /* lna off (air-in off) */
  1263. rc = r820t_write_reg_mask(priv, 0x05, 0x20, 0x20);
  1264. if (rc < 0)
  1265. return rc;
  1266. /* mixer gain mode = manual */
  1267. rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
  1268. if (rc < 0)
  1269. return rc;
  1270. /* filter corner = lowest */
  1271. rc = r820t_write_reg_mask(priv, 0x0a, 0x0f, 0x0f);
  1272. if (rc < 0)
  1273. return rc;
  1274. /* filter bw=+2cap, hp=5M */
  1275. rc = r820t_write_reg_mask(priv, 0x0b, 0x60, 0x6f);
  1276. if (rc < 0)
  1277. return rc;
  1278. /* adc=on, vga code mode, gain = 26.5dB */
  1279. rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
  1280. if (rc < 0)
  1281. return rc;
  1282. /* ring clk = on */
  1283. rc = r820t_write_reg_mask(priv, 0x0f, 0, 0x08);
  1284. if (rc < 0)
  1285. return rc;
  1286. /* ring power = on */
  1287. rc = r820t_write_reg_mask(priv, 0x18, 0x10, 0x10);
  1288. if (rc < 0)
  1289. return rc;
  1290. /* from ring = ring pll in */
  1291. rc = r820t_write_reg_mask(priv, 0x1c, 0x02, 0x02);
  1292. if (rc < 0)
  1293. return rc;
  1294. /* sw_pdect = det3 */
  1295. rc = r820t_write_reg_mask(priv, 0x1e, 0x80, 0x80);
  1296. if (rc < 0)
  1297. return rc;
  1298. /* Set filt_3dB */
  1299. rc = r820t_write_reg_mask(priv, 0x06, 0x20, 0x20);
  1300. return rc;
  1301. }
  1302. static int r820t_multi_read(struct r820t_priv *priv)
  1303. {
  1304. int rc, i;
  1305. u16 sum = 0;
  1306. u8 data[2], min = 255, max = 0;
  1307. usleep_range(5000, 6000);
  1308. for (i = 0; i < 6; i++) {
  1309. rc = r820t_read(priv, 0x00, data, sizeof(data));
  1310. if (rc < 0)
  1311. return rc;
  1312. sum += data[1];
  1313. if (data[1] < min)
  1314. min = data[1];
  1315. if (data[1] > max)
  1316. max = data[1];
  1317. }
  1318. rc = sum - max - min;
  1319. return rc;
  1320. }
  1321. static int r820t_imr_cross(struct r820t_priv *priv,
  1322. struct r820t_sect_type iq_point[3],
  1323. u8 *x_direct)
  1324. {
  1325. struct r820t_sect_type cross[5]; /* (0,0)(0,Q-1)(0,I-1)(Q-1,0)(I-1,0) */
  1326. struct r820t_sect_type tmp;
  1327. int i, rc;
  1328. u8 reg08, reg09;
  1329. reg08 = r820t_read_cache_reg(priv, 8) & 0xc0;
  1330. reg09 = r820t_read_cache_reg(priv, 9) & 0xc0;
  1331. tmp.gain_x = 0;
  1332. tmp.phase_y = 0;
  1333. tmp.value = 255;
  1334. for (i = 0; i < 5; i++) {
  1335. switch (i) {
  1336. case 0:
  1337. cross[i].gain_x = reg08;
  1338. cross[i].phase_y = reg09;
  1339. break;
  1340. case 1:
  1341. cross[i].gain_x = reg08; /* 0 */
  1342. cross[i].phase_y = reg09 + 1; /* Q-1 */
  1343. break;
  1344. case 2:
  1345. cross[i].gain_x = reg08; /* 0 */
  1346. cross[i].phase_y = (reg09 | 0x20) + 1; /* I-1 */
  1347. break;
  1348. case 3:
  1349. cross[i].gain_x = reg08 + 1; /* Q-1 */
  1350. cross[i].phase_y = reg09;
  1351. break;
  1352. default:
  1353. cross[i].gain_x = (reg08 | 0x20) + 1; /* I-1 */
  1354. cross[i].phase_y = reg09;
  1355. }
  1356. rc = r820t_write_reg(priv, 0x08, cross[i].gain_x);
  1357. if (rc < 0)
  1358. return rc;
  1359. rc = r820t_write_reg(priv, 0x09, cross[i].phase_y);
  1360. if (rc < 0)
  1361. return rc;
  1362. rc = r820t_multi_read(priv);
  1363. if (rc < 0)
  1364. return rc;
  1365. cross[i].value = rc;
  1366. if (cross[i].value < tmp.value)
  1367. tmp = cross[i];
  1368. }
  1369. if ((tmp.phase_y & 0x1f) == 1) { /* y-direction */
  1370. *x_direct = 0;
  1371. iq_point[0] = cross[0];
  1372. iq_point[1] = cross[1];
  1373. iq_point[2] = cross[2];
  1374. } else { /* (0,0) or x-direction */
  1375. *x_direct = 1;
  1376. iq_point[0] = cross[0];
  1377. iq_point[1] = cross[3];
  1378. iq_point[2] = cross[4];
  1379. }
  1380. return 0;
  1381. }
  1382. static void r820t_compre_cor(struct r820t_sect_type iq[3])
  1383. {
  1384. int i;
  1385. for (i = 3; i > 0; i--) {
  1386. if (iq[0].value > iq[i - 1].value)
  1387. swap(iq[0], iq[i - 1]);
  1388. }
  1389. }
  1390. static int r820t_compre_step(struct r820t_priv *priv,
  1391. struct r820t_sect_type iq[3], u8 reg)
  1392. {
  1393. int rc;
  1394. struct r820t_sect_type tmp;
  1395. /*
  1396. * Purpose: if (Gain<9 or Phase<9), Gain+1 or Phase+1 and compare
  1397. * with min value:
  1398. * new < min => update to min and continue
  1399. * new > min => Exit
  1400. */
  1401. /* min value already saved in iq[0] */
  1402. tmp.phase_y = iq[0].phase_y;
  1403. tmp.gain_x = iq[0].gain_x;
  1404. while (((tmp.gain_x & 0x1f) < IMR_TRIAL) &&
  1405. ((tmp.phase_y & 0x1f) < IMR_TRIAL)) {
  1406. if (reg == 0x08)
  1407. tmp.gain_x++;
  1408. else
  1409. tmp.phase_y++;
  1410. rc = r820t_write_reg(priv, 0x08, tmp.gain_x);
  1411. if (rc < 0)
  1412. return rc;
  1413. rc = r820t_write_reg(priv, 0x09, tmp.phase_y);
  1414. if (rc < 0)
  1415. return rc;
  1416. rc = r820t_multi_read(priv);
  1417. if (rc < 0)
  1418. return rc;
  1419. tmp.value = rc;
  1420. if (tmp.value <= iq[0].value) {
  1421. iq[0].gain_x = tmp.gain_x;
  1422. iq[0].phase_y = tmp.phase_y;
  1423. iq[0].value = tmp.value;
  1424. } else {
  1425. return 0;
  1426. }
  1427. }
  1428. return 0;
  1429. }
  1430. static int r820t_iq_tree(struct r820t_priv *priv,
  1431. struct r820t_sect_type iq[3],
  1432. u8 fix_val, u8 var_val, u8 fix_reg)
  1433. {
  1434. int rc, i;
  1435. u8 tmp, var_reg;
  1436. /*
  1437. * record IMC results by input gain/phase location then adjust
  1438. * gain or phase positive 1 step and negtive 1 step,
  1439. * both record results
  1440. */
  1441. if (fix_reg == 0x08)
  1442. var_reg = 0x09;
  1443. else
  1444. var_reg = 0x08;
  1445. for (i = 0; i < 3; i++) {
  1446. rc = r820t_write_reg(priv, fix_reg, fix_val);
  1447. if (rc < 0)
  1448. return rc;
  1449. rc = r820t_write_reg(priv, var_reg, var_val);
  1450. if (rc < 0)
  1451. return rc;
  1452. rc = r820t_multi_read(priv);
  1453. if (rc < 0)
  1454. return rc;
  1455. iq[i].value = rc;
  1456. if (fix_reg == 0x08) {
  1457. iq[i].gain_x = fix_val;
  1458. iq[i].phase_y = var_val;
  1459. } else {
  1460. iq[i].phase_y = fix_val;
  1461. iq[i].gain_x = var_val;
  1462. }
  1463. if (i == 0) { /* try right-side point */
  1464. var_val++;
  1465. } else if (i == 1) { /* try left-side point */
  1466. /* if absolute location is 1, change I/Q direction */
  1467. if ((var_val & 0x1f) < 0x02) {
  1468. tmp = 2 - (var_val & 0x1f);
  1469. /* b[5]:I/Q selection. 0:Q-path, 1:I-path */
  1470. if (var_val & 0x20) {
  1471. var_val &= 0xc0;
  1472. var_val |= tmp;
  1473. } else {
  1474. var_val |= 0x20 | tmp;
  1475. }
  1476. } else {
  1477. var_val -= 2;
  1478. }
  1479. }
  1480. }
  1481. return 0;
  1482. }
  1483. static int r820t_section(struct r820t_priv *priv,
  1484. struct r820t_sect_type *iq_point)
  1485. {
  1486. int rc;
  1487. struct r820t_sect_type compare_iq[3], compare_bet[3];
  1488. /* Try X-1 column and save min result to compare_bet[0] */
  1489. if (!(iq_point->gain_x & 0x1f))
  1490. compare_iq[0].gain_x = ((iq_point->gain_x) & 0xdf) + 1; /* Q-path, Gain=1 */
  1491. else
  1492. compare_iq[0].gain_x = iq_point->gain_x - 1; /* left point */
  1493. compare_iq[0].phase_y = iq_point->phase_y;
  1494. /* y-direction */
  1495. rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
  1496. compare_iq[0].phase_y, 0x08);
  1497. if (rc < 0)
  1498. return rc;
  1499. r820t_compre_cor(compare_iq);
  1500. compare_bet[0] = compare_iq[0];
  1501. /* Try X column and save min result to compare_bet[1] */
  1502. compare_iq[0].gain_x = iq_point->gain_x;
  1503. compare_iq[0].phase_y = iq_point->phase_y;
  1504. rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
  1505. compare_iq[0].phase_y, 0x08);
  1506. if (rc < 0)
  1507. return rc;
  1508. r820t_compre_cor(compare_iq);
  1509. compare_bet[1] = compare_iq[0];
  1510. /* Try X+1 column and save min result to compare_bet[2] */
  1511. if ((iq_point->gain_x & 0x1f) == 0x00)
  1512. compare_iq[0].gain_x = ((iq_point->gain_x) | 0x20) + 1; /* I-path, Gain=1 */
  1513. else
  1514. compare_iq[0].gain_x = iq_point->gain_x + 1;
  1515. compare_iq[0].phase_y = iq_point->phase_y;
  1516. rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
  1517. compare_iq[0].phase_y, 0x08);
  1518. if (rc < 0)
  1519. return rc;
  1520. r820t_compre_cor(compare_iq);
  1521. compare_bet[2] = compare_iq[0];
  1522. r820t_compre_cor(compare_bet);
  1523. *iq_point = compare_bet[0];
  1524. return 0;
  1525. }
  1526. static int r820t_vga_adjust(struct r820t_priv *priv)
  1527. {
  1528. int rc;
  1529. u8 vga_count;
  1530. /* increase vga power to let image significant */
  1531. for (vga_count = 12; vga_count < 16; vga_count++) {
  1532. rc = r820t_write_reg_mask(priv, 0x0c, vga_count, 0x0f);
  1533. if (rc < 0)
  1534. return rc;
  1535. usleep_range(10000, 11000);
  1536. rc = r820t_multi_read(priv);
  1537. if (rc < 0)
  1538. return rc;
  1539. if (rc > 40 * 4)
  1540. break;
  1541. }
  1542. return 0;
  1543. }
  1544. static int r820t_iq(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
  1545. {
  1546. struct r820t_sect_type compare_iq[3];
  1547. int rc;
  1548. u8 x_direction = 0; /* 1:x, 0:y */
  1549. u8 dir_reg, other_reg;
  1550. r820t_vga_adjust(priv);
  1551. rc = r820t_imr_cross(priv, compare_iq, &x_direction);
  1552. if (rc < 0)
  1553. return rc;
  1554. if (x_direction == 1) {
  1555. dir_reg = 0x08;
  1556. other_reg = 0x09;
  1557. } else {
  1558. dir_reg = 0x09;
  1559. other_reg = 0x08;
  1560. }
  1561. /* compare and find min of 3 points. determine i/q direction */
  1562. r820t_compre_cor(compare_iq);
  1563. /* increase step to find min value of this direction */
  1564. rc = r820t_compre_step(priv, compare_iq, dir_reg);
  1565. if (rc < 0)
  1566. return rc;
  1567. /* the other direction */
  1568. rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
  1569. compare_iq[0].phase_y, dir_reg);
  1570. if (rc < 0)
  1571. return rc;
  1572. /* compare and find min of 3 points. determine i/q direction */
  1573. r820t_compre_cor(compare_iq);
  1574. /* increase step to find min value on this direction */
  1575. rc = r820t_compre_step(priv, compare_iq, other_reg);
  1576. if (rc < 0)
  1577. return rc;
  1578. /* check 3 points again */
  1579. rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
  1580. compare_iq[0].phase_y, other_reg);
  1581. if (rc < 0)
  1582. return rc;
  1583. r820t_compre_cor(compare_iq);
  1584. /* section-9 check */
  1585. rc = r820t_section(priv, compare_iq);
  1586. *iq_pont = compare_iq[0];
  1587. /* reset gain/phase control setting */
  1588. rc = r820t_write_reg_mask(priv, 0x08, 0, 0x3f);
  1589. if (rc < 0)
  1590. return rc;
  1591. rc = r820t_write_reg_mask(priv, 0x09, 0, 0x3f);
  1592. return rc;
  1593. }
  1594. static int r820t_f_imr(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
  1595. {
  1596. int rc;
  1597. r820t_vga_adjust(priv);
  1598. /*
  1599. * search surrounding points from previous point
  1600. * try (x-1), (x), (x+1) columns, and find min IMR result point
  1601. */
  1602. rc = r820t_section(priv, iq_pont);
  1603. if (rc < 0)
  1604. return rc;
  1605. return 0;
  1606. }
  1607. static int r820t_imr(struct r820t_priv *priv, unsigned imr_mem, bool im_flag)
  1608. {
  1609. struct r820t_sect_type imr_point;
  1610. int rc;
  1611. u32 ring_vco, ring_freq, ring_ref;
  1612. u8 n_ring, n;
  1613. int reg18, reg19, reg1f;
  1614. if (priv->cfg->xtal > 24000000)
  1615. ring_ref = priv->cfg->xtal / 2000;
  1616. else
  1617. ring_ref = priv->cfg->xtal / 1000;
  1618. n_ring = 15;
  1619. for (n = 0; n < 16; n++) {
  1620. if ((16 + n) * 8 * ring_ref >= 3100000) {
  1621. n_ring = n;
  1622. break;
  1623. }
  1624. }
  1625. reg18 = r820t_read_cache_reg(priv, 0x18);
  1626. reg19 = r820t_read_cache_reg(priv, 0x19);
  1627. reg1f = r820t_read_cache_reg(priv, 0x1f);
  1628. reg18 &= 0xf0; /* set ring[3:0] */
  1629. reg18 |= n_ring;
  1630. ring_vco = (16 + n_ring) * 8 * ring_ref;
  1631. reg18 &= 0xdf; /* clear ring_se23 */
  1632. reg19 &= 0xfc; /* clear ring_seldiv */
  1633. reg1f &= 0xfc; /* clear ring_att */
  1634. switch (imr_mem) {
  1635. case 0:
  1636. ring_freq = ring_vco / 48;
  1637. reg18 |= 0x20; /* ring_se23 = 1 */
  1638. reg19 |= 0x03; /* ring_seldiv = 3 */
  1639. reg1f |= 0x02; /* ring_att 10 */
  1640. break;
  1641. case 1:
  1642. ring_freq = ring_vco / 16;
  1643. reg18 |= 0x00; /* ring_se23 = 0 */
  1644. reg19 |= 0x02; /* ring_seldiv = 2 */
  1645. reg1f |= 0x00; /* pw_ring 00 */
  1646. break;
  1647. case 2:
  1648. ring_freq = ring_vco / 8;
  1649. reg18 |= 0x00; /* ring_se23 = 0 */
  1650. reg19 |= 0x01; /* ring_seldiv = 1 */
  1651. reg1f |= 0x03; /* pw_ring 11 */
  1652. break;
  1653. case 3:
  1654. ring_freq = ring_vco / 6;
  1655. reg18 |= 0x20; /* ring_se23 = 1 */
  1656. reg19 |= 0x00; /* ring_seldiv = 0 */
  1657. reg1f |= 0x03; /* pw_ring 11 */
  1658. break;
  1659. case 4:
  1660. ring_freq = ring_vco / 4;
  1661. reg18 |= 0x00; /* ring_se23 = 0 */
  1662. reg19 |= 0x00; /* ring_seldiv = 0 */
  1663. reg1f |= 0x01; /* pw_ring 01 */
  1664. break;
  1665. default:
  1666. ring_freq = ring_vco / 4;
  1667. reg18 |= 0x00; /* ring_se23 = 0 */
  1668. reg19 |= 0x00; /* ring_seldiv = 0 */
  1669. reg1f |= 0x01; /* pw_ring 01 */
  1670. break;
  1671. }
  1672. /* write pw_ring, n_ring, ringdiv2 registers */
  1673. /* n_ring, ring_se23 */
  1674. rc = r820t_write_reg(priv, 0x18, reg18);
  1675. if (rc < 0)
  1676. return rc;
  1677. /* ring_sediv */
  1678. rc = r820t_write_reg(priv, 0x19, reg19);
  1679. if (rc < 0)
  1680. return rc;
  1681. /* pw_ring */
  1682. rc = r820t_write_reg(priv, 0x1f, reg1f);
  1683. if (rc < 0)
  1684. return rc;
  1685. /* mux input freq ~ rf_in freq */
  1686. rc = r820t_set_mux(priv, (ring_freq - 5300) * 1000);
  1687. if (rc < 0)
  1688. return rc;
  1689. rc = r820t_set_pll(priv, V4L2_TUNER_DIGITAL_TV,
  1690. (ring_freq - 5300) * 1000);
  1691. if (!priv->has_lock)
  1692. rc = -EINVAL;
  1693. if (rc < 0)
  1694. return rc;
  1695. if (im_flag) {
  1696. rc = r820t_iq(priv, &imr_point);
  1697. } else {
  1698. imr_point.gain_x = priv->imr_data[3].gain_x;
  1699. imr_point.phase_y = priv->imr_data[3].phase_y;
  1700. imr_point.value = priv->imr_data[3].value;
  1701. rc = r820t_f_imr(priv, &imr_point);
  1702. }
  1703. if (rc < 0)
  1704. return rc;
  1705. /* save IMR value */
  1706. switch (imr_mem) {
  1707. case 0:
  1708. priv->imr_data[0].gain_x = imr_point.gain_x;
  1709. priv->imr_data[0].phase_y = imr_point.phase_y;
  1710. priv->imr_data[0].value = imr_point.value;
  1711. break;
  1712. case 1:
  1713. priv->imr_data[1].gain_x = imr_point.gain_x;
  1714. priv->imr_data[1].phase_y = imr_point.phase_y;
  1715. priv->imr_data[1].value = imr_point.value;
  1716. break;
  1717. case 2:
  1718. priv->imr_data[2].gain_x = imr_point.gain_x;
  1719. priv->imr_data[2].phase_y = imr_point.phase_y;
  1720. priv->imr_data[2].value = imr_point.value;
  1721. break;
  1722. case 3:
  1723. priv->imr_data[3].gain_x = imr_point.gain_x;
  1724. priv->imr_data[3].phase_y = imr_point.phase_y;
  1725. priv->imr_data[3].value = imr_point.value;
  1726. break;
  1727. case 4:
  1728. priv->imr_data[4].gain_x = imr_point.gain_x;
  1729. priv->imr_data[4].phase_y = imr_point.phase_y;
  1730. priv->imr_data[4].value = imr_point.value;
  1731. break;
  1732. default:
  1733. priv->imr_data[4].gain_x = imr_point.gain_x;
  1734. priv->imr_data[4].phase_y = imr_point.phase_y;
  1735. priv->imr_data[4].value = imr_point.value;
  1736. break;
  1737. }
  1738. return 0;
  1739. }
  1740. static int r820t_imr_callibrate(struct r820t_priv *priv)
  1741. {
  1742. int rc, i;
  1743. int xtal_cap = 0;
  1744. if (priv->init_done)
  1745. return 0;
  1746. /* Detect Xtal capacitance */
  1747. if ((priv->cfg->rafael_chip == CHIP_R820T) ||
  1748. (priv->cfg->rafael_chip == CHIP_R828S) ||
  1749. (priv->cfg->rafael_chip == CHIP_R820C)) {
  1750. priv->xtal_cap_sel = XTAL_HIGH_CAP_0P;
  1751. } else {
  1752. /* Initialize registers */
  1753. rc = r820t_write(priv, 0x05,
  1754. r820t_init_array, sizeof(r820t_init_array));
  1755. if (rc < 0)
  1756. return rc;
  1757. for (i = 0; i < 3; i++) {
  1758. rc = r820t_xtal_check(priv);
  1759. if (rc < 0)
  1760. return rc;
  1761. if (!i || rc > xtal_cap)
  1762. xtal_cap = rc;
  1763. }
  1764. priv->xtal_cap_sel = xtal_cap;
  1765. }
  1766. /*
  1767. * Disables IMR callibration. That emulates the same behaviour
  1768. * as what is done by rtl-sdr userspace library. Useful for testing
  1769. */
  1770. if (no_imr_cal) {
  1771. priv->init_done = true;
  1772. return 0;
  1773. }
  1774. /* Initialize registers */
  1775. rc = r820t_write(priv, 0x05,
  1776. r820t_init_array, sizeof(r820t_init_array));
  1777. if (rc < 0)
  1778. return rc;
  1779. rc = r820t_imr_prepare(priv);
  1780. if (rc < 0)
  1781. return rc;
  1782. rc = r820t_imr(priv, 3, true);
  1783. if (rc < 0)
  1784. return rc;
  1785. rc = r820t_imr(priv, 1, false);
  1786. if (rc < 0)
  1787. return rc;
  1788. rc = r820t_imr(priv, 0, false);
  1789. if (rc < 0)
  1790. return rc;
  1791. rc = r820t_imr(priv, 2, false);
  1792. if (rc < 0)
  1793. return rc;
  1794. rc = r820t_imr(priv, 4, false);
  1795. if (rc < 0)
  1796. return rc;
  1797. priv->init_done = true;
  1798. priv->imr_done = true;
  1799. return 0;
  1800. }
  1801. #if 0
  1802. /* Not used, for now */
  1803. static int r820t_gpio(struct r820t_priv *priv, bool enable)
  1804. {
  1805. return r820t_write_reg_mask(priv, 0x0f, enable ? 1 : 0, 0x01);
  1806. }
  1807. #endif
  1808. /*
  1809. * r820t frontend operations and tuner attach code
  1810. *
  1811. * All driver locks and i2c control are only in this part of the code
  1812. */
  1813. static int r820t_init(struct dvb_frontend *fe)
  1814. {
  1815. struct r820t_priv *priv = fe->tuner_priv;
  1816. int rc;
  1817. tuner_dbg("%s:\n", __func__);
  1818. mutex_lock(&priv->lock);
  1819. if (fe->ops.i2c_gate_ctrl)
  1820. fe->ops.i2c_gate_ctrl(fe, 1);
  1821. rc = r820t_imr_callibrate(priv);
  1822. if (rc < 0)
  1823. goto err;
  1824. /* Initialize registers */
  1825. rc = r820t_write(priv, 0x05,
  1826. r820t_init_array, sizeof(r820t_init_array));
  1827. err:
  1828. if (fe->ops.i2c_gate_ctrl)
  1829. fe->ops.i2c_gate_ctrl(fe, 0);
  1830. mutex_unlock(&priv->lock);
  1831. if (rc < 0)
  1832. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1833. return rc;
  1834. }
  1835. static int r820t_sleep(struct dvb_frontend *fe)
  1836. {
  1837. struct r820t_priv *priv = fe->tuner_priv;
  1838. int rc;
  1839. tuner_dbg("%s:\n", __func__);
  1840. mutex_lock(&priv->lock);
  1841. if (fe->ops.i2c_gate_ctrl)
  1842. fe->ops.i2c_gate_ctrl(fe, 1);
  1843. rc = r820t_standby(priv);
  1844. if (fe->ops.i2c_gate_ctrl)
  1845. fe->ops.i2c_gate_ctrl(fe, 0);
  1846. mutex_unlock(&priv->lock);
  1847. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1848. return rc;
  1849. }
  1850. static int r820t_set_analog_freq(struct dvb_frontend *fe,
  1851. struct analog_parameters *p)
  1852. {
  1853. struct r820t_priv *priv = fe->tuner_priv;
  1854. unsigned bw;
  1855. int rc;
  1856. tuner_dbg("%s called\n", __func__);
  1857. /* if std is not defined, choose one */
  1858. if (!p->std)
  1859. p->std = V4L2_STD_MN;
  1860. if ((p->std == V4L2_STD_PAL_M) || (p->std == V4L2_STD_NTSC))
  1861. bw = 6;
  1862. else
  1863. bw = 8;
  1864. mutex_lock(&priv->lock);
  1865. if (fe->ops.i2c_gate_ctrl)
  1866. fe->ops.i2c_gate_ctrl(fe, 1);
  1867. rc = generic_set_freq(fe, 62500l * p->frequency, bw,
  1868. V4L2_TUNER_ANALOG_TV, p->std, SYS_UNDEFINED);
  1869. if (fe->ops.i2c_gate_ctrl)
  1870. fe->ops.i2c_gate_ctrl(fe, 0);
  1871. mutex_unlock(&priv->lock);
  1872. return rc;
  1873. }
  1874. static int r820t_set_params(struct dvb_frontend *fe)
  1875. {
  1876. struct r820t_priv *priv = fe->tuner_priv;
  1877. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1878. int rc;
  1879. unsigned bw;
  1880. tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  1881. __func__, c->delivery_system, c->frequency, c->bandwidth_hz);
  1882. mutex_lock(&priv->lock);
  1883. if (fe->ops.i2c_gate_ctrl)
  1884. fe->ops.i2c_gate_ctrl(fe, 1);
  1885. bw = (c->bandwidth_hz + 500000) / 1000000;
  1886. if (!bw)
  1887. bw = 8;
  1888. rc = generic_set_freq(fe, c->frequency, bw,
  1889. V4L2_TUNER_DIGITAL_TV, 0, c->delivery_system);
  1890. if (fe->ops.i2c_gate_ctrl)
  1891. fe->ops.i2c_gate_ctrl(fe, 0);
  1892. mutex_unlock(&priv->lock);
  1893. if (rc)
  1894. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1895. return rc;
  1896. }
  1897. static int r820t_signal(struct dvb_frontend *fe, u16 *strength)
  1898. {
  1899. struct r820t_priv *priv = fe->tuner_priv;
  1900. int rc = 0;
  1901. mutex_lock(&priv->lock);
  1902. if (fe->ops.i2c_gate_ctrl)
  1903. fe->ops.i2c_gate_ctrl(fe, 1);
  1904. if (priv->has_lock) {
  1905. rc = r820t_read_gain(priv);
  1906. if (rc < 0)
  1907. goto err;
  1908. /* A higher gain at LNA means a lower signal strength */
  1909. *strength = (45 - rc) << 4 | 0xff;
  1910. if (*strength == 0xff)
  1911. *strength = 0;
  1912. } else {
  1913. *strength = 0;
  1914. }
  1915. err:
  1916. if (fe->ops.i2c_gate_ctrl)
  1917. fe->ops.i2c_gate_ctrl(fe, 0);
  1918. mutex_unlock(&priv->lock);
  1919. tuner_dbg("%s: %s, gain=%d strength=%d\n",
  1920. __func__,
  1921. priv->has_lock ? "PLL locked" : "no signal",
  1922. rc, *strength);
  1923. return 0;
  1924. }
  1925. static int r820t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  1926. {
  1927. struct r820t_priv *priv = fe->tuner_priv;
  1928. tuner_dbg("%s:\n", __func__);
  1929. *frequency = priv->int_freq;
  1930. return 0;
  1931. }
  1932. static int r820t_release(struct dvb_frontend *fe)
  1933. {
  1934. struct r820t_priv *priv = fe->tuner_priv;
  1935. tuner_dbg("%s:\n", __func__);
  1936. mutex_lock(&r820t_list_mutex);
  1937. if (priv)
  1938. hybrid_tuner_release_state(priv);
  1939. mutex_unlock(&r820t_list_mutex);
  1940. fe->tuner_priv = NULL;
  1941. return 0;
  1942. }
  1943. static const struct dvb_tuner_ops r820t_tuner_ops = {
  1944. .info = {
  1945. .name = "Rafael Micro R820T",
  1946. .frequency_min = 42000000,
  1947. .frequency_max = 1002000000,
  1948. },
  1949. .init = r820t_init,
  1950. .release = r820t_release,
  1951. .sleep = r820t_sleep,
  1952. .set_params = r820t_set_params,
  1953. .set_analog_params = r820t_set_analog_freq,
  1954. .get_if_frequency = r820t_get_if_frequency,
  1955. .get_rf_strength = r820t_signal,
  1956. };
  1957. struct dvb_frontend *r820t_attach(struct dvb_frontend *fe,
  1958. struct i2c_adapter *i2c,
  1959. const struct r820t_config *cfg)
  1960. {
  1961. struct r820t_priv *priv;
  1962. int rc = -ENODEV;
  1963. u8 data[5];
  1964. int instance;
  1965. mutex_lock(&r820t_list_mutex);
  1966. instance = hybrid_tuner_request_state(struct r820t_priv, priv,
  1967. hybrid_tuner_instance_list,
  1968. i2c, cfg->i2c_addr,
  1969. "r820t");
  1970. switch (instance) {
  1971. case 0:
  1972. /* memory allocation failure */
  1973. goto err_no_gate;
  1974. case 1:
  1975. /* new tuner instance */
  1976. priv->cfg = cfg;
  1977. mutex_init(&priv->lock);
  1978. fe->tuner_priv = priv;
  1979. break;
  1980. case 2:
  1981. /* existing tuner instance */
  1982. fe->tuner_priv = priv;
  1983. break;
  1984. }
  1985. if (fe->ops.i2c_gate_ctrl)
  1986. fe->ops.i2c_gate_ctrl(fe, 1);
  1987. /* check if the tuner is there */
  1988. rc = r820t_read(priv, 0x00, data, sizeof(data));
  1989. if (rc < 0)
  1990. goto err;
  1991. rc = r820t_sleep(fe);
  1992. if (rc < 0)
  1993. goto err;
  1994. tuner_info("Rafael Micro r820t successfully identified\n");
  1995. if (fe->ops.i2c_gate_ctrl)
  1996. fe->ops.i2c_gate_ctrl(fe, 0);
  1997. mutex_unlock(&r820t_list_mutex);
  1998. memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops,
  1999. sizeof(struct dvb_tuner_ops));
  2000. return fe;
  2001. err:
  2002. if (fe->ops.i2c_gate_ctrl)
  2003. fe->ops.i2c_gate_ctrl(fe, 0);
  2004. err_no_gate:
  2005. mutex_unlock(&r820t_list_mutex);
  2006. tuner_info("%s: failed=%d\n", __func__, rc);
  2007. r820t_release(fe);
  2008. return NULL;
  2009. }
  2010. EXPORT_SYMBOL_GPL(r820t_attach);
  2011. MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
  2012. MODULE_AUTHOR("Mauro Carvalho Chehab");
  2013. MODULE_LICENSE("GPL");