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