r820t.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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. tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
  394. __func__, reg, len, len, p);
  395. /* Copy data to the output buffer */
  396. for (i = 0; i < len; i++)
  397. val[i] = bitrev8(p[i]);
  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_khz = 4070;
  811. filt_cal_lo = 60000;
  812. filt_gain = 0x10; /* +3db, 6mhz on */
  813. img_r = 0x00; /* image negative */
  814. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  815. hp_cor = 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
  816. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  817. loop_through = 0x00; /* r5[7], lt on */
  818. lt_att = 0x00; /* r31[7], lt att enable */
  819. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  820. polyfil_cur = 0x60; /* r25[6:5]:min */
  821. #if 0 /* 7 MHz type 2 - nor sure why/where this is used - Perhaps Australia? */
  822. if_khz = 4570;
  823. filt_cal_lo = 63000;
  824. filt_gain = 0x10; /* +3db, 6mhz on */
  825. img_r = 0x00; /* image negative */
  826. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  827. hp_cor = 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
  828. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  829. loop_through = 0x00; /* r5[7], lt on */
  830. lt_att = 0x00; /* r31[7], lt att enable */
  831. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  832. polyfil_cur = 0x60; /* r25[6:5]:min */
  833. #endif
  834. } else {
  835. if_khz = 4570;
  836. filt_cal_lo = 68500;
  837. filt_gain = 0x10; /* +3db, 6mhz on */
  838. img_r = 0x00; /* image negative */
  839. filt_q = 0x10; /* r10[4]:low q(1'b1) */
  840. hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
  841. ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
  842. loop_through = 0x00; /* r5[7], lt on */
  843. lt_att = 0x00; /* r31[7], lt att enable */
  844. flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
  845. polyfil_cur = 0x60; /* r25[6:5]:min */
  846. }
  847. }
  848. /* Initialize the shadow registers */
  849. memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
  850. /* Init Flag & Xtal_check Result */
  851. if (priv->imr_done)
  852. val = 1 | priv->xtal_cap_sel << 1;
  853. else
  854. val = 0;
  855. rc = r820t_write_reg_mask(priv, 0x0c, val, 0x0f);
  856. if (rc < 0)
  857. return rc;
  858. /* version */
  859. rc = r820t_write_reg_mask(priv, 0x13, VER_NUM, 0x3f);
  860. if (rc < 0)
  861. return rc;
  862. /* for LT Gain test */
  863. if (type != V4L2_TUNER_ANALOG_TV) {
  864. rc = r820t_write_reg_mask(priv, 0x1d, 0x00, 0x38);
  865. if (rc < 0)
  866. return rc;
  867. msleep(1);
  868. }
  869. priv->int_freq = if_khz * 1000;
  870. /* Check if standard changed. If so, filter calibration is needed */
  871. if (type != priv->type)
  872. need_calibration = true;
  873. else if ((type == V4L2_TUNER_ANALOG_TV) && (std != priv->std))
  874. need_calibration = true;
  875. else if ((type == V4L2_TUNER_DIGITAL_TV) &&
  876. ((delsys != priv->delsys) || bw != priv->bw))
  877. need_calibration = true;
  878. else
  879. need_calibration = false;
  880. if (need_calibration) {
  881. tuner_dbg("calibrating the tuner\n");
  882. for (i = 0; i < 2; i++) {
  883. /* Set filt_cap */
  884. rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x60);
  885. if (rc < 0)
  886. return rc;
  887. /* set cali clk =on */
  888. rc = r820t_write_reg_mask(priv, 0x0f, 0x04, 0x04);
  889. if (rc < 0)
  890. return rc;
  891. /* X'tal cap 0pF for PLL */
  892. rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x03);
  893. if (rc < 0)
  894. return rc;
  895. rc = r820t_set_pll(priv, filt_cal_lo);
  896. if (rc < 0 || !priv->has_lock)
  897. return rc;
  898. /* Start Trigger */
  899. rc = r820t_write_reg_mask(priv, 0x0b, 0x10, 0x10);
  900. if (rc < 0)
  901. return rc;
  902. msleep(1);
  903. /* Stop Trigger */
  904. rc = r820t_write_reg_mask(priv, 0x0b, 0x00, 0x10);
  905. if (rc < 0)
  906. return rc;
  907. /* set cali clk =off */
  908. rc = r820t_write_reg_mask(priv, 0x0f, 0x00, 0x04);
  909. if (rc < 0)
  910. return rc;
  911. /* Check if calibration worked */
  912. rc = r820_read(priv, 0x00, data, sizeof(data));
  913. if (rc < 0)
  914. return rc;
  915. priv->fil_cal_code = data[4] & 0x0f;
  916. if (priv->fil_cal_code && priv->fil_cal_code != 0x0f)
  917. break;
  918. }
  919. /* narrowest */
  920. if (priv->fil_cal_code == 0x0f)
  921. priv->fil_cal_code = 0;
  922. }
  923. rc = r820t_write_reg_mask(priv, 0x0a,
  924. filt_q | priv->fil_cal_code, 0x1f);
  925. if (rc < 0)
  926. return rc;
  927. /* Set BW, Filter_gain, & HP corner */
  928. rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x10);
  929. if (rc < 0)
  930. return rc;
  931. /* Set Img_R */
  932. rc = r820t_write_reg_mask(priv, 0x07, img_r, 0x80);
  933. if (rc < 0)
  934. return rc;
  935. /* Set filt_3dB, V6MHz */
  936. rc = r820t_write_reg_mask(priv, 0x06, filt_gain, 0x30);
  937. if (rc < 0)
  938. return rc;
  939. /* channel filter extension */
  940. rc = r820t_write_reg_mask(priv, 0x1e, ext_enable, 0x60);
  941. if (rc < 0)
  942. return rc;
  943. /* Loop through */
  944. rc = r820t_write_reg_mask(priv, 0x05, loop_through, 0x80);
  945. if (rc < 0)
  946. return rc;
  947. /* Loop through attenuation */
  948. rc = r820t_write_reg_mask(priv, 0x1f, lt_att, 0x80);
  949. if (rc < 0)
  950. return rc;
  951. /* filter extension widest */
  952. rc = r820t_write_reg_mask(priv, 0x0f, flt_ext_widest, 0x80);
  953. if (rc < 0)
  954. return rc;
  955. /* RF poly filter current */
  956. rc = r820t_write_reg_mask(priv, 0x19, polyfil_cur, 0x60);
  957. if (rc < 0)
  958. return rc;
  959. /* Store current standard. If it changes, re-calibrate the tuner */
  960. priv->delsys = delsys;
  961. priv->type = type;
  962. priv->std = std;
  963. priv->bw = bw;
  964. return 0;
  965. }
  966. static int r820t_read_gain(struct r820t_priv *priv)
  967. {
  968. u8 data[4];
  969. int rc;
  970. rc = r820_read(priv, 0x00, data, sizeof(data));
  971. if (rc < 0)
  972. return rc;
  973. return ((data[3] & 0x0f) << 1) + ((data[3] & 0xf0) >> 4);
  974. }
  975. static int r820t_set_gain_mode(struct r820t_priv *priv,
  976. bool set_manual_gain,
  977. int gain)
  978. {
  979. int rc;
  980. if (set_manual_gain) {
  981. int i, total_gain = 0;
  982. uint8_t mix_index = 0, lna_index = 0;
  983. u8 data[4];
  984. /* LNA auto off */
  985. rc = r820t_write_reg_mask(priv, 0x05, 0x10, 0x10);
  986. if (rc < 0)
  987. return rc;
  988. /* Mixer auto off */
  989. rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
  990. if (rc < 0)
  991. return rc;
  992. rc = r820_read(priv, 0x00, data, sizeof(data));
  993. if (rc < 0)
  994. return rc;
  995. /* set fixed VGA gain for now (16.3 dB) */
  996. rc = r820t_write_reg_mask(priv, 0x0c, 0x08, 0x9f);
  997. if (rc < 0)
  998. return rc;
  999. for (i = 0; i < 15; i++) {
  1000. if (total_gain >= gain)
  1001. break;
  1002. total_gain += r820t_lna_gain_steps[++lna_index];
  1003. if (total_gain >= gain)
  1004. break;
  1005. total_gain += r820t_mixer_gain_steps[++mix_index];
  1006. }
  1007. /* set LNA gain */
  1008. rc = r820t_write_reg_mask(priv, 0x05, lna_index, 0x0f);
  1009. if (rc < 0)
  1010. return rc;
  1011. /* set Mixer gain */
  1012. rc = r820t_write_reg_mask(priv, 0x07, mix_index, 0x0f);
  1013. if (rc < 0)
  1014. return rc;
  1015. } else {
  1016. /* LNA */
  1017. rc = r820t_write_reg_mask(priv, 0x05, 0, 0xef);
  1018. if (rc < 0)
  1019. return rc;
  1020. /* Mixer */
  1021. rc = r820t_write_reg_mask(priv, 0x07, 0x10, 0xef);
  1022. if (rc < 0)
  1023. return rc;
  1024. /* set fixed VGA gain for now (26.5 dB) */
  1025. rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
  1026. if (rc < 0)
  1027. return rc;
  1028. }
  1029. return 0;
  1030. }
  1031. static int generic_set_freq(struct dvb_frontend *fe,
  1032. u32 freq /* in HZ */,
  1033. unsigned bw,
  1034. enum v4l2_tuner_type type,
  1035. v4l2_std_id std, u32 delsys)
  1036. {
  1037. struct r820t_priv *priv = fe->tuner_priv;
  1038. int rc = -EINVAL;
  1039. u32 lo_freq;
  1040. tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
  1041. freq / 1000, bw);
  1042. rc = r820t_set_tv_standard(priv, bw, type, std, delsys);
  1043. if (rc < 0)
  1044. goto err;
  1045. if ((type == V4L2_TUNER_ANALOG_TV) && (std == V4L2_STD_SECAM_LC))
  1046. lo_freq = freq - priv->int_freq;
  1047. else
  1048. lo_freq = freq + priv->int_freq;
  1049. rc = r820t_set_mux(priv, lo_freq);
  1050. if (rc < 0)
  1051. goto err;
  1052. rc = r820t_set_gain_mode(priv, true, 0);
  1053. if (rc < 0)
  1054. goto err;
  1055. rc = r820t_set_pll(priv, lo_freq);
  1056. if (rc < 0 || !priv->has_lock)
  1057. goto err;
  1058. rc = r820t_sysfreq_sel(priv, freq, type, std, delsys);
  1059. if (rc < 0)
  1060. goto err;
  1061. tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
  1062. __func__, freq, r820t_read_gain(priv));
  1063. err:
  1064. if (rc < 0)
  1065. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1066. return rc;
  1067. }
  1068. /*
  1069. * r820t standby logic
  1070. */
  1071. static int r820t_standby(struct r820t_priv *priv)
  1072. {
  1073. int rc;
  1074. rc = r820t_write_reg(priv, 0x06, 0xb1);
  1075. if (rc < 0)
  1076. return rc;
  1077. rc = r820t_write_reg(priv, 0x05, 0x03);
  1078. if (rc < 0)
  1079. return rc;
  1080. rc = r820t_write_reg(priv, 0x07, 0x3a);
  1081. if (rc < 0)
  1082. return rc;
  1083. rc = r820t_write_reg(priv, 0x08, 0x40);
  1084. if (rc < 0)
  1085. return rc;
  1086. rc = r820t_write_reg(priv, 0x09, 0xc0);
  1087. if (rc < 0)
  1088. return rc;
  1089. rc = r820t_write_reg(priv, 0x0a, 0x36);
  1090. if (rc < 0)
  1091. return rc;
  1092. rc = r820t_write_reg(priv, 0x0c, 0x35);
  1093. if (rc < 0)
  1094. return rc;
  1095. rc = r820t_write_reg(priv, 0x0f, 0x68);
  1096. if (rc < 0)
  1097. return rc;
  1098. rc = r820t_write_reg(priv, 0x11, 0x03);
  1099. if (rc < 0)
  1100. return rc;
  1101. rc = r820t_write_reg(priv, 0x17, 0xf4);
  1102. if (rc < 0)
  1103. return rc;
  1104. rc = r820t_write_reg(priv, 0x19, 0x0c);
  1105. /* Force initial calibration */
  1106. priv->type = -1;
  1107. return rc;
  1108. }
  1109. /*
  1110. * r820t device init logic
  1111. */
  1112. static int r820t_xtal_check(struct r820t_priv *priv)
  1113. {
  1114. int rc, i;
  1115. u8 data[3], val;
  1116. /* Initialize the shadow registers */
  1117. memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
  1118. /* cap 30pF & Drive Low */
  1119. rc = r820t_write_reg_mask(priv, 0x10, 0x0b, 0x0b);
  1120. if (rc < 0)
  1121. return rc;
  1122. /* set pll autotune = 128kHz */
  1123. rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
  1124. if (rc < 0)
  1125. return rc;
  1126. /* set manual initial reg = 111111; */
  1127. rc = r820t_write_reg_mask(priv, 0x13, 0x7f, 0x7f);
  1128. if (rc < 0)
  1129. return rc;
  1130. /* set auto */
  1131. rc = r820t_write_reg_mask(priv, 0x13, 0x00, 0x40);
  1132. if (rc < 0)
  1133. return rc;
  1134. /* Try several xtal capacitor alternatives */
  1135. for (i = 0; i < ARRAY_SIZE(r820t_xtal_capacitor); i++) {
  1136. rc = r820t_write_reg_mask(priv, 0x10,
  1137. r820t_xtal_capacitor[i][0], 0x1b);
  1138. if (rc < 0)
  1139. return rc;
  1140. msleep(5);
  1141. rc = r820_read(priv, 0x00, data, sizeof(data));
  1142. if (rc < 0)
  1143. return rc;
  1144. if ((!data[2]) & 0x40)
  1145. continue;
  1146. val = data[2] & 0x3f;
  1147. if (priv->cfg->xtal == 16000000 && (val > 29 || val < 23))
  1148. break;
  1149. if (val != 0x3f)
  1150. break;
  1151. }
  1152. if (i == ARRAY_SIZE(r820t_xtal_capacitor))
  1153. return -EINVAL;
  1154. return r820t_xtal_capacitor[i][1];
  1155. }
  1156. /*
  1157. * r820t frontend operations and tuner attach code
  1158. *
  1159. * All driver locks and i2c control are only in this part of the code
  1160. */
  1161. static int r820t_init(struct dvb_frontend *fe)
  1162. {
  1163. struct r820t_priv *priv = fe->tuner_priv;
  1164. int rc, i;
  1165. int xtal_cap = 0;
  1166. tuner_dbg("%s:\n", __func__);
  1167. mutex_lock(&priv->lock);
  1168. if (fe->ops.i2c_gate_ctrl)
  1169. fe->ops.i2c_gate_ctrl(fe, 1);
  1170. if ((priv->cfg->rafael_chip == CHIP_R820T) ||
  1171. (priv->cfg->rafael_chip == CHIP_R828S) ||
  1172. (priv->cfg->rafael_chip == CHIP_R820C)) {
  1173. priv->xtal_cap_sel = XTAL_HIGH_CAP_0P;
  1174. } else {
  1175. for (i = 0; i < 3; i++) {
  1176. rc = r820t_xtal_check(priv);
  1177. if (rc < 0)
  1178. goto err;
  1179. if (!i || rc > xtal_cap)
  1180. xtal_cap = rc;
  1181. }
  1182. priv->xtal_cap_sel = xtal_cap;
  1183. }
  1184. /* Initialize registers */
  1185. rc = r820t_write(priv, 0x05,
  1186. r820t_init_array, sizeof(r820t_init_array));
  1187. err:
  1188. if (fe->ops.i2c_gate_ctrl)
  1189. fe->ops.i2c_gate_ctrl(fe, 0);
  1190. mutex_unlock(&priv->lock);
  1191. if (rc < 0)
  1192. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1193. return rc;
  1194. }
  1195. static int r820t_sleep(struct dvb_frontend *fe)
  1196. {
  1197. struct r820t_priv *priv = fe->tuner_priv;
  1198. int rc;
  1199. tuner_dbg("%s:\n", __func__);
  1200. mutex_lock(&priv->lock);
  1201. if (fe->ops.i2c_gate_ctrl)
  1202. fe->ops.i2c_gate_ctrl(fe, 1);
  1203. rc = r820t_standby(priv);
  1204. if (fe->ops.i2c_gate_ctrl)
  1205. fe->ops.i2c_gate_ctrl(fe, 0);
  1206. mutex_unlock(&priv->lock);
  1207. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1208. return rc;
  1209. }
  1210. static int r820t_set_analog_freq(struct dvb_frontend *fe,
  1211. struct analog_parameters *p)
  1212. {
  1213. struct r820t_priv *priv = fe->tuner_priv;
  1214. unsigned bw;
  1215. int rc;
  1216. tuner_dbg("%s called\n", __func__);
  1217. /* if std is not defined, choose one */
  1218. if (!p->std)
  1219. p->std = V4L2_STD_MN;
  1220. if ((p->std == V4L2_STD_PAL_M) || (p->std == V4L2_STD_NTSC))
  1221. bw = 6;
  1222. else
  1223. bw = 8;
  1224. mutex_lock(&priv->lock);
  1225. if (fe->ops.i2c_gate_ctrl)
  1226. fe->ops.i2c_gate_ctrl(fe, 1);
  1227. rc = generic_set_freq(fe, 62500l * p->frequency, bw,
  1228. V4L2_TUNER_ANALOG_TV, p->std, SYS_UNDEFINED);
  1229. if (fe->ops.i2c_gate_ctrl)
  1230. fe->ops.i2c_gate_ctrl(fe, 0);
  1231. mutex_unlock(&priv->lock);
  1232. return rc;
  1233. }
  1234. static int r820t_set_params(struct dvb_frontend *fe)
  1235. {
  1236. struct r820t_priv *priv = fe->tuner_priv;
  1237. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1238. int rc;
  1239. unsigned bw;
  1240. tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  1241. __func__, c->delivery_system, c->frequency, c->bandwidth_hz);
  1242. mutex_lock(&priv->lock);
  1243. if (fe->ops.i2c_gate_ctrl)
  1244. fe->ops.i2c_gate_ctrl(fe, 1);
  1245. bw = (c->bandwidth_hz + 500000) / 1000000;
  1246. if (!bw)
  1247. bw = 8;
  1248. rc = generic_set_freq(fe, c->frequency, bw,
  1249. V4L2_TUNER_DIGITAL_TV, 0, c->delivery_system);
  1250. if (fe->ops.i2c_gate_ctrl)
  1251. fe->ops.i2c_gate_ctrl(fe, 0);
  1252. mutex_unlock(&priv->lock);
  1253. if (rc)
  1254. tuner_dbg("%s: failed=%d\n", __func__, rc);
  1255. return rc;
  1256. }
  1257. static int r820t_signal(struct dvb_frontend *fe, u16 *strength)
  1258. {
  1259. struct r820t_priv *priv = fe->tuner_priv;
  1260. int rc = 0;
  1261. mutex_lock(&priv->lock);
  1262. if (fe->ops.i2c_gate_ctrl)
  1263. fe->ops.i2c_gate_ctrl(fe, 1);
  1264. if (priv->has_lock) {
  1265. rc = r820t_read_gain(priv);
  1266. if (rc < 0)
  1267. goto err;
  1268. /* A higher gain at LNA means a lower signal strength */
  1269. *strength = (45 - rc) << 4 | 0xff;
  1270. } else {
  1271. *strength = 0;
  1272. }
  1273. err:
  1274. if (fe->ops.i2c_gate_ctrl)
  1275. fe->ops.i2c_gate_ctrl(fe, 0);
  1276. mutex_unlock(&priv->lock);
  1277. tuner_dbg("%s: %s, gain=%d strength=%d\n",
  1278. __func__,
  1279. priv->has_lock ? "PLL locked" : "no signal",
  1280. rc, *strength);
  1281. return 0;
  1282. }
  1283. static int r820t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  1284. {
  1285. struct r820t_priv *priv = fe->tuner_priv;
  1286. tuner_dbg("%s:\n", __func__);
  1287. *frequency = priv->int_freq;
  1288. return 0;
  1289. }
  1290. static int r820t_release(struct dvb_frontend *fe)
  1291. {
  1292. struct r820t_priv *priv = fe->tuner_priv;
  1293. tuner_dbg("%s:\n", __func__);
  1294. mutex_lock(&r820t_list_mutex);
  1295. if (priv)
  1296. hybrid_tuner_release_state(priv);
  1297. mutex_unlock(&r820t_list_mutex);
  1298. fe->tuner_priv = NULL;
  1299. kfree(fe->tuner_priv);
  1300. return 0;
  1301. }
  1302. static const struct dvb_tuner_ops r820t_tuner_ops = {
  1303. .info = {
  1304. .name = "Rafael Micro R820T",
  1305. .frequency_min = 42000000,
  1306. .frequency_max = 1002000000,
  1307. },
  1308. .init = r820t_init,
  1309. .release = r820t_release,
  1310. .sleep = r820t_sleep,
  1311. .set_params = r820t_set_params,
  1312. .set_analog_params = r820t_set_analog_freq,
  1313. .get_if_frequency = r820t_get_if_frequency,
  1314. .get_rf_strength = r820t_signal,
  1315. };
  1316. struct dvb_frontend *r820t_attach(struct dvb_frontend *fe,
  1317. struct i2c_adapter *i2c,
  1318. const struct r820t_config *cfg)
  1319. {
  1320. struct r820t_priv *priv;
  1321. int rc = -ENODEV;
  1322. u8 data[5];
  1323. int instance;
  1324. mutex_lock(&r820t_list_mutex);
  1325. instance = hybrid_tuner_request_state(struct r820t_priv, priv,
  1326. hybrid_tuner_instance_list,
  1327. i2c, cfg->i2c_addr,
  1328. "r820t");
  1329. switch (instance) {
  1330. case 0:
  1331. /* memory allocation failure */
  1332. goto err_no_gate;
  1333. break;
  1334. case 1:
  1335. /* new tuner instance */
  1336. priv->cfg = cfg;
  1337. mutex_init(&priv->lock);
  1338. fe->tuner_priv = priv;
  1339. break;
  1340. case 2:
  1341. /* existing tuner instance */
  1342. fe->tuner_priv = priv;
  1343. break;
  1344. }
  1345. memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops, sizeof(r820t_tuner_ops));
  1346. if (fe->ops.i2c_gate_ctrl)
  1347. fe->ops.i2c_gate_ctrl(fe, 1);
  1348. /* check if the tuner is there */
  1349. rc = r820_read(priv, 0x00, data, sizeof(data));
  1350. if (rc < 0)
  1351. goto err;
  1352. rc = r820t_sleep(fe);
  1353. if (rc < 0)
  1354. goto err;
  1355. tuner_info("Rafael Micro r820t successfully identified\n");
  1356. fe->tuner_priv = priv;
  1357. memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops,
  1358. sizeof(struct dvb_tuner_ops));
  1359. if (fe->ops.i2c_gate_ctrl)
  1360. fe->ops.i2c_gate_ctrl(fe, 0);
  1361. mutex_unlock(&r820t_list_mutex);
  1362. return fe;
  1363. err:
  1364. if (fe->ops.i2c_gate_ctrl)
  1365. fe->ops.i2c_gate_ctrl(fe, 0);
  1366. err_no_gate:
  1367. mutex_unlock(&r820t_list_mutex);
  1368. tuner_info("%s: failed=%d\n", __func__, rc);
  1369. r820t_release(fe);
  1370. return NULL;
  1371. }
  1372. EXPORT_SYMBOL_GPL(r820t_attach);
  1373. MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
  1374. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1375. MODULE_LICENSE("GPL");