r820t.c 54 KB

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