r820t.c 39 KB

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