r820t.c 36 KB

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