r820t.c 55 KB

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