r820t.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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 VER_NUM 49
  48. static int debug;
  49. module_param(debug, int, 0644);
  50. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  51. /*
  52. * enums and structures
  53. */
  54. enum xtal_cap_value {
  55. XTAL_LOW_CAP_30P = 0,
  56. XTAL_LOW_CAP_20P,
  57. XTAL_LOW_CAP_10P,
  58. XTAL_LOW_CAP_0P,
  59. XTAL_HIGH_CAP_0P
  60. };
  61. struct r820t_priv {
  62. struct list_head hybrid_tuner_instance_list;
  63. const struct r820t_config *cfg;
  64. struct tuner_i2c_props i2c_props;
  65. struct mutex lock;
  66. u8 regs[NUM_REGS];
  67. u8 buf[NUM_REGS + 1];
  68. enum xtal_cap_value xtal_cap_sel;
  69. u16 pll; /* kHz */
  70. u32 int_freq;
  71. u8 fil_cal_code;
  72. bool imr_done;
  73. /* Store current mode */
  74. u32 delsys;
  75. enum v4l2_tuner_type type;
  76. v4l2_std_id std;
  77. u32 bw; /* in MHz */
  78. bool has_lock;
  79. };
  80. struct r820t_freq_range {
  81. u32 freq;
  82. u8 open_d;
  83. u8 rf_mux_ploy;
  84. u8 tf_c;
  85. u8 xtal_cap20p;
  86. u8 xtal_cap10p;
  87. u8 xtal_cap0p;
  88. u8 imr_mem; /* Not used, currently */
  89. };
  90. #define VCO_POWER_REF 0x02
  91. /*
  92. * Static constants
  93. */
  94. static LIST_HEAD(hybrid_tuner_instance_list);
  95. static DEFINE_MUTEX(r820t_list_mutex);
  96. /* Those initial values start from REG_SHADOW_START */
  97. static const u8 r820t_init_array[NUM_REGS] = {
  98. 0x83, 0x32, 0x75, /* 05 to 07 */
  99. 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */
  100. 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */
  101. 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */
  102. 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */
  103. 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */
  104. 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */
  105. };
  106. /* Tuner frequency ranges */
  107. static const struct r820t_freq_range freq_ranges[] = {
  108. {
  109. .freq = 0,
  110. .open_d = 0x08, /* low */
  111. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  112. .tf_c = 0xdf, /* R27[7:0] band2,band0 */
  113. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  114. .xtal_cap10p = 0x01,
  115. .xtal_cap0p = 0x00,
  116. .imr_mem = 0,
  117. }, {
  118. .freq = 50, /* Start freq, in MHz */
  119. .open_d = 0x08, /* low */
  120. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  121. .tf_c = 0xbe, /* R27[7:0] band4,band1 */
  122. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  123. .xtal_cap10p = 0x01,
  124. .xtal_cap0p = 0x00,
  125. .imr_mem = 0,
  126. }, {
  127. .freq = 55, /* 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 = 0x8b, /* R27[7:0] band7,band4 */
  131. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  132. .xtal_cap10p = 0x01,
  133. .xtal_cap0p = 0x00,
  134. .imr_mem = 0,
  135. }, {
  136. .freq = 60, /* 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 = 0x7b, /* R27[7:0] band8,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 = 65, /* 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 = 0x69, /* R27[7:0] band9,band6 */
  149. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  150. .xtal_cap10p = 0x01,
  151. .xtal_cap0p = 0x00,
  152. .imr_mem = 0,
  153. }, {
  154. .freq = 70, /* 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 = 0x58, /* R27[7:0] band10,band7 */
  158. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  159. .xtal_cap10p = 0x01,
  160. .xtal_cap0p = 0x00,
  161. .imr_mem = 0,
  162. }, {
  163. .freq = 75, /* Start freq, in MHz */
  164. .open_d = 0x00, /* high */
  165. .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
  166. .tf_c = 0x44, /* R27[7:0] band11,band11 */
  167. .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
  168. .xtal_cap10p = 0x01,
  169. .xtal_cap0p = 0x00,
  170. .imr_mem = 0,
  171. }, {
  172. .freq = 80, /* 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 = 90, /* 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 = 0x34, /* R27[7:0] band12,band11 */
  185. .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
  186. .xtal_cap10p = 0x01,
  187. .xtal_cap0p = 0x00,
  188. .imr_mem = 0,
  189. }, {
  190. .freq = 100, /* 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 = 110, /* 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 = 0x24, /* R27[7:0] band13,band11 */
  203. .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
  204. .xtal_cap10p = 0x01,
  205. .xtal_cap0p = 0x00,
  206. .imr_mem = 1,
  207. }, {
  208. .freq = 120, /* 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 = 140, /* 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 = 0x14, /* R27[7:0] band14,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 = 180, /* 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 = 0x13, /* R27[7:0] band14,band12 */
  230. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  231. .xtal_cap10p = 0x00,
  232. .xtal_cap0p = 0x00,
  233. .imr_mem = 1,
  234. }, {
  235. .freq = 220, /* 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 = 2,
  243. }, {
  244. .freq = 250, /* 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 = 0x11, /* R27[7:0] highest,highest */
  248. .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
  249. .xtal_cap10p = 0x00,
  250. .xtal_cap0p = 0x00,
  251. .imr_mem = 2,
  252. }, {
  253. .freq = 280, /* 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 = 0x00, /* 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 = 310, /* Start freq, in MHz */
  263. .open_d = 0x00, /* high */
  264. .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
  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 = 450, /* 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 = 3,
  279. }, {
  280. .freq = 588, /* Start freq, in MHz */
  281. .open_d = 0x00, /* high */
  282. .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
  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 = 650, /* 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 = 4,
  297. }
  298. };
  299. static int r820t_xtal_capacitor[][2] = {
  300. { 0x0b, XTAL_LOW_CAP_30P },
  301. { 0x02, XTAL_LOW_CAP_20P },
  302. { 0x01, XTAL_LOW_CAP_10P },
  303. { 0x00, XTAL_LOW_CAP_0P },
  304. { 0x10, XTAL_HIGH_CAP_0P },
  305. };
  306. /*
  307. * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
  308. * input power, for raw results see:
  309. * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
  310. */
  311. static const int r820t_lna_gain_steps[] = {
  312. 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
  313. };
  314. static const int r820t_mixer_gain_steps[] = {
  315. 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
  316. };
  317. /*
  318. * I2C read/write code and shadow registers logic
  319. */
  320. static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val,
  321. int len)
  322. {
  323. int r = reg - REG_SHADOW_START;
  324. if (r < 0) {
  325. len += r;
  326. r = 0;
  327. }
  328. if (len <= 0)
  329. return;
  330. if (len > NUM_REGS)
  331. len = NUM_REGS;
  332. tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
  333. __func__, r + REG_SHADOW_START, len, len, val);
  334. memcpy(&priv->regs[r], val, len);
  335. }
  336. static int r820t_write(struct r820t_priv *priv, u8 reg, const u8 *val,
  337. int len)
  338. {
  339. int rc, size, pos = 0;
  340. /* Store the shadow registers */
  341. shadow_store(priv, reg, val, len);
  342. do {
  343. if (len > priv->cfg->max_i2c_msg_len - 1)
  344. size = priv->cfg->max_i2c_msg_len - 1;
  345. else
  346. size = len;
  347. /* Fill I2C buffer */
  348. priv->buf[0] = reg;
  349. memcpy(&priv->buf[1], &val[pos], size);
  350. rc = tuner_i2c_xfer_send(&priv->i2c_props, priv->buf, size + 1);
  351. if (rc != size + 1) {
  352. tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
  353. __func__, rc, reg, size, size, &priv->buf[1]);
  354. if (rc < 0)
  355. return rc;
  356. return -EREMOTEIO;
  357. }
  358. tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
  359. __func__, reg, size, size, &priv->buf[1]);
  360. reg += size;
  361. len -= size;
  362. pos += size;
  363. } while (len > 0);
  364. return 0;
  365. }
  366. static int r820t_write_reg(struct r820t_priv *priv, u8 reg, u8 val)
  367. {
  368. return r820t_write(priv, reg, &val, 1);
  369. }
  370. static int r820t_write_reg_mask(struct r820t_priv *priv, u8 reg, u8 val,
  371. u8 bit_mask)
  372. {
  373. int r = reg - REG_SHADOW_START;
  374. if (r >= 0 && r < NUM_REGS)
  375. val = (priv->regs[r] & ~bit_mask) | (val & bit_mask);
  376. else
  377. return -EINVAL;
  378. return r820t_write(priv, reg, &val, 1);
  379. }
  380. static int r820_read(struct r820t_priv *priv, u8 reg, u8 *val, int len)
  381. {
  382. int rc, i;
  383. u8 *p = &priv->buf[1];
  384. priv->buf[0] = reg;
  385. rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, priv->buf, 1, p, len);
  386. if (rc != len) {
  387. tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
  388. __func__, rc, reg, len, len, p);
  389. if (rc < 0)
  390. return rc;
  391. return -EREMOTEIO;
  392. }
  393. /* Copy data to the output buffer */
  394. for (i = 0; i < len; i++)
  395. val[i] = bitrev8(p[i]);
  396. tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
  397. __func__, reg, len, len, val);
  398. return 0;
  399. }
  400. /*
  401. * r820t tuning logic
  402. */
  403. static int r820t_set_mux(struct r820t_priv *priv, u32 freq)
  404. {
  405. const struct r820t_freq_range *range;
  406. int i, rc;
  407. u8 val;
  408. /* Get the proper frequency range */
  409. freq = freq / 1000000;
  410. for (i = 0; i < ARRAY_SIZE(freq_ranges) - 1; i++) {
  411. if (freq < freq_ranges[i + 1].freq)
  412. break;
  413. }
  414. range = &freq_ranges[i];
  415. tuner_dbg("set r820t range#%d for frequency %d MHz\n", i, freq);
  416. /* Open Drain */
  417. rc = r820t_write_reg_mask(priv, 0x17, range->open_d, 0x08);
  418. if (rc < 0)
  419. return rc;
  420. /* RF_MUX,Polymux */
  421. rc = r820t_write_reg_mask(priv, 0x1a, range->rf_mux_ploy, 0xc3);
  422. if (rc < 0)
  423. return rc;
  424. /* TF BAND */
  425. rc = r820t_write_reg(priv, 0x1b, range->tf_c);
  426. if (rc < 0)
  427. return rc;
  428. /* XTAL CAP & Drive */
  429. switch (priv->xtal_cap_sel) {
  430. case XTAL_LOW_CAP_30P:
  431. case XTAL_LOW_CAP_20P:
  432. val = range->xtal_cap20p | 0x08;
  433. break;
  434. case XTAL_LOW_CAP_10P:
  435. val = range->xtal_cap10p | 0x08;
  436. break;
  437. case XTAL_HIGH_CAP_0P:
  438. val = range->xtal_cap0p | 0x00;
  439. break;
  440. default:
  441. case XTAL_LOW_CAP_0P:
  442. val = range->xtal_cap0p | 0x08;
  443. break;
  444. }
  445. rc = r820t_write_reg_mask(priv, 0x10, val, 0x0b);
  446. if (rc < 0)
  447. return rc;
  448. /*
  449. * FIXME: the original driver has a logic there with preserves
  450. * gain/phase from registers 8 and 9 reading the data from the
  451. * registers before writing, if "IMF done". That code was sort of
  452. * commented there, as the flag is always false.
  453. */
  454. rc = r820t_write_reg_mask(priv, 0x08, 0, 0x3f);
  455. if (rc < 0)
  456. return rc;
  457. rc = r820t_write_reg_mask(priv, 0x09, 0, 0x3f);
  458. return rc;
  459. }
  460. static int r820t_set_pll(struct r820t_priv *priv, u32 freq)
  461. {
  462. u64 tmp64, vco_freq;
  463. int rc, i;
  464. u32 vco_fra; /* VCO contribution by SDM (kHz) */
  465. u32 vco_min = 1770000;
  466. u32 vco_max = vco_min * 2;
  467. u32 pll_ref;
  468. u16 n_sdm = 2;
  469. u16 sdm = 0;
  470. u8 mix_div = 2;
  471. u8 div_buf = 0;
  472. u8 div_num = 0;
  473. u8 ni, si, nint, vco_fine_tune, val;
  474. u8 data[5];
  475. freq = freq / 1000; /* Frequency in kHz */
  476. pll_ref = priv->cfg->xtal / 1000;
  477. tuner_dbg("set r820t pll for frequency %d kHz = %d\n", freq, pll_ref);
  478. /* FIXME: this seems to be a hack - probably it can be removed */
  479. rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x00);
  480. if (rc < 0)
  481. return rc;
  482. /* set pll autotune = 128kHz */
  483. rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
  484. if (rc < 0)
  485. return rc;
  486. /* set VCO current = 100 */
  487. rc = r820t_write_reg_mask(priv, 0x12, 0x80, 0xe0);
  488. if (rc < 0)
  489. return rc;
  490. /* Calculate divider */
  491. while (mix_div <= 64) {
  492. if (((freq * mix_div) >= vco_min) &&
  493. ((freq * mix_div) < vco_max)) {
  494. div_buf = mix_div;
  495. while (div_buf > 2) {
  496. div_buf = div_buf >> 1;
  497. div_num++;
  498. }
  499. break;
  500. }
  501. mix_div = mix_div << 1;
  502. }
  503. rc = r820_read(priv, 0x00, data, sizeof(data));
  504. if (rc < 0)
  505. return rc;
  506. vco_fine_tune = (data[4] & 0x30) >> 4;
  507. if (vco_fine_tune > VCO_POWER_REF)
  508. div_num = div_num - 1;
  509. else if (vco_fine_tune < VCO_POWER_REF)
  510. div_num = div_num + 1;
  511. rc = r820t_write_reg_mask(priv, 0x10, div_num << 5, 0xe0);
  512. if (rc < 0)
  513. return rc;
  514. vco_freq = (u64)(freq * (u64)mix_div);
  515. tmp64 = vco_freq;
  516. do_div(tmp64, 2 * pll_ref);
  517. nint = (u8)tmp64;
  518. tmp64 = vco_freq - ((u64)2) * pll_ref * nint;
  519. do_div(tmp64, 1000);
  520. vco_fra = (u16)(tmp64);
  521. pll_ref /= 1000;
  522. /* boundary spur prevention */
  523. if (vco_fra < pll_ref / 64) {
  524. vco_fra = 0;
  525. } else if (vco_fra > pll_ref * 127 / 64) {
  526. vco_fra = 0;
  527. nint++;
  528. } else if ((vco_fra > pll_ref * 127 / 128) && (vco_fra < pll_ref)) {
  529. vco_fra = pll_ref * 127 / 128;
  530. } else if ((vco_fra > pll_ref) && (vco_fra < pll_ref * 129 / 128)) {
  531. vco_fra = pll_ref * 129 / 128;
  532. }
  533. if (nint > 63) {
  534. tuner_info("No valid PLL values for %u kHz!\n", freq);
  535. return -EINVAL;
  536. }
  537. ni = (nint - 13) / 4;
  538. si = nint - 4 * ni - 13;
  539. rc = r820t_write_reg(priv, 0x14, ni + (si << 6));
  540. if (rc < 0)
  541. return rc;
  542. /* pw_sdm */
  543. if (!vco_fra)
  544. val = 0x08;
  545. else
  546. val = 0x00;
  547. rc = r820t_write_reg_mask(priv, 0x12, val, 0x08);
  548. if (rc < 0)
  549. return rc;
  550. /* sdm calculator */
  551. while (vco_fra > 1) {
  552. if (vco_fra > (2 * pll_ref / n_sdm)) {
  553. sdm = sdm + 32768 / (n_sdm / 2);
  554. vco_fra = vco_fra - 2 * pll_ref / n_sdm;
  555. if (n_sdm >= 0x8000)
  556. break;
  557. }
  558. n_sdm = n_sdm << 1;
  559. }
  560. rc = r820t_write_reg_mask(priv, 0x16, sdm >> 8, 0x08);
  561. if (rc < 0)
  562. return rc;
  563. rc = r820t_write_reg_mask(priv, 0x15, sdm & 0xff, 0x08);
  564. if (rc < 0)
  565. return rc;
  566. for (i = 0; i < 2; i++) {
  567. /*
  568. * FIXME: Rafael chips R620D, R828D and R828 seems to
  569. * need 20 ms for analog TV
  570. */
  571. msleep(10);
  572. /* Check if PLL has locked */
  573. rc = r820_read(priv, 0x00, data, 3);
  574. if (rc < 0)
  575. return rc;
  576. if (data[2] & 0x40)
  577. break;
  578. if (!i) {
  579. /* Didn't lock. Increase VCO current */
  580. rc = r820t_write_reg_mask(priv, 0x12, 0x60, 0xe0);
  581. if (rc < 0)
  582. return rc;
  583. }
  584. }
  585. if (!(data[2] & 0x40)) {
  586. priv->has_lock = false;
  587. return 0;
  588. }
  589. priv->has_lock = true;
  590. tuner_dbg("tuner has lock at frequency %d kHz\n", freq);
  591. /* set pll autotune = 8kHz */
  592. rc = r820t_write_reg_mask(priv, 0x1a, 0x08, 0x08);
  593. return rc;
  594. }
  595. static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq,
  596. enum v4l2_tuner_type type,
  597. v4l2_std_id std,
  598. u32 delsys)
  599. {
  600. int rc;
  601. u8 mixer_top, lna_top, cp_cur, div_buf_cur, lna_vth_l, mixer_vth_l;
  602. u8 air_cable1_in, cable2_in, pre_dect, lna_discharge, filter_cur;
  603. tuner_dbg("adjusting tuner parameters for the standard\n");
  604. switch (delsys) {
  605. case SYS_DVBT:
  606. if ((freq == 506000000) || (freq == 666000000) ||
  607. (freq == 818000000)) {
  608. mixer_top = 0x14; /* mixer top:14 , top-1, low-discharge */
  609. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  610. cp_cur = 0x28; /* 101, 0.2 */
  611. div_buf_cur = 0x20; /* 10, 200u */
  612. } else {
  613. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  614. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  615. cp_cur = 0x38; /* 111, auto */
  616. div_buf_cur = 0x30; /* 11, 150u */
  617. }
  618. lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
  619. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  620. air_cable1_in = 0x00;
  621. cable2_in = 0x00;
  622. pre_dect = 0x40;
  623. lna_discharge = 14;
  624. filter_cur = 0x40; /* 10, low */
  625. break;
  626. case SYS_DVBT2:
  627. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  628. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  629. lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
  630. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  631. air_cable1_in = 0x00;
  632. cable2_in = 0x00;
  633. pre_dect = 0x40;
  634. lna_discharge = 14;
  635. cp_cur = 0x38; /* 111, auto */
  636. div_buf_cur = 0x30; /* 11, 150u */
  637. filter_cur = 0x40; /* 10, low */
  638. break;
  639. case SYS_ISDBT:
  640. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  641. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  642. lna_vth_l = 0x75; /* lna vth 1.04 , vtl 0.84 */
  643. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  644. air_cable1_in = 0x00;
  645. cable2_in = 0x00;
  646. pre_dect = 0x40;
  647. lna_discharge = 14;
  648. cp_cur = 0x38; /* 111, auto */
  649. div_buf_cur = 0x30; /* 11, 150u */
  650. filter_cur = 0x40; /* 10, low */
  651. break;
  652. default: /* DVB-T 8M */
  653. mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
  654. lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
  655. lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
  656. mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
  657. air_cable1_in = 0x00;
  658. cable2_in = 0x00;
  659. pre_dect = 0x40;
  660. lna_discharge = 14;
  661. cp_cur = 0x38; /* 111, auto */
  662. div_buf_cur = 0x30; /* 11, 150u */
  663. filter_cur = 0x40; /* 10, low */
  664. break;
  665. }
  666. rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7);
  667. if (rc < 0)
  668. return rc;
  669. rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0xf8);
  670. if (rc < 0)
  671. return rc;
  672. rc = r820t_write_reg(priv, 0x0d, lna_vth_l);
  673. if (rc < 0)
  674. return rc;
  675. rc = r820t_write_reg(priv, 0x0e, mixer_vth_l);
  676. if (rc < 0)
  677. return rc;
  678. /* Air-IN only for Astrometa */
  679. rc = r820t_write_reg_mask(priv, 0x05, air_cable1_in, 0x60);
  680. if (rc < 0)
  681. return rc;
  682. rc = r820t_write_reg_mask(priv, 0x06, cable2_in, 0x08);
  683. if (rc < 0)
  684. return rc;
  685. rc = r820t_write_reg_mask(priv, 0x11, cp_cur, 0x38);
  686. if (rc < 0)
  687. return rc;
  688. rc = r820t_write_reg_mask(priv, 0x17, div_buf_cur, 0x30);
  689. if (rc < 0)
  690. return rc;
  691. rc = r820t_write_reg_mask(priv, 0x0a, filter_cur, 0x60);
  692. if (rc < 0)
  693. return rc;
  694. /*
  695. * Original driver initializes regs 0x05 and 0x06 with the
  696. * same value again on this point. Probably, it is just an
  697. * error there
  698. */
  699. /*
  700. * Set LNA
  701. */
  702. tuner_dbg("adjusting LNA parameters\n");
  703. if (type != V4L2_TUNER_ANALOG_TV) {
  704. /* LNA TOP: lowest */
  705. rc = r820t_write_reg_mask(priv, 0x1d, 0, 0x38);
  706. if (rc < 0)
  707. return rc;
  708. /* 0: normal mode */
  709. rc = r820t_write_reg_mask(priv, 0x1c, 0, 0x04);
  710. if (rc < 0)
  711. return rc;
  712. /* 0: PRE_DECT off */
  713. rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
  714. if (rc < 0)
  715. return rc;
  716. /* agc clk 250hz */
  717. rc = r820t_write_reg_mask(priv, 0x1a, 0x30, 0x30);
  718. if (rc < 0)
  719. return rc;
  720. msleep(250);
  721. /* write LNA TOP = 3 */
  722. rc = r820t_write_reg_mask(priv, 0x1d, 0x18, 0x38);
  723. if (rc < 0)
  724. return rc;
  725. /*
  726. * write discharge mode
  727. * FIXME: IMHO, the mask here is wrong, but it matches
  728. * what's there at the original driver
  729. */
  730. rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
  731. if (rc < 0)
  732. return rc;
  733. /* LNA discharge current */
  734. rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
  735. if (rc < 0)
  736. return rc;
  737. /* agc clk 60hz */
  738. rc = r820t_write_reg_mask(priv, 0x1a, 0x20, 0x30);
  739. if (rc < 0)
  740. return rc;
  741. } else {
  742. /* PRE_DECT off */
  743. rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
  744. if (rc < 0)
  745. return rc;
  746. /* write LNA TOP */
  747. rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0x38);
  748. if (rc < 0)
  749. return rc;
  750. /*
  751. * write discharge mode
  752. * FIXME: IMHO, the mask here is wrong, but it matches
  753. * what's there at the original driver
  754. */
  755. rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
  756. if (rc < 0)
  757. return rc;
  758. /* LNA discharge current */
  759. rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
  760. if (rc < 0)
  761. return rc;
  762. /* agc clk 1Khz, external det1 cap 1u */
  763. rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x30);
  764. if (rc < 0)
  765. return rc;
  766. rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x04);
  767. if (rc < 0)
  768. return rc;
  769. }
  770. return 0;
  771. }
  772. static int r820t_set_tv_standard(struct r820t_priv *priv,
  773. unsigned bw,
  774. enum v4l2_tuner_type type,
  775. v4l2_std_id std, u32 delsys)
  776. {
  777. int rc, i;
  778. u32 if_khz, filt_cal_lo;
  779. u8 data[5], val;
  780. u8 filt_gain, img_r, filt_q, hp_cor, ext_enable, loop_through;
  781. u8 lt_att, flt_ext_widest, polyfil_cur;
  782. bool need_calibration;
  783. tuner_dbg("selecting the delivery system\n");
  784. if (delsys == SYS_ISDBT) {
  785. if_khz = 4063;
  786. filt_cal_lo = 59000;
  787. filt_gain = 0x10; /* +3db, 6mhz on */
  788. img_r = 0x00; /* image negative */
  789. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  790. hp_cor = 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
  791. ext_enable = 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
  792. loop_through = 0x00; /* r5[7], lt on */
  793. lt_att = 0x00; /* r31[7], lt att enable */
  794. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  795. polyfil_cur = 0x60; /* r25[6:5]:min */
  796. } else {
  797. if (bw <= 6) {
  798. if_khz = 3570;
  799. filt_cal_lo = 56000; /* 52000->56000 */
  800. filt_gain = 0x10; /* +3db, 6mhz on */
  801. img_r = 0x00; /* image negative */
  802. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  803. hp_cor = 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
  804. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  805. loop_through = 0x00; /* r5[7], lt on */
  806. lt_att = 0x00; /* r31[7], lt att enable */
  807. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  808. polyfil_cur = 0x60; /* r25[6:5]:min */
  809. } else if (bw == 7) {
  810. #if 0
  811. /*
  812. * There are two 7 MHz tables defined on the original
  813. * driver, but just the second one seems to be visible
  814. * by rtl2832. Keep this one here commented, as it
  815. * might be needed in the future
  816. */
  817. if_khz = 4070;
  818. filt_cal_lo = 60000;
  819. filt_gain = 0x10; /* +3db, 6mhz on */
  820. img_r = 0x00; /* image negative */
  821. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  822. hp_cor = 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
  823. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  824. loop_through = 0x00; /* r5[7], lt on */
  825. lt_att = 0x00; /* r31[7], lt att enable */
  826. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  827. polyfil_cur = 0x60; /* r25[6:5]:min */
  828. #endif
  829. /* 7 MHz, second table */
  830. if_khz = 4570;
  831. filt_cal_lo = 63000;
  832. filt_gain = 0x10; /* +3db, 6mhz on */
  833. img_r = 0x00; /* image negative */
  834. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  835. hp_cor = 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
  836. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  837. loop_through = 0x00; /* r5[7], lt on */
  838. lt_att = 0x00; /* r31[7], lt att enable */
  839. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  840. polyfil_cur = 0x60; /* r25[6:5]:min */
  841. } else {
  842. if_khz = 4570;
  843. filt_cal_lo = 68500;
  844. filt_gain = 0x10; /* +3db, 6mhz on */
  845. img_r = 0x00; /* image negative */
  846. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  847. hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
  848. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  849. loop_through = 0x00; /* r5[7], lt on */
  850. lt_att = 0x00; /* r31[7], lt att enable */
  851. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  852. polyfil_cur = 0x60; /* r25[6:5]:min */
  853. }
  854. }
  855. /* Initialize the shadow registers */
  856. memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
  857. /* Init Flag & Xtal_check Result */
  858. if (priv->imr_done)
  859. val = 1 | priv->xtal_cap_sel << 1;
  860. else
  861. val = 0;
  862. rc = r820t_write_reg_mask(priv, 0x0c, val, 0x0f);
  863. if (rc < 0)
  864. return rc;
  865. /* version */
  866. rc = r820t_write_reg_mask(priv, 0x13, VER_NUM, 0x3f);
  867. if (rc < 0)
  868. return rc;
  869. /* for LT Gain test */
  870. if (type != V4L2_TUNER_ANALOG_TV) {
  871. rc = r820t_write_reg_mask(priv, 0x1d, 0x00, 0x38);
  872. if (rc < 0)
  873. return rc;
  874. msleep(1);
  875. }
  876. priv->int_freq = if_khz * 1000;
  877. /* Check if standard changed. If so, filter calibration is needed */
  878. if (type != priv->type)
  879. need_calibration = true;
  880. else if ((type == V4L2_TUNER_ANALOG_TV) && (std != priv->std))
  881. need_calibration = true;
  882. else if ((type == V4L2_TUNER_DIGITAL_TV) &&
  883. ((delsys != priv->delsys) || bw != priv->bw))
  884. need_calibration = true;
  885. else
  886. need_calibration = false;
  887. if (need_calibration) {
  888. tuner_dbg("calibrating the tuner\n");
  889. for (i = 0; i < 2; i++) {
  890. /* Set filt_cap */
  891. rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x60);
  892. if (rc < 0)
  893. return rc;
  894. /* set cali clk =on */
  895. rc = r820t_write_reg_mask(priv, 0x0f, 0x04, 0x04);
  896. if (rc < 0)
  897. return rc;
  898. /* X'tal cap 0pF for PLL */
  899. rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x03);
  900. if (rc < 0)
  901. return rc;
  902. rc = r820t_set_pll(priv, filt_cal_lo);
  903. if (rc < 0 || !priv->has_lock)
  904. return rc;
  905. /* Start Trigger */
  906. rc = r820t_write_reg_mask(priv, 0x0b, 0x10, 0x10);
  907. if (rc < 0)
  908. return rc;
  909. msleep(1);
  910. /* Stop Trigger */
  911. rc = r820t_write_reg_mask(priv, 0x0b, 0x00, 0x10);
  912. if (rc < 0)
  913. return rc;
  914. /* set cali clk =off */
  915. rc = r820t_write_reg_mask(priv, 0x0f, 0x00, 0x04);
  916. if (rc < 0)
  917. return rc;
  918. /* Check if calibration worked */
  919. rc = r820_read(priv, 0x00, data, sizeof(data));
  920. if (rc < 0)
  921. return rc;
  922. priv->fil_cal_code = data[4] & 0x0f;
  923. if (priv->fil_cal_code && priv->fil_cal_code != 0x0f)
  924. break;
  925. }
  926. /* narrowest */
  927. if (priv->fil_cal_code == 0x0f)
  928. priv->fil_cal_code = 0;
  929. }
  930. rc = r820t_write_reg_mask(priv, 0x0a,
  931. filt_q | priv->fil_cal_code, 0x1f);
  932. if (rc < 0)
  933. return rc;
  934. /* Set BW, Filter_gain, & HP corner */
  935. rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x10);
  936. if (rc < 0)
  937. return rc;
  938. /* Set Img_R */
  939. rc = r820t_write_reg_mask(priv, 0x07, img_r, 0x80);
  940. if (rc < 0)
  941. return rc;
  942. /* Set filt_3dB, V6MHz */
  943. rc = r820t_write_reg_mask(priv, 0x06, filt_gain, 0x30);
  944. if (rc < 0)
  945. return rc;
  946. /* channel filter extension */
  947. rc = r820t_write_reg_mask(priv, 0x1e, ext_enable, 0x60);
  948. if (rc < 0)
  949. return rc;
  950. /* Loop through */
  951. rc = r820t_write_reg_mask(priv, 0x05, loop_through, 0x80);
  952. if (rc < 0)
  953. return rc;
  954. /* Loop through attenuation */
  955. rc = r820t_write_reg_mask(priv, 0x1f, lt_att, 0x80);
  956. if (rc < 0)
  957. return rc;
  958. /* filter extension widest */
  959. rc = r820t_write_reg_mask(priv, 0x0f, flt_ext_widest, 0x80);
  960. if (rc < 0)
  961. return rc;
  962. /* RF poly filter current */
  963. rc = r820t_write_reg_mask(priv, 0x19, polyfil_cur, 0x60);
  964. if (rc < 0)
  965. return rc;
  966. /* Store current standard. If it changes, re-calibrate the tuner */
  967. priv->delsys = delsys;
  968. priv->type = type;
  969. priv->std = std;
  970. priv->bw = bw;
  971. return 0;
  972. }
  973. static int r820t_read_gain(struct r820t_priv *priv)
  974. {
  975. u8 data[4];
  976. int rc;
  977. rc = r820_read(priv, 0x00, data, sizeof(data));
  978. if (rc < 0)
  979. return rc;
  980. return ((data[3] & 0x0f) << 1) + ((data[3] & 0xf0) >> 4);
  981. }
  982. static int r820t_set_gain_mode(struct r820t_priv *priv,
  983. bool set_manual_gain,
  984. int gain)
  985. {
  986. int rc;
  987. if (set_manual_gain) {
  988. int i, total_gain = 0;
  989. uint8_t mix_index = 0, lna_index = 0;
  990. u8 data[4];
  991. /* LNA auto off */
  992. rc = r820t_write_reg_mask(priv, 0x05, 0x10, 0x10);
  993. if (rc < 0)
  994. return rc;
  995. /* Mixer auto off */
  996. rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
  997. if (rc < 0)
  998. return rc;
  999. rc = r820_read(priv, 0x00, data, sizeof(data));
  1000. if (rc < 0)
  1001. return rc;
  1002. /* set fixed VGA gain for now (16.3 dB) */
  1003. rc = r820t_write_reg_mask(priv, 0x0c, 0x08, 0x9f);
  1004. if (rc < 0)
  1005. return rc;
  1006. for (i = 0; i < 15; i++) {
  1007. if (total_gain >= gain)
  1008. break;
  1009. total_gain += r820t_lna_gain_steps[++lna_index];
  1010. if (total_gain >= gain)
  1011. break;
  1012. total_gain += r820t_mixer_gain_steps[++mix_index];
  1013. }
  1014. /* set LNA gain */
  1015. rc = r820t_write_reg_mask(priv, 0x05, lna_index, 0x0f);
  1016. if (rc < 0)
  1017. return rc;
  1018. /* set Mixer gain */
  1019. rc = r820t_write_reg_mask(priv, 0x07, mix_index, 0x0f);
  1020. if (rc < 0)
  1021. return rc;
  1022. } else {
  1023. /* LNA */
  1024. rc = r820t_write_reg_mask(priv, 0x05, 0, 0xef);
  1025. if (rc < 0)
  1026. return rc;
  1027. /* Mixer */
  1028. rc = r820t_write_reg_mask(priv, 0x07, 0x10, 0xef);
  1029. if (rc < 0)
  1030. return rc;
  1031. /* set fixed VGA gain for now (26.5 dB) */
  1032. rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
  1033. if (rc < 0)
  1034. return rc;
  1035. }
  1036. return 0;
  1037. }
  1038. static int generic_set_freq(struct dvb_frontend *fe,
  1039. u32 freq /* in HZ */,
  1040. unsigned bw,
  1041. enum v4l2_tuner_type type,
  1042. v4l2_std_id std, u32 delsys)
  1043. {
  1044. struct r820t_priv *priv = fe->tuner_priv;
  1045. int rc = -EINVAL;
  1046. u32 lo_freq;
  1047. tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
  1048. freq / 1000, bw);
  1049. rc = r820t_set_tv_standard(priv, bw, type, std, delsys);
  1050. if (rc < 0)
  1051. goto err;
  1052. if ((type == V4L2_TUNER_ANALOG_TV) && (std == V4L2_STD_SECAM_LC))
  1053. lo_freq = freq - priv->int_freq;
  1054. else
  1055. lo_freq = freq + priv->int_freq;
  1056. rc = r820t_set_mux(priv, lo_freq);
  1057. if (rc < 0)
  1058. goto err;
  1059. rc = r820t_set_gain_mode(priv, true, 0);
  1060. if (rc < 0)
  1061. goto err;
  1062. rc = r820t_set_pll(priv, lo_freq);
  1063. if (rc < 0 || !priv->has_lock)
  1064. goto err;
  1065. rc = r820t_sysfreq_sel(priv, freq, type, std, delsys);
  1066. if (rc < 0)
  1067. goto err;
  1068. tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
  1069. __func__, freq, r820t_read_gain(priv));
  1070. err:
  1071. if (rc < 0)
  1072. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1073. return rc;
  1074. }
  1075. /*
  1076. * r820t standby logic
  1077. */
  1078. static int r820t_standby(struct r820t_priv *priv)
  1079. {
  1080. int rc;
  1081. rc = r820t_write_reg(priv, 0x06, 0xb1);
  1082. if (rc < 0)
  1083. return rc;
  1084. rc = r820t_write_reg(priv, 0x05, 0x03);
  1085. if (rc < 0)
  1086. return rc;
  1087. rc = r820t_write_reg(priv, 0x07, 0x3a);
  1088. if (rc < 0)
  1089. return rc;
  1090. rc = r820t_write_reg(priv, 0x08, 0x40);
  1091. if (rc < 0)
  1092. return rc;
  1093. rc = r820t_write_reg(priv, 0x09, 0xc0);
  1094. if (rc < 0)
  1095. return rc;
  1096. rc = r820t_write_reg(priv, 0x0a, 0x36);
  1097. if (rc < 0)
  1098. return rc;
  1099. rc = r820t_write_reg(priv, 0x0c, 0x35);
  1100. if (rc < 0)
  1101. return rc;
  1102. rc = r820t_write_reg(priv, 0x0f, 0x68);
  1103. if (rc < 0)
  1104. return rc;
  1105. rc = r820t_write_reg(priv, 0x11, 0x03);
  1106. if (rc < 0)
  1107. return rc;
  1108. rc = r820t_write_reg(priv, 0x17, 0xf4);
  1109. if (rc < 0)
  1110. return rc;
  1111. rc = r820t_write_reg(priv, 0x19, 0x0c);
  1112. /* Force initial calibration */
  1113. priv->type = -1;
  1114. return rc;
  1115. }
  1116. /*
  1117. * r820t device init logic
  1118. */
  1119. static int r820t_xtal_check(struct r820t_priv *priv)
  1120. {
  1121. int rc, i;
  1122. u8 data[3], val;
  1123. /* Initialize the shadow registers */
  1124. memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
  1125. /* cap 30pF & Drive Low */
  1126. rc = r820t_write_reg_mask(priv, 0x10, 0x0b, 0x0b);
  1127. if (rc < 0)
  1128. return rc;
  1129. /* set pll autotune = 128kHz */
  1130. rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
  1131. if (rc < 0)
  1132. return rc;
  1133. /* set manual initial reg = 111111; */
  1134. rc = r820t_write_reg_mask(priv, 0x13, 0x7f, 0x7f);
  1135. if (rc < 0)
  1136. return rc;
  1137. /* set auto */
  1138. rc = r820t_write_reg_mask(priv, 0x13, 0x00, 0x40);
  1139. if (rc < 0)
  1140. return rc;
  1141. /* Try several xtal capacitor alternatives */
  1142. for (i = 0; i < ARRAY_SIZE(r820t_xtal_capacitor); i++) {
  1143. rc = r820t_write_reg_mask(priv, 0x10,
  1144. r820t_xtal_capacitor[i][0], 0x1b);
  1145. if (rc < 0)
  1146. return rc;
  1147. msleep(5);
  1148. rc = r820_read(priv, 0x00, data, sizeof(data));
  1149. if (rc < 0)
  1150. return rc;
  1151. if ((!data[2]) & 0x40)
  1152. continue;
  1153. val = data[2] & 0x3f;
  1154. if (priv->cfg->xtal == 16000000 && (val > 29 || val < 23))
  1155. break;
  1156. if (val != 0x3f)
  1157. break;
  1158. }
  1159. if (i == ARRAY_SIZE(r820t_xtal_capacitor))
  1160. return -EINVAL;
  1161. return r820t_xtal_capacitor[i][1];
  1162. }
  1163. /*
  1164. * r820t frontend operations and tuner attach code
  1165. *
  1166. * All driver locks and i2c control are only in this part of the code
  1167. */
  1168. static int r820t_init(struct dvb_frontend *fe)
  1169. {
  1170. struct r820t_priv *priv = fe->tuner_priv;
  1171. int rc, i;
  1172. int xtal_cap = 0;
  1173. tuner_dbg("%s:\n", __func__);
  1174. mutex_lock(&priv->lock);
  1175. if (fe->ops.i2c_gate_ctrl)
  1176. fe->ops.i2c_gate_ctrl(fe, 1);
  1177. if ((priv->cfg->rafael_chip == CHIP_R820T) ||
  1178. (priv->cfg->rafael_chip == CHIP_R828S) ||
  1179. (priv->cfg->rafael_chip == CHIP_R820C)) {
  1180. priv->xtal_cap_sel = XTAL_HIGH_CAP_0P;
  1181. } else {
  1182. for (i = 0; i < 3; i++) {
  1183. rc = r820t_xtal_check(priv);
  1184. if (rc < 0)
  1185. goto err;
  1186. if (!i || rc > xtal_cap)
  1187. xtal_cap = rc;
  1188. }
  1189. priv->xtal_cap_sel = xtal_cap;
  1190. }
  1191. /* Initialize registers */
  1192. rc = r820t_write(priv, 0x05,
  1193. r820t_init_array, sizeof(r820t_init_array));
  1194. err:
  1195. if (fe->ops.i2c_gate_ctrl)
  1196. fe->ops.i2c_gate_ctrl(fe, 0);
  1197. mutex_unlock(&priv->lock);
  1198. if (rc < 0)
  1199. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1200. return rc;
  1201. }
  1202. static int r820t_sleep(struct dvb_frontend *fe)
  1203. {
  1204. struct r820t_priv *priv = fe->tuner_priv;
  1205. int rc;
  1206. tuner_dbg("%s:\n", __func__);
  1207. mutex_lock(&priv->lock);
  1208. if (fe->ops.i2c_gate_ctrl)
  1209. fe->ops.i2c_gate_ctrl(fe, 1);
  1210. rc = r820t_standby(priv);
  1211. if (fe->ops.i2c_gate_ctrl)
  1212. fe->ops.i2c_gate_ctrl(fe, 0);
  1213. mutex_unlock(&priv->lock);
  1214. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1215. return rc;
  1216. }
  1217. static int r820t_set_analog_freq(struct dvb_frontend *fe,
  1218. struct analog_parameters *p)
  1219. {
  1220. struct r820t_priv *priv = fe->tuner_priv;
  1221. unsigned bw;
  1222. int rc;
  1223. tuner_dbg("%s called\n", __func__);
  1224. /* if std is not defined, choose one */
  1225. if (!p->std)
  1226. p->std = V4L2_STD_MN;
  1227. if ((p->std == V4L2_STD_PAL_M) || (p->std == V4L2_STD_NTSC))
  1228. bw = 6;
  1229. else
  1230. bw = 8;
  1231. mutex_lock(&priv->lock);
  1232. if (fe->ops.i2c_gate_ctrl)
  1233. fe->ops.i2c_gate_ctrl(fe, 1);
  1234. rc = generic_set_freq(fe, 62500l * p->frequency, bw,
  1235. V4L2_TUNER_ANALOG_TV, p->std, SYS_UNDEFINED);
  1236. if (fe->ops.i2c_gate_ctrl)
  1237. fe->ops.i2c_gate_ctrl(fe, 0);
  1238. mutex_unlock(&priv->lock);
  1239. return rc;
  1240. }
  1241. static int r820t_set_params(struct dvb_frontend *fe)
  1242. {
  1243. struct r820t_priv *priv = fe->tuner_priv;
  1244. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1245. int rc;
  1246. unsigned bw;
  1247. tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  1248. __func__, c->delivery_system, c->frequency, c->bandwidth_hz);
  1249. mutex_lock(&priv->lock);
  1250. if (fe->ops.i2c_gate_ctrl)
  1251. fe->ops.i2c_gate_ctrl(fe, 1);
  1252. bw = (c->bandwidth_hz + 500000) / 1000000;
  1253. if (!bw)
  1254. bw = 8;
  1255. rc = generic_set_freq(fe, c->frequency, bw,
  1256. V4L2_TUNER_DIGITAL_TV, 0, c->delivery_system);
  1257. if (fe->ops.i2c_gate_ctrl)
  1258. fe->ops.i2c_gate_ctrl(fe, 0);
  1259. mutex_unlock(&priv->lock);
  1260. if (rc)
  1261. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1262. return rc;
  1263. }
  1264. static int r820t_signal(struct dvb_frontend *fe, u16 *strength)
  1265. {
  1266. struct r820t_priv *priv = fe->tuner_priv;
  1267. int rc = 0;
  1268. mutex_lock(&priv->lock);
  1269. if (fe->ops.i2c_gate_ctrl)
  1270. fe->ops.i2c_gate_ctrl(fe, 1);
  1271. if (priv->has_lock) {
  1272. rc = r820t_read_gain(priv);
  1273. if (rc < 0)
  1274. goto err;
  1275. /* A higher gain at LNA means a lower signal strength */
  1276. *strength = (45 - rc) << 4 | 0xff;
  1277. } else {
  1278. *strength = 0;
  1279. }
  1280. err:
  1281. if (fe->ops.i2c_gate_ctrl)
  1282. fe->ops.i2c_gate_ctrl(fe, 0);
  1283. mutex_unlock(&priv->lock);
  1284. tuner_dbg("%s: %s, gain=%d strength=%d\n",
  1285. __func__,
  1286. priv->has_lock ? "PLL locked" : "no signal",
  1287. rc, *strength);
  1288. return 0;
  1289. }
  1290. static int r820t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  1291. {
  1292. struct r820t_priv *priv = fe->tuner_priv;
  1293. tuner_dbg("%s:\n", __func__);
  1294. *frequency = priv->int_freq;
  1295. return 0;
  1296. }
  1297. static int r820t_release(struct dvb_frontend *fe)
  1298. {
  1299. struct r820t_priv *priv = fe->tuner_priv;
  1300. tuner_dbg("%s:\n", __func__);
  1301. mutex_lock(&r820t_list_mutex);
  1302. if (priv)
  1303. hybrid_tuner_release_state(priv);
  1304. mutex_unlock(&r820t_list_mutex);
  1305. fe->tuner_priv = NULL;
  1306. kfree(fe->tuner_priv);
  1307. return 0;
  1308. }
  1309. static const struct dvb_tuner_ops r820t_tuner_ops = {
  1310. .info = {
  1311. .name = "Rafael Micro R820T",
  1312. .frequency_min = 42000000,
  1313. .frequency_max = 1002000000,
  1314. },
  1315. .init = r820t_init,
  1316. .release = r820t_release,
  1317. .sleep = r820t_sleep,
  1318. .set_params = r820t_set_params,
  1319. .set_analog_params = r820t_set_analog_freq,
  1320. .get_if_frequency = r820t_get_if_frequency,
  1321. .get_rf_strength = r820t_signal,
  1322. };
  1323. struct dvb_frontend *r820t_attach(struct dvb_frontend *fe,
  1324. struct i2c_adapter *i2c,
  1325. const struct r820t_config *cfg)
  1326. {
  1327. struct r820t_priv *priv;
  1328. int rc = -ENODEV;
  1329. u8 data[5];
  1330. int instance;
  1331. mutex_lock(&r820t_list_mutex);
  1332. instance = hybrid_tuner_request_state(struct r820t_priv, priv,
  1333. hybrid_tuner_instance_list,
  1334. i2c, cfg->i2c_addr,
  1335. "r820t");
  1336. switch (instance) {
  1337. case 0:
  1338. /* memory allocation failure */
  1339. goto err_no_gate;
  1340. break;
  1341. case 1:
  1342. /* new tuner instance */
  1343. priv->cfg = cfg;
  1344. mutex_init(&priv->lock);
  1345. fe->tuner_priv = priv;
  1346. break;
  1347. case 2:
  1348. /* existing tuner instance */
  1349. fe->tuner_priv = priv;
  1350. break;
  1351. }
  1352. memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops, sizeof(r820t_tuner_ops));
  1353. if (fe->ops.i2c_gate_ctrl)
  1354. fe->ops.i2c_gate_ctrl(fe, 1);
  1355. /* check if the tuner is there */
  1356. rc = r820_read(priv, 0x00, data, sizeof(data));
  1357. if (rc < 0)
  1358. goto err;
  1359. rc = r820t_sleep(fe);
  1360. if (rc < 0)
  1361. goto err;
  1362. tuner_info("Rafael Micro r820t successfully identified\n");
  1363. fe->tuner_priv = priv;
  1364. memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops,
  1365. sizeof(struct dvb_tuner_ops));
  1366. if (fe->ops.i2c_gate_ctrl)
  1367. fe->ops.i2c_gate_ctrl(fe, 0);
  1368. mutex_unlock(&r820t_list_mutex);
  1369. return fe;
  1370. err:
  1371. if (fe->ops.i2c_gate_ctrl)
  1372. fe->ops.i2c_gate_ctrl(fe, 0);
  1373. err_no_gate:
  1374. mutex_unlock(&r820t_list_mutex);
  1375. tuner_info("%s: failed=%d\n", __func__, rc);
  1376. r820t_release(fe);
  1377. return NULL;
  1378. }
  1379. EXPORT_SYMBOL_GPL(r820t_attach);
  1380. MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
  1381. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1382. MODULE_LICENSE("GPL");