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