r820t.c 54 KB

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