r820t.c 55 KB

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