tda18271-fe.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
  3. Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/videodev2.h>
  18. #include "tda18271-priv.h"
  19. int tda18271_debug;
  20. module_param_named(debug, tda18271_debug, int, 0644);
  21. MODULE_PARM_DESC(debug, "set debug level "
  22. "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
  23. static LIST_HEAD(tda18271_list);
  24. static DEFINE_MUTEX(tda18271_list_mutex);
  25. /*---------------------------------------------------------------------*/
  26. static int tda18271_ir_cal_init(struct dvb_frontend *fe)
  27. {
  28. struct tda18271_priv *priv = fe->tuner_priv;
  29. unsigned char *regs = priv->tda18271_regs;
  30. tda18271_read_regs(fe);
  31. /* test IR_CAL_OK to see if we need init */
  32. if ((regs[R_EP1] & 0x08) == 0)
  33. tda18271_init_regs(fe);
  34. return 0;
  35. }
  36. /* ------------------------------------------------------------------ */
  37. static int tda18271_channel_configuration(struct dvb_frontend *fe,
  38. u32 ifc, u32 freq, u32 bw, u8 std,
  39. int radio)
  40. {
  41. struct tda18271_priv *priv = fe->tuner_priv;
  42. unsigned char *regs = priv->tda18271_regs;
  43. u32 N;
  44. /* update TV broadcast parameters */
  45. /* set standard */
  46. regs[R_EP3] &= ~0x1f; /* clear std bits */
  47. regs[R_EP3] |= std;
  48. /* set cal mode to normal */
  49. regs[R_EP4] &= ~0x03;
  50. /* update IF output level & IF notch frequency */
  51. regs[R_EP4] &= ~0x1c; /* clear if level bits */
  52. switch (priv->mode) {
  53. case TDA18271_ANALOG:
  54. regs[R_MPD] &= ~0x80; /* IF notch = 0 */
  55. break;
  56. case TDA18271_DIGITAL:
  57. regs[R_EP4] |= 0x04; /* IF level = 1 */
  58. regs[R_MPD] |= 0x80; /* IF notch = 1 */
  59. break;
  60. }
  61. if (radio)
  62. regs[R_EP4] |= 0x80;
  63. else
  64. regs[R_EP4] &= ~0x80;
  65. /* update RF_TOP / IF_TOP */
  66. switch (priv->mode) {
  67. case TDA18271_ANALOG:
  68. regs[R_EB22] = 0x2c;
  69. break;
  70. case TDA18271_DIGITAL:
  71. regs[R_EB22] = 0x37;
  72. break;
  73. }
  74. tda18271_write_regs(fe, R_EB22, 1);
  75. /* --------------------------------------------------------------- */
  76. /* disable Power Level Indicator */
  77. regs[R_EP1] |= 0x40;
  78. /* frequency dependent parameters */
  79. tda18271_calc_ir_measure(fe, &freq);
  80. tda18271_calc_bp_filter(fe, &freq);
  81. tda18271_calc_rf_band(fe, &freq);
  82. tda18271_calc_gain_taper(fe, &freq);
  83. /* --------------------------------------------------------------- */
  84. /* dual tuner and agc1 extra configuration */
  85. /* main vco when Master, cal vco when slave */
  86. regs[R_EB1] |= 0x04; /* FIXME: assumes master */
  87. /* agc1 always active */
  88. regs[R_EB1] &= ~0x02;
  89. /* agc1 has priority on agc2 */
  90. regs[R_EB1] &= ~0x01;
  91. tda18271_write_regs(fe, R_EB1, 1);
  92. /* --------------------------------------------------------------- */
  93. N = freq + ifc;
  94. /* FIXME: assumes master */
  95. tda18271_calc_main_pll(fe, N);
  96. tda18271_write_regs(fe, R_MPD, 4);
  97. tda18271_write_regs(fe, R_TM, 7);
  98. /* main pll charge pump source */
  99. regs[R_EB4] |= 0x20;
  100. tda18271_write_regs(fe, R_EB4, 1);
  101. msleep(1);
  102. /* normal operation for the main pll */
  103. regs[R_EB4] &= ~0x20;
  104. tda18271_write_regs(fe, R_EB4, 1);
  105. msleep(5);
  106. return 0;
  107. }
  108. static int tda18271_read_thermometer(struct dvb_frontend *fe)
  109. {
  110. struct tda18271_priv *priv = fe->tuner_priv;
  111. unsigned char *regs = priv->tda18271_regs;
  112. int tm;
  113. /* switch thermometer on */
  114. regs[R_TM] |= 0x10;
  115. tda18271_write_regs(fe, R_TM, 1);
  116. /* read thermometer info */
  117. tda18271_read_regs(fe);
  118. if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
  119. (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
  120. if ((regs[R_TM] & 0x20) == 0x20)
  121. regs[R_TM] &= ~0x20;
  122. else
  123. regs[R_TM] |= 0x20;
  124. tda18271_write_regs(fe, R_TM, 1);
  125. msleep(10); /* temperature sensing */
  126. /* read thermometer info */
  127. tda18271_read_regs(fe);
  128. }
  129. tm = tda18271_lookup_thermometer(fe);
  130. /* switch thermometer off */
  131. regs[R_TM] &= ~0x10;
  132. tda18271_write_regs(fe, R_TM, 1);
  133. /* set CAL mode to normal */
  134. regs[R_EP4] &= ~0x03;
  135. tda18271_write_regs(fe, R_EP4, 1);
  136. return tm;
  137. }
  138. static int tda18271_rf_tracking_filters_correction(struct dvb_frontend *fe,
  139. u32 freq)
  140. {
  141. struct tda18271_priv *priv = fe->tuner_priv;
  142. struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
  143. unsigned char *regs = priv->tda18271_regs;
  144. int tm_current, rfcal_comp, approx, i;
  145. u8 dc_over_dt, rf_tab;
  146. /* power up */
  147. regs[R_EP3] &= ~0xe0; /* sm = 0, sm_lt = 0, sm_xt = 0 */
  148. tda18271_write_regs(fe, R_EP3, 1);
  149. /* read die current temperature */
  150. tm_current = tda18271_read_thermometer(fe);
  151. /* frequency dependent parameters */
  152. tda18271_calc_rf_cal(fe, &freq);
  153. rf_tab = regs[R_EB14];
  154. i = tda18271_lookup_rf_band(fe, &freq, NULL);
  155. if (i < 0)
  156. return -EINVAL;
  157. if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
  158. approx = map[i].rf_a1 *
  159. (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
  160. } else {
  161. approx = map[i].rf_a2 *
  162. (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
  163. }
  164. if (approx < 0)
  165. approx = 0;
  166. if (approx > 255)
  167. approx = 255;
  168. tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
  169. /* calculate temperature compensation */
  170. rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
  171. regs[R_EB14] = approx + rfcal_comp;
  172. tda18271_write_regs(fe, R_EB14, 1);
  173. return 0;
  174. }
  175. static int tda18271_por(struct dvb_frontend *fe)
  176. {
  177. struct tda18271_priv *priv = fe->tuner_priv;
  178. unsigned char *regs = priv->tda18271_regs;
  179. /* power up detector 1 */
  180. regs[R_EB12] &= ~0x20;
  181. tda18271_write_regs(fe, R_EB12, 1);
  182. regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
  183. regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
  184. tda18271_write_regs(fe, R_EB18, 1);
  185. regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
  186. /* POR mode */
  187. regs[R_EP3] &= ~0xe0; /* clear sm, sm_lt, sm_xt */
  188. regs[R_EP3] |= 0x80; /* sm = 1, sm_lt = 0, sm_xt = 0 */
  189. tda18271_write_regs(fe, R_EP3, 1);
  190. /* disable 1.5 MHz low pass filter */
  191. regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
  192. regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
  193. tda18271_write_regs(fe, R_EB21, 3);
  194. return 0;
  195. }
  196. static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
  197. {
  198. struct tda18271_priv *priv = fe->tuner_priv;
  199. unsigned char *regs = priv->tda18271_regs;
  200. u32 N;
  201. /* set CAL mode to normal */
  202. regs[R_EP4] &= ~0x03;
  203. tda18271_write_regs(fe, R_EP4, 1);
  204. /* switch off agc1 */
  205. regs[R_EP3] |= 0x40; /* sm_lt = 1 */
  206. regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
  207. tda18271_write_regs(fe, R_EB18, 1);
  208. /* frequency dependent parameters */
  209. tda18271_calc_bp_filter(fe, &freq);
  210. tda18271_calc_gain_taper(fe, &freq);
  211. tda18271_calc_rf_band(fe, &freq);
  212. tda18271_calc_km(fe, &freq);
  213. tda18271_write_regs(fe, R_EP1, 3);
  214. tda18271_write_regs(fe, R_EB13, 1);
  215. /* main pll charge pump source */
  216. regs[R_EB4] |= 0x20;
  217. tda18271_write_regs(fe, R_EB4, 1);
  218. /* cal pll charge pump source */
  219. regs[R_EB7] |= 0x20;
  220. tda18271_write_regs(fe, R_EB7, 1);
  221. /* force dcdc converter to 0 V */
  222. regs[R_EB14] = 0x00;
  223. tda18271_write_regs(fe, R_EB14, 1);
  224. /* disable plls lock */
  225. regs[R_EB20] &= ~0x20;
  226. tda18271_write_regs(fe, R_EB20, 1);
  227. /* set CAL mode to RF tracking filter calibration */
  228. regs[R_EP4] |= 0x03;
  229. tda18271_write_regs(fe, R_EP4, 2);
  230. /* --------------------------------------------------------------- */
  231. /* set the internal calibration signal */
  232. N = freq;
  233. tda18271_calc_main_pll(fe, N);
  234. tda18271_write_regs(fe, R_MPD, 4);
  235. /* downconvert internal calibration */
  236. N += 1000000;
  237. tda18271_calc_main_pll(fe, N);
  238. tda18271_write_regs(fe, R_MPD, 4);
  239. msleep(5);
  240. tda18271_write_regs(fe, R_EP2, 1);
  241. tda18271_write_regs(fe, R_EP1, 1);
  242. tda18271_write_regs(fe, R_EP2, 1);
  243. tda18271_write_regs(fe, R_EP1, 1);
  244. /* --------------------------------------------------------------- */
  245. /* normal operation for the main pll */
  246. regs[R_EB4] &= ~0x20;
  247. tda18271_write_regs(fe, R_EB4, 1);
  248. /* normal operation for the cal pll */
  249. regs[R_EB7] &= ~0x20;
  250. tda18271_write_regs(fe, R_EB7, 1);
  251. msleep(5); /* plls locking */
  252. /* launch the rf tracking filters calibration */
  253. regs[R_EB20] |= 0x20;
  254. tda18271_write_regs(fe, R_EB20, 1);
  255. msleep(60); /* calibration */
  256. /* --------------------------------------------------------------- */
  257. /* set CAL mode to normal */
  258. regs[R_EP4] &= ~0x03;
  259. /* switch on agc1 */
  260. regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
  261. regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
  262. tda18271_write_regs(fe, R_EB18, 1);
  263. tda18271_write_regs(fe, R_EP3, 2);
  264. /* synchronization */
  265. tda18271_write_regs(fe, R_EP1, 1);
  266. /* get calibration result */
  267. tda18271_read_extended(fe);
  268. return regs[R_EB14];
  269. }
  270. static int tda18271_powerscan(struct dvb_frontend *fe,
  271. u32 *freq_in, u32 *freq_out)
  272. {
  273. struct tda18271_priv *priv = fe->tuner_priv;
  274. unsigned char *regs = priv->tda18271_regs;
  275. int sgn, bcal, count, wait;
  276. u8 cid_target;
  277. u16 count_limit;
  278. u32 freq;
  279. freq = *freq_in;
  280. tda18271_calc_rf_band(fe, &freq);
  281. tda18271_calc_rf_cal(fe, &freq);
  282. tda18271_calc_gain_taper(fe, &freq);
  283. tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
  284. tda18271_write_regs(fe, R_EP2, 1);
  285. tda18271_write_regs(fe, R_EB14, 1);
  286. /* downconvert frequency */
  287. freq += 1000000;
  288. tda18271_calc_main_pll(fe, freq);
  289. tda18271_write_regs(fe, R_MPD, 4);
  290. msleep(5); /* pll locking */
  291. /* detection mode */
  292. regs[R_EP4] &= ~0x03;
  293. regs[R_EP4] |= 0x01;
  294. tda18271_write_regs(fe, R_EP4, 1);
  295. /* launch power detection measurement */
  296. tda18271_write_regs(fe, R_EP2, 1);
  297. /* read power detection info, stored in EB10 */
  298. tda18271_read_extended(fe);
  299. /* algorithm initialization */
  300. sgn = 1;
  301. *freq_out = *freq_in;
  302. bcal = 0;
  303. count = 0;
  304. wait = false;
  305. while ((regs[R_EB10] & 0x3f) < cid_target) {
  306. /* downconvert updated freq to 1 MHz */
  307. freq = *freq_in + (sgn * count) + 1000000;
  308. tda18271_calc_main_pll(fe, freq);
  309. tda18271_write_regs(fe, R_MPD, 4);
  310. if (wait) {
  311. msleep(5); /* pll locking */
  312. wait = false;
  313. } else
  314. udelay(100); /* pll locking */
  315. /* launch power detection measurement */
  316. tda18271_write_regs(fe, R_EP2, 1);
  317. /* read power detection info, stored in EB10 */
  318. tda18271_read_extended(fe);
  319. count += 200;
  320. if (count < count_limit)
  321. continue;
  322. if (sgn <= 0)
  323. break;
  324. sgn = -1 * sgn;
  325. count = 200;
  326. wait = true;
  327. }
  328. if ((regs[R_EB10] & 0x3f) >= cid_target) {
  329. bcal = 1;
  330. *freq_out = freq - 1000000;
  331. } else
  332. bcal = 0;
  333. tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
  334. bcal, *freq_in, *freq_out, freq);
  335. return bcal;
  336. }
  337. static int tda18271_powerscan_init(struct dvb_frontend *fe)
  338. {
  339. struct tda18271_priv *priv = fe->tuner_priv;
  340. unsigned char *regs = priv->tda18271_regs;
  341. /* set standard to digital */
  342. regs[R_EP3] &= ~0x1f; /* clear std bits */
  343. regs[R_EP3] |= 0x12;
  344. /* set cal mode to normal */
  345. regs[R_EP4] &= ~0x03;
  346. /* update IF output level & IF notch frequency */
  347. regs[R_EP4] &= ~0x1c; /* clear if level bits */
  348. tda18271_write_regs(fe, R_EP3, 2);
  349. regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
  350. tda18271_write_regs(fe, R_EB18, 1);
  351. regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
  352. /* 1.5 MHz low pass filter */
  353. regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
  354. regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
  355. tda18271_write_regs(fe, R_EB21, 3);
  356. return 0;
  357. }
  358. static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
  359. {
  360. struct tda18271_priv *priv = fe->tuner_priv;
  361. struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
  362. unsigned char *regs = priv->tda18271_regs;
  363. int bcal, rf, i;
  364. #define RF1 0
  365. #define RF2 1
  366. #define RF3 2
  367. u32 rf_default[3];
  368. u32 rf_freq[3];
  369. u8 prog_cal[3];
  370. u8 prog_tab[3];
  371. i = tda18271_lookup_rf_band(fe, &freq, NULL);
  372. if (i < 0)
  373. return i;
  374. rf_default[RF1] = 1000 * map[i].rf1_def;
  375. rf_default[RF2] = 1000 * map[i].rf2_def;
  376. rf_default[RF3] = 1000 * map[i].rf3_def;
  377. for (rf = RF1; rf <= RF3; rf++) {
  378. if (0 == rf_default[rf])
  379. return 0;
  380. tda_cal("freq = %d, rf = %d\n", freq, rf);
  381. /* look for optimized calibration frequency */
  382. bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
  383. tda18271_calc_rf_cal(fe, &rf_freq[rf]);
  384. prog_tab[rf] = regs[R_EB14];
  385. if (1 == bcal)
  386. prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
  387. else
  388. prog_cal[rf] = prog_tab[rf];
  389. switch (rf) {
  390. case RF1:
  391. map[i].rf_a1 = 0;
  392. map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
  393. map[i].rf1 = rf_freq[RF1] / 1000;
  394. break;
  395. case RF2:
  396. map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
  397. prog_cal[RF1] + prog_tab[RF1]) /
  398. ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
  399. map[i].rf2 = rf_freq[RF2] / 1000;
  400. break;
  401. case RF3:
  402. map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
  403. prog_cal[RF2] + prog_tab[RF2]) /
  404. ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
  405. map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
  406. map[i].rf3 = rf_freq[RF3] / 1000;
  407. break;
  408. default:
  409. BUG();
  410. }
  411. }
  412. return 0;
  413. }
  414. static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
  415. {
  416. struct tda18271_priv *priv = fe->tuner_priv;
  417. unsigned int i;
  418. tda_info("tda18271: performing RF tracking filter calibration\n");
  419. /* wait for die temperature stabilization */
  420. msleep(200);
  421. tda18271_powerscan_init(fe);
  422. /* rf band calibration */
  423. for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
  424. tda18271_rf_tracking_filters_init(fe, 1000 *
  425. priv->rf_cal_state[i].rfmax);
  426. priv->tm_rfcal = tda18271_read_thermometer(fe);
  427. return 0;
  428. }
  429. /* ------------------------------------------------------------------ */
  430. static int tda18271_rf_cal_init(struct dvb_frontend *fe)
  431. {
  432. struct tda18271_priv *priv = fe->tuner_priv;
  433. if (priv->cal_initialized)
  434. return 0;
  435. tda18271_calc_rf_filter_curve(fe);
  436. tda18271_por(fe);
  437. tda_info("tda18271: RF tracking filter calibration complete\n");
  438. priv->cal_initialized = true;
  439. return 0;
  440. }
  441. static int tda18271_init(struct dvb_frontend *fe)
  442. {
  443. struct tda18271_priv *priv = fe->tuner_priv;
  444. mutex_lock(&priv->lock);
  445. /* initialization */
  446. tda18271_ir_cal_init(fe);
  447. if (priv->id == TDA18271HDC2)
  448. tda18271_rf_cal_init(fe);
  449. mutex_unlock(&priv->lock);
  450. return 0;
  451. }
  452. static int tda18271c2_tune(struct dvb_frontend *fe,
  453. u32 ifc, u32 freq, u32 bw, u8 std, int radio)
  454. {
  455. struct tda18271_priv *priv = fe->tuner_priv;
  456. tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
  457. tda18271_init(fe);
  458. mutex_lock(&priv->lock);
  459. tda18271_rf_tracking_filters_correction(fe, freq);
  460. tda18271_channel_configuration(fe, ifc, freq, bw, std, radio);
  461. mutex_unlock(&priv->lock);
  462. return 0;
  463. }
  464. /* ------------------------------------------------------------------ */
  465. static int tda18271c1_tune(struct dvb_frontend *fe,
  466. u32 ifc, u32 freq, u32 bw, u8 std, int radio)
  467. {
  468. struct tda18271_priv *priv = fe->tuner_priv;
  469. unsigned char *regs = priv->tda18271_regs;
  470. u32 N = 0;
  471. tda18271_init(fe);
  472. mutex_lock(&priv->lock);
  473. tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
  474. /* RF tracking filter calibration */
  475. /* calculate bp filter */
  476. tda18271_calc_bp_filter(fe, &freq);
  477. tda18271_write_regs(fe, R_EP1, 1);
  478. regs[R_EB4] &= 0x07;
  479. regs[R_EB4] |= 0x60;
  480. tda18271_write_regs(fe, R_EB4, 1);
  481. regs[R_EB7] = 0x60;
  482. tda18271_write_regs(fe, R_EB7, 1);
  483. regs[R_EB14] = 0x00;
  484. tda18271_write_regs(fe, R_EB14, 1);
  485. regs[R_EB20] = 0xcc;
  486. tda18271_write_regs(fe, R_EB20, 1);
  487. /* set cal mode to RF tracking filter calibration */
  488. regs[R_EP4] |= 0x03;
  489. /* calculate cal pll */
  490. switch (priv->mode) {
  491. case TDA18271_ANALOG:
  492. N = freq - 1250000;
  493. break;
  494. case TDA18271_DIGITAL:
  495. N = freq + bw / 2;
  496. break;
  497. }
  498. tda18271_calc_cal_pll(fe, N);
  499. /* calculate main pll */
  500. switch (priv->mode) {
  501. case TDA18271_ANALOG:
  502. N = freq - 250000;
  503. break;
  504. case TDA18271_DIGITAL:
  505. N = freq + bw / 2 + 1000000;
  506. break;
  507. }
  508. tda18271_calc_main_pll(fe, N);
  509. tda18271_write_regs(fe, R_EP3, 11);
  510. msleep(5); /* RF tracking filter calibration initialization */
  511. /* search for K,M,CO for RF calibration */
  512. tda18271_calc_km(fe, &freq);
  513. tda18271_write_regs(fe, R_EB13, 1);
  514. /* search for rf band */
  515. tda18271_calc_rf_band(fe, &freq);
  516. /* search for gain taper */
  517. tda18271_calc_gain_taper(fe, &freq);
  518. tda18271_write_regs(fe, R_EP2, 1);
  519. tda18271_write_regs(fe, R_EP1, 1);
  520. tda18271_write_regs(fe, R_EP2, 1);
  521. tda18271_write_regs(fe, R_EP1, 1);
  522. regs[R_EB4] &= 0x07;
  523. regs[R_EB4] |= 0x40;
  524. tda18271_write_regs(fe, R_EB4, 1);
  525. regs[R_EB7] = 0x40;
  526. tda18271_write_regs(fe, R_EB7, 1);
  527. msleep(10);
  528. regs[R_EB20] = 0xec;
  529. tda18271_write_regs(fe, R_EB20, 1);
  530. msleep(60); /* RF tracking filter calibration completion */
  531. regs[R_EP4] &= ~0x03; /* set cal mode to normal */
  532. tda18271_write_regs(fe, R_EP4, 1);
  533. tda18271_write_regs(fe, R_EP1, 1);
  534. /* RF tracking filter correction for VHF_Low band */
  535. if (0 == tda18271_calc_rf_cal(fe, &freq))
  536. tda18271_write_regs(fe, R_EB14, 1);
  537. /* Channel Configuration */
  538. switch (priv->mode) {
  539. case TDA18271_ANALOG:
  540. regs[R_EB22] = 0x2c;
  541. break;
  542. case TDA18271_DIGITAL:
  543. regs[R_EB22] = 0x37;
  544. break;
  545. }
  546. tda18271_write_regs(fe, R_EB22, 1);
  547. regs[R_EP1] |= 0x40; /* set dis power level on */
  548. /* set standard */
  549. regs[R_EP3] &= ~0x1f; /* clear std bits */
  550. /* see table 22 */
  551. regs[R_EP3] |= std;
  552. regs[R_EP4] &= ~0x03; /* set cal mode to normal */
  553. regs[R_EP4] &= ~0x1c; /* clear if level bits */
  554. switch (priv->mode) {
  555. case TDA18271_ANALOG:
  556. regs[R_MPD] &= ~0x80; /* IF notch = 0 */
  557. break;
  558. case TDA18271_DIGITAL:
  559. regs[R_EP4] |= 0x04;
  560. regs[R_MPD] |= 0x80;
  561. break;
  562. }
  563. if (radio)
  564. regs[R_EP4] |= 0x80;
  565. else
  566. regs[R_EP4] &= ~0x80;
  567. /* image rejection validity */
  568. tda18271_calc_ir_measure(fe, &freq);
  569. /* calculate MAIN PLL */
  570. N = freq + ifc;
  571. tda18271_calc_main_pll(fe, N);
  572. tda18271_write_regs(fe, R_TM, 15);
  573. msleep(5);
  574. mutex_unlock(&priv->lock);
  575. return 0;
  576. }
  577. static inline int tda18271_tune(struct dvb_frontend *fe,
  578. u32 ifc, u32 freq, u32 bw, u8 std, int radio)
  579. {
  580. struct tda18271_priv *priv = fe->tuner_priv;
  581. int ret = -EINVAL;
  582. switch (priv->id) {
  583. case TDA18271HDC1:
  584. ret = tda18271c1_tune(fe, ifc, freq, bw, std, radio);
  585. break;
  586. case TDA18271HDC2:
  587. ret = tda18271c2_tune(fe, ifc, freq, bw, std, radio);
  588. break;
  589. }
  590. return ret;
  591. }
  592. /* ------------------------------------------------------------------ */
  593. static int tda18271_set_params(struct dvb_frontend *fe,
  594. struct dvb_frontend_parameters *params)
  595. {
  596. struct tda18271_priv *priv = fe->tuner_priv;
  597. struct tda18271_std_map *std_map = &priv->std;
  598. int ret;
  599. u8 std;
  600. u16 sgIF;
  601. u32 bw, freq = params->frequency;
  602. priv->mode = TDA18271_DIGITAL;
  603. /* see table 22 */
  604. if (fe->ops.info.type == FE_ATSC) {
  605. switch (params->u.vsb.modulation) {
  606. case VSB_8:
  607. case VSB_16:
  608. std = std_map->atsc_6.std_bits;
  609. sgIF = std_map->atsc_6.if_freq;
  610. break;
  611. case QAM_64:
  612. case QAM_256:
  613. std = std_map->qam_6.std_bits;
  614. sgIF = std_map->qam_6.if_freq;
  615. break;
  616. default:
  617. tda_warn("modulation not set!\n");
  618. return -EINVAL;
  619. }
  620. #if 0
  621. /* userspace request is already center adjusted */
  622. freq += 1750000; /* Adjust to center (+1.75MHZ) */
  623. #endif
  624. bw = 6000000;
  625. } else if (fe->ops.info.type == FE_OFDM) {
  626. switch (params->u.ofdm.bandwidth) {
  627. case BANDWIDTH_6_MHZ:
  628. bw = 6000000;
  629. std = std_map->dvbt_6.std_bits;
  630. sgIF = std_map->dvbt_6.if_freq;
  631. break;
  632. case BANDWIDTH_7_MHZ:
  633. bw = 7000000;
  634. std = std_map->dvbt_7.std_bits;
  635. sgIF = std_map->dvbt_7.if_freq;
  636. break;
  637. case BANDWIDTH_8_MHZ:
  638. bw = 8000000;
  639. std = std_map->dvbt_8.std_bits;
  640. sgIF = std_map->dvbt_8.if_freq;
  641. break;
  642. default:
  643. tda_warn("bandwidth not set!\n");
  644. return -EINVAL;
  645. }
  646. } else {
  647. tda_warn("modulation type not supported!\n");
  648. return -EINVAL;
  649. }
  650. ret = tda18271_tune(fe, sgIF * 1000, freq, bw, std, 0);
  651. if (ret < 0)
  652. goto fail;
  653. priv->frequency = freq;
  654. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
  655. params->u.ofdm.bandwidth : 0;
  656. fail:
  657. return ret;
  658. }
  659. static int tda18271_set_analog_params(struct dvb_frontend *fe,
  660. struct analog_parameters *params)
  661. {
  662. struct tda18271_priv *priv = fe->tuner_priv;
  663. struct tda18271_std_map *std_map = &priv->std;
  664. char *mode;
  665. int ret, radio = 0;
  666. u8 std;
  667. u16 sgIF;
  668. u32 freq = params->frequency * 62500;
  669. priv->mode = TDA18271_ANALOG;
  670. if (params->mode == V4L2_TUNER_RADIO) {
  671. radio = 1;
  672. freq = freq / 1000;
  673. std = std_map->fm_radio.std_bits;
  674. sgIF = std_map->fm_radio.if_freq;
  675. mode = "fm";
  676. } else if (params->std & V4L2_STD_MN) {
  677. std = std_map->atv_mn.std_bits;
  678. sgIF = std_map->atv_mn.if_freq;
  679. mode = "MN";
  680. } else if (params->std & V4L2_STD_B) {
  681. std = std_map->atv_b.std_bits;
  682. sgIF = std_map->atv_b.if_freq;
  683. mode = "B";
  684. } else if (params->std & V4L2_STD_GH) {
  685. std = std_map->atv_gh.std_bits;
  686. sgIF = std_map->atv_gh.if_freq;
  687. mode = "GH";
  688. } else if (params->std & V4L2_STD_PAL_I) {
  689. std = std_map->atv_i.std_bits;
  690. sgIF = std_map->atv_i.if_freq;
  691. mode = "I";
  692. } else if (params->std & V4L2_STD_DK) {
  693. std = std_map->atv_dk.std_bits;
  694. sgIF = std_map->atv_dk.if_freq;
  695. mode = "DK";
  696. } else if (params->std & V4L2_STD_SECAM_L) {
  697. std = std_map->atv_l.std_bits;
  698. sgIF = std_map->atv_l.if_freq;
  699. mode = "L";
  700. } else if (params->std & V4L2_STD_SECAM_LC) {
  701. std = std_map->atv_lc.std_bits;
  702. sgIF = std_map->atv_lc.if_freq;
  703. mode = "L'";
  704. } else {
  705. std = std_map->atv_i.std_bits;
  706. sgIF = std_map->atv_i.if_freq;
  707. mode = "xx";
  708. }
  709. tda_dbg("setting tda18271 to system %s\n", mode);
  710. ret = tda18271_tune(fe, sgIF * 1000, freq, 0, std, radio);
  711. if (ret < 0)
  712. goto fail;
  713. priv->frequency = freq;
  714. priv->bandwidth = 0;
  715. fail:
  716. return ret;
  717. }
  718. static int tda18271_release(struct dvb_frontend *fe)
  719. {
  720. struct tda18271_priv *priv = fe->tuner_priv;
  721. mutex_lock(&tda18271_list_mutex);
  722. priv->count--;
  723. if (!priv->count) {
  724. tda_dbg("destroying instance @ %d-%04x\n",
  725. i2c_adapter_id(priv->i2c_adap),
  726. priv->i2c_addr);
  727. list_del(&priv->tda18271_list);
  728. kfree(priv);
  729. }
  730. mutex_unlock(&tda18271_list_mutex);
  731. fe->tuner_priv = NULL;
  732. return 0;
  733. }
  734. static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  735. {
  736. struct tda18271_priv *priv = fe->tuner_priv;
  737. *frequency = priv->frequency;
  738. return 0;
  739. }
  740. static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  741. {
  742. struct tda18271_priv *priv = fe->tuner_priv;
  743. *bandwidth = priv->bandwidth;
  744. return 0;
  745. }
  746. /* ------------------------------------------------------------------ */
  747. #define tda18271_update_std(std_cfg, name) do { \
  748. if (map->std_cfg.if_freq + map->std_cfg.std_bits > 0) { \
  749. tda_dbg("Using custom std config for %s\n", name); \
  750. memcpy(&std->std_cfg, &map->std_cfg, \
  751. sizeof(struct tda18271_std_map_item)); \
  752. } } while (0)
  753. #define tda18271_dump_std_item(std_cfg, name) do { \
  754. tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n", \
  755. name, std->std_cfg.if_freq, std->std_cfg.std_bits); \
  756. } while (0)
  757. static int tda18271_dump_std_map(struct dvb_frontend *fe)
  758. {
  759. struct tda18271_priv *priv = fe->tuner_priv;
  760. struct tda18271_std_map *std = &priv->std;
  761. tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
  762. tda18271_dump_std_item(fm_radio, "fm");
  763. tda18271_dump_std_item(atv_b, "pal b");
  764. tda18271_dump_std_item(atv_dk, "pal dk");
  765. tda18271_dump_std_item(atv_gh, "pal gh");
  766. tda18271_dump_std_item(atv_i, "pal i");
  767. tda18271_dump_std_item(atv_l, "pal l");
  768. tda18271_dump_std_item(atv_lc, "pal l'");
  769. tda18271_dump_std_item(atv_mn, "atv mn");
  770. tda18271_dump_std_item(atsc_6, "atsc 6");
  771. tda18271_dump_std_item(dvbt_6, "dvbt 6");
  772. tda18271_dump_std_item(dvbt_7, "dvbt 7");
  773. tda18271_dump_std_item(dvbt_8, "dvbt 8");
  774. tda18271_dump_std_item(qam_6, "qam 6");
  775. tda18271_dump_std_item(qam_8, "qam 8");
  776. return 0;
  777. }
  778. static int tda18271_update_std_map(struct dvb_frontend *fe,
  779. struct tda18271_std_map *map)
  780. {
  781. struct tda18271_priv *priv = fe->tuner_priv;
  782. struct tda18271_std_map *std = &priv->std;
  783. if (!map)
  784. return -EINVAL;
  785. tda18271_update_std(fm_radio, "fm");
  786. tda18271_update_std(atv_b, "atv b");
  787. tda18271_update_std(atv_dk, "atv dk");
  788. tda18271_update_std(atv_gh, "atv gh");
  789. tda18271_update_std(atv_i, "atv i");
  790. tda18271_update_std(atv_l, "atv l");
  791. tda18271_update_std(atv_lc, "atv l'");
  792. tda18271_update_std(atv_mn, "atv mn");
  793. tda18271_update_std(atsc_6, "atsc 6");
  794. tda18271_update_std(dvbt_6, "dvbt 6");
  795. tda18271_update_std(dvbt_7, "dvbt 7");
  796. tda18271_update_std(dvbt_8, "dvbt 8");
  797. tda18271_update_std(qam_6, "qam 6");
  798. tda18271_update_std(qam_8, "qam 8");
  799. return 0;
  800. }
  801. static int tda18271_get_id(struct dvb_frontend *fe)
  802. {
  803. struct tda18271_priv *priv = fe->tuner_priv;
  804. unsigned char *regs = priv->tda18271_regs;
  805. char *name;
  806. int ret = 0;
  807. mutex_lock(&priv->lock);
  808. tda18271_read_regs(fe);
  809. mutex_unlock(&priv->lock);
  810. switch (regs[R_ID] & 0x7f) {
  811. case 3:
  812. name = "TDA18271HD/C1";
  813. priv->id = TDA18271HDC1;
  814. break;
  815. case 4:
  816. name = "TDA18271HD/C2";
  817. priv->id = TDA18271HDC2;
  818. break;
  819. default:
  820. name = "Unknown device";
  821. ret = -EINVAL;
  822. break;
  823. }
  824. tda_info("%s detected @ %d-%04x%s\n", name,
  825. i2c_adapter_id(priv->i2c_adap), priv->i2c_addr,
  826. (0 == ret) ? "" : ", device not supported.");
  827. return ret;
  828. }
  829. static struct dvb_tuner_ops tda18271_tuner_ops = {
  830. .info = {
  831. .name = "NXP TDA18271HD",
  832. .frequency_min = 45000000,
  833. .frequency_max = 864000000,
  834. .frequency_step = 62500
  835. },
  836. .init = tda18271_init,
  837. .set_params = tda18271_set_params,
  838. .set_analog_params = tda18271_set_analog_params,
  839. .release = tda18271_release,
  840. .get_frequency = tda18271_get_frequency,
  841. .get_bandwidth = tda18271_get_bandwidth,
  842. };
  843. struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
  844. struct i2c_adapter *i2c,
  845. struct tda18271_config *cfg)
  846. {
  847. struct tda18271_priv *priv = NULL;
  848. int state_found = 0;
  849. mutex_lock(&tda18271_list_mutex);
  850. list_for_each_entry(priv, &tda18271_list, tda18271_list) {
  851. if ((i2c_adapter_id(priv->i2c_adap) == i2c_adapter_id(i2c)) &&
  852. (priv->i2c_addr == addr)) {
  853. tda_dbg("attaching existing tuner @ %d-%04x\n",
  854. i2c_adapter_id(priv->i2c_adap),
  855. priv->i2c_addr);
  856. priv->count++;
  857. fe->tuner_priv = priv;
  858. state_found = 1;
  859. /* allow dvb driver to override i2c gate setting */
  860. if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
  861. priv->gate = cfg->gate;
  862. break;
  863. }
  864. }
  865. if (state_found == 0) {
  866. tda_dbg("creating new tuner instance @ %d-%04x\n",
  867. i2c_adapter_id(i2c), addr);
  868. priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
  869. if (priv == NULL) {
  870. mutex_unlock(&tda18271_list_mutex);
  871. return NULL;
  872. }
  873. priv->i2c_addr = addr;
  874. priv->i2c_adap = i2c;
  875. priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
  876. priv->cal_initialized = false;
  877. mutex_init(&priv->lock);
  878. priv->count++;
  879. fe->tuner_priv = priv;
  880. list_add_tail(&priv->tda18271_list, &tda18271_list);
  881. if (tda18271_get_id(fe) < 0)
  882. goto fail;
  883. if (tda18271_assign_map_layout(fe) < 0)
  884. goto fail;
  885. mutex_lock(&priv->lock);
  886. tda18271_init_regs(fe);
  887. mutex_unlock(&priv->lock);
  888. }
  889. /* override default std map with values in config struct */
  890. if ((cfg) && (cfg->std_map))
  891. tda18271_update_std_map(fe, cfg->std_map);
  892. mutex_unlock(&tda18271_list_mutex);
  893. memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
  894. sizeof(struct dvb_tuner_ops));
  895. if (tda18271_debug & DBG_MAP)
  896. tda18271_dump_std_map(fe);
  897. return fe;
  898. fail:
  899. mutex_unlock(&tda18271_list_mutex);
  900. tda18271_release(fe);
  901. return NULL;
  902. }
  903. EXPORT_SYMBOL_GPL(tda18271_attach);
  904. MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
  905. MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
  906. MODULE_LICENSE("GPL");
  907. MODULE_VERSION("0.2");
  908. /*
  909. * Overrides for Emacs so that we follow Linus's tabbing style.
  910. * ---------------------------------------------------------------------------
  911. * Local variables:
  912. * c-basic-offset: 8
  913. * End:
  914. */