r820t.c 54 KB

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