r820t.c 39 KB

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