tda18271-fe.c 27 KB

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