r820t.c 56 KB

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