r820t.c 54 KB

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