tuner-simple.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * i2c tv tuner chip device driver
  3. * controls all those simple 4-control-bytes style tuners.
  4. *
  5. * This "tuner-simple" module was split apart from the original "tuner" module.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/i2c.h>
  9. #include <linux/videodev2.h>
  10. #include <media/tuner.h>
  11. #include <media/v4l2-common.h>
  12. #include <media/tuner-types.h>
  13. #include "tuner-i2c.h"
  14. #include "tuner-simple.h"
  15. static int debug;
  16. module_param(debug, int, 0644);
  17. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  18. #define TUNER_SIMPLE_MAX 64
  19. static unsigned int simple_devcount;
  20. static int offset;
  21. module_param(offset, int, 0664);
  22. MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
  23. static unsigned int atv_input[TUNER_SIMPLE_MAX] = \
  24. { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
  25. static unsigned int dtv_input[TUNER_SIMPLE_MAX] = \
  26. { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
  27. module_param_array(atv_input, int, NULL, 0644);
  28. module_param_array(dtv_input, int, NULL, 0644);
  29. MODULE_PARM_DESC(atv_input, "specify atv rf input, 0 for autoselect");
  30. MODULE_PARM_DESC(dtv_input, "specify dtv rf input, 0 for autoselect");
  31. /* ---------------------------------------------------------------------- */
  32. /* tv standard selection for Temic 4046 FM5
  33. this value takes the low bits of control byte 2
  34. from datasheet Rev.01, Feb.00
  35. standard BG I L L2 D
  36. picture IF 38.9 38.9 38.9 33.95 38.9
  37. sound 1 33.4 32.9 32.4 40.45 32.4
  38. sound 2 33.16
  39. NICAM 33.05 32.348 33.05 33.05
  40. */
  41. #define TEMIC_SET_PAL_I 0x05
  42. #define TEMIC_SET_PAL_DK 0x09
  43. #define TEMIC_SET_PAL_L 0x0a /* SECAM ? */
  44. #define TEMIC_SET_PAL_L2 0x0b /* change IF ! */
  45. #define TEMIC_SET_PAL_BG 0x0c
  46. /* tv tuner system standard selection for Philips FQ1216ME
  47. this value takes the low bits of control byte 2
  48. from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
  49. standard BG DK I L L`
  50. picture carrier 38.90 38.90 38.90 38.90 33.95
  51. colour 34.47 34.47 34.47 34.47 38.38
  52. sound 1 33.40 32.40 32.90 32.40 40.45
  53. sound 2 33.16 - - - -
  54. NICAM 33.05 33.05 32.35 33.05 39.80
  55. */
  56. #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
  57. #define PHILIPS_SET_PAL_BGDK 0x09
  58. #define PHILIPS_SET_PAL_L2 0x0a
  59. #define PHILIPS_SET_PAL_L 0x0b
  60. /* system switching for Philips FI1216MF MK2
  61. from datasheet "1996 Jul 09",
  62. standard BG L L'
  63. picture carrier 38.90 38.90 33.95
  64. colour 34.47 34.37 38.38
  65. sound 1 33.40 32.40 40.45
  66. sound 2 33.16 - -
  67. NICAM 33.05 33.05 39.80
  68. */
  69. #define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
  70. #define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
  71. #define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
  72. /* Control byte */
  73. #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
  74. #define TUNER_RATIO_SELECT_50 0x00
  75. #define TUNER_RATIO_SELECT_32 0x02
  76. #define TUNER_RATIO_SELECT_166 0x04
  77. #define TUNER_RATIO_SELECT_62 0x06
  78. #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
  79. /* Status byte */
  80. #define TUNER_POR 0x80
  81. #define TUNER_FL 0x40
  82. #define TUNER_MODE 0x38
  83. #define TUNER_AFC 0x07
  84. #define TUNER_SIGNAL 0x07
  85. #define TUNER_STEREO 0x10
  86. #define TUNER_PLL_LOCKED 0x40
  87. #define TUNER_STEREO_MK3 0x04
  88. static DEFINE_MUTEX(tuner_simple_list_mutex);
  89. static LIST_HEAD(hybrid_tuner_instance_list);
  90. struct tuner_simple_priv {
  91. unsigned int nr;
  92. u16 last_div;
  93. struct tuner_i2c_props i2c_props;
  94. struct list_head hybrid_tuner_instance_list;
  95. unsigned int type;
  96. struct tunertype *tun;
  97. u32 frequency;
  98. u32 bandwidth;
  99. bool radio_mode;
  100. };
  101. /* ---------------------------------------------------------------------- */
  102. static int tuner_read_status(struct dvb_frontend *fe)
  103. {
  104. struct tuner_simple_priv *priv = fe->tuner_priv;
  105. unsigned char byte;
  106. if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
  107. return 0;
  108. return byte;
  109. }
  110. static inline int tuner_signal(const int status)
  111. {
  112. return (status & TUNER_SIGNAL) << 13;
  113. }
  114. static inline int tuner_stereo(const int type, const int status)
  115. {
  116. switch (type) {
  117. case TUNER_PHILIPS_FM1216ME_MK3:
  118. case TUNER_PHILIPS_FM1236_MK3:
  119. case TUNER_PHILIPS_FM1256_IH3:
  120. case TUNER_LG_NTSC_TAPE:
  121. case TUNER_TCL_MF02GIP_5N:
  122. return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  123. case TUNER_PHILIPS_FM1216MK5:
  124. return status | TUNER_STEREO;
  125. default:
  126. return status & TUNER_STEREO;
  127. }
  128. }
  129. static inline int tuner_islocked(const int status)
  130. {
  131. return (status & TUNER_FL);
  132. }
  133. static inline int tuner_afcstatus(const int status)
  134. {
  135. return (status & TUNER_AFC) - 2;
  136. }
  137. static int simple_get_status(struct dvb_frontend *fe, u32 *status)
  138. {
  139. struct tuner_simple_priv *priv = fe->tuner_priv;
  140. int tuner_status;
  141. if (priv->i2c_props.adap == NULL)
  142. return -EINVAL;
  143. tuner_status = tuner_read_status(fe);
  144. *status = 0;
  145. if (tuner_islocked(tuner_status))
  146. *status = TUNER_STATUS_LOCKED;
  147. if (tuner_stereo(priv->type, tuner_status))
  148. *status |= TUNER_STATUS_STEREO;
  149. tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
  150. return 0;
  151. }
  152. static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  153. {
  154. struct tuner_simple_priv *priv = fe->tuner_priv;
  155. int signal;
  156. if (priv->i2c_props.adap == NULL || !priv->radio_mode)
  157. return -EINVAL;
  158. signal = tuner_signal(tuner_read_status(fe));
  159. *strength = signal;
  160. tuner_dbg("Signal strength: %d\n", signal);
  161. return 0;
  162. }
  163. /* ---------------------------------------------------------------------- */
  164. static inline char *tuner_param_name(enum param_type type)
  165. {
  166. char *name;
  167. switch (type) {
  168. case TUNER_PARAM_TYPE_RADIO:
  169. name = "radio";
  170. break;
  171. case TUNER_PARAM_TYPE_PAL:
  172. name = "pal";
  173. break;
  174. case TUNER_PARAM_TYPE_SECAM:
  175. name = "secam";
  176. break;
  177. case TUNER_PARAM_TYPE_NTSC:
  178. name = "ntsc";
  179. break;
  180. case TUNER_PARAM_TYPE_DIGITAL:
  181. name = "digital";
  182. break;
  183. default:
  184. name = "unknown";
  185. break;
  186. }
  187. return name;
  188. }
  189. static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
  190. enum param_type desired_type)
  191. {
  192. struct tuner_simple_priv *priv = fe->tuner_priv;
  193. struct tunertype *tun = priv->tun;
  194. int i;
  195. for (i = 0; i < tun->count; i++)
  196. if (desired_type == tun->params[i].type)
  197. break;
  198. /* use default tuner params if desired_type not available */
  199. if (i == tun->count) {
  200. tuner_dbg("desired params (%s) undefined for tuner %d\n",
  201. tuner_param_name(desired_type), priv->type);
  202. i = 0;
  203. }
  204. tuner_dbg("using tuner params #%d (%s)\n", i,
  205. tuner_param_name(tun->params[i].type));
  206. return &tun->params[i];
  207. }
  208. static int simple_config_lookup(struct dvb_frontend *fe,
  209. struct tuner_params *t_params,
  210. unsigned *frequency, u8 *config, u8 *cb)
  211. {
  212. struct tuner_simple_priv *priv = fe->tuner_priv;
  213. int i;
  214. for (i = 0; i < t_params->count; i++) {
  215. if (*frequency > t_params->ranges[i].limit)
  216. continue;
  217. break;
  218. }
  219. if (i == t_params->count) {
  220. tuner_dbg("frequency out of range (%d > %d)\n",
  221. *frequency, t_params->ranges[i - 1].limit);
  222. *frequency = t_params->ranges[--i].limit;
  223. }
  224. *config = t_params->ranges[i].config;
  225. *cb = t_params->ranges[i].cb;
  226. tuner_dbg("freq = %d.%02d (%d), range = %d, "
  227. "config = 0x%02x, cb = 0x%02x\n",
  228. *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
  229. i, *config, *cb);
  230. return i;
  231. }
  232. /* ---------------------------------------------------------------------- */
  233. static void simple_set_rf_input(struct dvb_frontend *fe,
  234. u8 *config, u8 *cb, unsigned int rf)
  235. {
  236. struct tuner_simple_priv *priv = fe->tuner_priv;
  237. switch (priv->type) {
  238. case TUNER_PHILIPS_TUV1236D:
  239. switch (rf) {
  240. case 1:
  241. *cb |= 0x08;
  242. break;
  243. default:
  244. *cb &= ~0x08;
  245. break;
  246. }
  247. break;
  248. case TUNER_PHILIPS_FCV1236D:
  249. switch (rf) {
  250. case 1:
  251. *cb |= 0x01;
  252. break;
  253. default:
  254. *cb &= ~0x01;
  255. break;
  256. }
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. static int simple_std_setup(struct dvb_frontend *fe,
  263. struct analog_parameters *params,
  264. u8 *config, u8 *cb)
  265. {
  266. struct tuner_simple_priv *priv = fe->tuner_priv;
  267. int rc;
  268. /* tv norm specific stuff for multi-norm tuners */
  269. switch (priv->type) {
  270. case TUNER_PHILIPS_SECAM: /* FI1216MF */
  271. /* 0x01 -> ??? no change ??? */
  272. /* 0x02 -> PAL BDGHI / SECAM L */
  273. /* 0x04 -> ??? PAL others / SECAM others ??? */
  274. *cb &= ~0x03;
  275. if (params->std & V4L2_STD_SECAM_L)
  276. /* also valid for V4L2_STD_SECAM */
  277. *cb |= PHILIPS_MF_SET_STD_L;
  278. else if (params->std & V4L2_STD_SECAM_LC)
  279. *cb |= PHILIPS_MF_SET_STD_LC;
  280. else /* V4L2_STD_B|V4L2_STD_GH */
  281. *cb |= PHILIPS_MF_SET_STD_BG;
  282. break;
  283. case TUNER_TEMIC_4046FM5:
  284. *cb &= ~0x0f;
  285. if (params->std & V4L2_STD_PAL_BG) {
  286. *cb |= TEMIC_SET_PAL_BG;
  287. } else if (params->std & V4L2_STD_PAL_I) {
  288. *cb |= TEMIC_SET_PAL_I;
  289. } else if (params->std & V4L2_STD_PAL_DK) {
  290. *cb |= TEMIC_SET_PAL_DK;
  291. } else if (params->std & V4L2_STD_SECAM_L) {
  292. *cb |= TEMIC_SET_PAL_L;
  293. }
  294. break;
  295. case TUNER_PHILIPS_FQ1216ME:
  296. *cb &= ~0x0f;
  297. if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  298. *cb |= PHILIPS_SET_PAL_BGDK;
  299. } else if (params->std & V4L2_STD_PAL_I) {
  300. *cb |= PHILIPS_SET_PAL_I;
  301. } else if (params->std & V4L2_STD_SECAM_L) {
  302. *cb |= PHILIPS_SET_PAL_L;
  303. }
  304. break;
  305. case TUNER_PHILIPS_FCV1236D:
  306. /* 0x00 -> ATSC antenna input 1 */
  307. /* 0x01 -> ATSC antenna input 2 */
  308. /* 0x02 -> NTSC antenna input 1 */
  309. /* 0x03 -> NTSC antenna input 2 */
  310. *cb &= ~0x03;
  311. if (!(params->std & V4L2_STD_ATSC))
  312. *cb |= 2;
  313. break;
  314. case TUNER_MICROTUNE_4042FI5:
  315. /* Set the charge pump for fast tuning */
  316. *config |= TUNER_CHARGE_PUMP;
  317. break;
  318. case TUNER_PHILIPS_TUV1236D:
  319. {
  320. struct tuner_i2c_props i2c = priv->i2c_props;
  321. /* 0x40 -> ATSC antenna input 1 */
  322. /* 0x48 -> ATSC antenna input 2 */
  323. /* 0x00 -> NTSC antenna input 1 */
  324. /* 0x08 -> NTSC antenna input 2 */
  325. u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
  326. *cb &= ~0x40;
  327. if (params->std & V4L2_STD_ATSC) {
  328. *cb |= 0x40;
  329. buffer[1] = 0x04;
  330. }
  331. /* set to the correct mode (analog or digital) */
  332. i2c.addr = 0x0a;
  333. rc = tuner_i2c_xfer_send(&i2c, &buffer[0], 2);
  334. if (2 != rc)
  335. tuner_warn("i2c i/o error: rc == %d "
  336. "(should be 2)\n", rc);
  337. rc = tuner_i2c_xfer_send(&i2c, &buffer[2], 2);
  338. if (2 != rc)
  339. tuner_warn("i2c i/o error: rc == %d "
  340. "(should be 2)\n", rc);
  341. break;
  342. }
  343. }
  344. if (atv_input[priv->nr])
  345. simple_set_rf_input(fe, config, cb, atv_input[priv->nr]);
  346. return 0;
  347. }
  348. static int simple_set_aux_byte(struct dvb_frontend *fe, u8 config, u8 aux)
  349. {
  350. struct tuner_simple_priv *priv = fe->tuner_priv;
  351. int rc;
  352. u8 buffer[2];
  353. buffer[0] = (config & ~0x38) | 0x18;
  354. buffer[1] = aux;
  355. tuner_dbg("setting aux byte: 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  356. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
  357. if (2 != rc)
  358. tuner_warn("i2c i/o error: rc == %d (should be 2)\n", rc);
  359. return rc == 2 ? 0 : rc;
  360. }
  361. static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
  362. u16 div, u8 config, u8 cb)
  363. {
  364. struct tuner_simple_priv *priv = fe->tuner_priv;
  365. int rc;
  366. switch (priv->type) {
  367. case TUNER_LG_TDVS_H06XF:
  368. simple_set_aux_byte(fe, config, 0x20);
  369. break;
  370. case TUNER_PHILIPS_FQ1216LME_MK3:
  371. simple_set_aux_byte(fe, config, 0x60); /* External AGC */
  372. break;
  373. case TUNER_MICROTUNE_4042FI5:
  374. {
  375. /* FIXME - this may also work for other tuners */
  376. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  377. u8 status_byte = 0;
  378. /* Wait until the PLL locks */
  379. for (;;) {
  380. if (time_after(jiffies, timeout))
  381. return 0;
  382. rc = tuner_i2c_xfer_recv(&priv->i2c_props,
  383. &status_byte, 1);
  384. if (1 != rc) {
  385. tuner_warn("i2c i/o read error: rc == %d "
  386. "(should be 1)\n", rc);
  387. break;
  388. }
  389. if (status_byte & TUNER_PLL_LOCKED)
  390. break;
  391. udelay(10);
  392. }
  393. /* Set the charge pump for optimized phase noise figure */
  394. config &= ~TUNER_CHARGE_PUMP;
  395. buffer[0] = (div>>8) & 0x7f;
  396. buffer[1] = div & 0xff;
  397. buffer[2] = config;
  398. buffer[3] = cb;
  399. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  400. buffer[0], buffer[1], buffer[2], buffer[3]);
  401. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  402. if (4 != rc)
  403. tuner_warn("i2c i/o error: rc == %d "
  404. "(should be 4)\n", rc);
  405. break;
  406. }
  407. }
  408. return 0;
  409. }
  410. static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
  411. {
  412. struct tuner_simple_priv *priv = fe->tuner_priv;
  413. switch (priv->type) {
  414. case TUNER_TENA_9533_DI:
  415. case TUNER_YMEC_TVF_5533MF:
  416. tuner_dbg("This tuner doesn't have FM. "
  417. "Most cards have a TEA5767 for FM\n");
  418. return 0;
  419. case TUNER_PHILIPS_FM1216ME_MK3:
  420. case TUNER_PHILIPS_FM1236_MK3:
  421. case TUNER_PHILIPS_FMD1216ME_MK3:
  422. case TUNER_PHILIPS_FMD1216MEX_MK3:
  423. case TUNER_LG_NTSC_TAPE:
  424. case TUNER_PHILIPS_FM1256_IH3:
  425. case TUNER_TCL_MF02GIP_5N:
  426. buffer[3] = 0x19;
  427. break;
  428. case TUNER_PHILIPS_FM1216MK5:
  429. buffer[2] = 0x88;
  430. buffer[3] = 0x09;
  431. break;
  432. case TUNER_TNF_5335MF:
  433. buffer[3] = 0x11;
  434. break;
  435. case TUNER_LG_PAL_FM:
  436. buffer[3] = 0xa5;
  437. break;
  438. case TUNER_THOMSON_DTT761X:
  439. buffer[3] = 0x39;
  440. break;
  441. case TUNER_PHILIPS_FQ1216LME_MK3:
  442. case TUNER_PHILIPS_FQ1236_MK5:
  443. tuner_err("This tuner doesn't have FM\n");
  444. /* Set the low band for sanity, since it covers 88-108 MHz */
  445. buffer[3] = 0x01;
  446. break;
  447. case TUNER_MICROTUNE_4049FM5:
  448. default:
  449. buffer[3] = 0xa4;
  450. break;
  451. }
  452. return 0;
  453. }
  454. /* ---------------------------------------------------------------------- */
  455. static int simple_set_tv_freq(struct dvb_frontend *fe,
  456. struct analog_parameters *params)
  457. {
  458. struct tuner_simple_priv *priv = fe->tuner_priv;
  459. u8 config, cb;
  460. u16 div;
  461. u8 buffer[4];
  462. int rc, IFPCoff, i;
  463. enum param_type desired_type;
  464. struct tuner_params *t_params;
  465. /* IFPCoff = Video Intermediate Frequency - Vif:
  466. 940 =16*58.75 NTSC/J (Japan)
  467. 732 =16*45.75 M/N STD
  468. 704 =16*44 ATSC (at DVB code)
  469. 632 =16*39.50 I U.K.
  470. 622.4=16*38.90 B/G D/K I, L STD
  471. 592 =16*37.00 D China
  472. 590 =16.36.875 B Australia
  473. 543.2=16*33.95 L' STD
  474. 171.2=16*10.70 FM Radio (at set_radio_freq)
  475. */
  476. if (params->std == V4L2_STD_NTSC_M_JP) {
  477. IFPCoff = 940;
  478. desired_type = TUNER_PARAM_TYPE_NTSC;
  479. } else if ((params->std & V4L2_STD_MN) &&
  480. !(params->std & ~V4L2_STD_MN)) {
  481. IFPCoff = 732;
  482. desired_type = TUNER_PARAM_TYPE_NTSC;
  483. } else if (params->std == V4L2_STD_SECAM_LC) {
  484. IFPCoff = 543;
  485. desired_type = TUNER_PARAM_TYPE_SECAM;
  486. } else {
  487. IFPCoff = 623;
  488. desired_type = TUNER_PARAM_TYPE_PAL;
  489. }
  490. t_params = simple_tuner_params(fe, desired_type);
  491. i = simple_config_lookup(fe, t_params, &params->frequency,
  492. &config, &cb);
  493. div = params->frequency + IFPCoff + offset;
  494. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
  495. "Offset=%d.%02d MHz, div=%0d\n",
  496. params->frequency / 16, params->frequency % 16 * 100 / 16,
  497. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  498. offset / 16, offset % 16 * 100 / 16, div);
  499. /* tv norm specific stuff for multi-norm tuners */
  500. simple_std_setup(fe, params, &config, &cb);
  501. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  502. buffer[0] = config;
  503. buffer[1] = cb;
  504. buffer[2] = (div>>8) & 0x7f;
  505. buffer[3] = div & 0xff;
  506. } else {
  507. buffer[0] = (div>>8) & 0x7f;
  508. buffer[1] = div & 0xff;
  509. buffer[2] = config;
  510. buffer[3] = cb;
  511. }
  512. priv->last_div = div;
  513. if (t_params->has_tda9887) {
  514. struct v4l2_priv_tun_config tda9887_cfg;
  515. int tda_config = 0;
  516. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  517. V4L2_STD_SECAM_LC)) &&
  518. !(params->std & ~(V4L2_STD_SECAM_L |
  519. V4L2_STD_SECAM_LC));
  520. tda9887_cfg.tuner = TUNER_TDA9887;
  521. tda9887_cfg.priv = &tda_config;
  522. if (params->std == V4L2_STD_SECAM_LC) {
  523. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  524. tda_config |= TDA9887_PORT1_ACTIVE;
  525. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  526. tda_config |= TDA9887_PORT2_ACTIVE;
  527. } else {
  528. if (t_params->port1_active)
  529. tda_config |= TDA9887_PORT1_ACTIVE;
  530. if (t_params->port2_active)
  531. tda_config |= TDA9887_PORT2_ACTIVE;
  532. }
  533. if (t_params->intercarrier_mode)
  534. tda_config |= TDA9887_INTERCARRIER;
  535. if (is_secam_l) {
  536. if (i == 0 && t_params->default_top_secam_low)
  537. tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
  538. else if (i == 1 && t_params->default_top_secam_mid)
  539. tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
  540. else if (t_params->default_top_secam_high)
  541. tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
  542. } else {
  543. if (i == 0 && t_params->default_top_low)
  544. tda_config |= TDA9887_TOP(t_params->default_top_low);
  545. else if (i == 1 && t_params->default_top_mid)
  546. tda_config |= TDA9887_TOP(t_params->default_top_mid);
  547. else if (t_params->default_top_high)
  548. tda_config |= TDA9887_TOP(t_params->default_top_high);
  549. }
  550. if (t_params->default_pll_gating_18)
  551. tda_config |= TDA9887_GATING_18;
  552. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  553. &tda9887_cfg);
  554. }
  555. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  556. buffer[0], buffer[1], buffer[2], buffer[3]);
  557. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  558. if (4 != rc)
  559. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  560. simple_post_tune(fe, &buffer[0], div, config, cb);
  561. return 0;
  562. }
  563. static int simple_set_radio_freq(struct dvb_frontend *fe,
  564. struct analog_parameters *params)
  565. {
  566. struct tunertype *tun;
  567. struct tuner_simple_priv *priv = fe->tuner_priv;
  568. u8 buffer[4];
  569. u16 div;
  570. int rc, j;
  571. struct tuner_params *t_params;
  572. unsigned int freq = params->frequency;
  573. tun = priv->tun;
  574. for (j = tun->count-1; j > 0; j--)
  575. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  576. break;
  577. /* default t_params (j=0) will be used if desired type wasn't found */
  578. t_params = &tun->params[j];
  579. /* Select Radio 1st IF used */
  580. switch (t_params->radio_if) {
  581. case 0: /* 10.7 MHz */
  582. freq += (unsigned int)(10.7*16000);
  583. break;
  584. case 1: /* 33.3 MHz */
  585. freq += (unsigned int)(33.3*16000);
  586. break;
  587. case 2: /* 41.3 MHz */
  588. freq += (unsigned int)(41.3*16000);
  589. break;
  590. default:
  591. tuner_warn("Unsupported radio_if value %d\n",
  592. t_params->radio_if);
  593. return 0;
  594. }
  595. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  596. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  597. /* Bandswitch byte */
  598. simple_radio_bandswitch(fe, &buffer[0]);
  599. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  600. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  601. freq * (1/800) */
  602. div = (freq + 400) / 800;
  603. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  604. buffer[0] = buffer[2];
  605. buffer[1] = buffer[3];
  606. buffer[2] = (div>>8) & 0x7f;
  607. buffer[3] = div & 0xff;
  608. } else {
  609. buffer[0] = (div>>8) & 0x7f;
  610. buffer[1] = div & 0xff;
  611. }
  612. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  613. buffer[0], buffer[1], buffer[2], buffer[3]);
  614. priv->last_div = div;
  615. if (t_params->has_tda9887) {
  616. int config = 0;
  617. struct v4l2_priv_tun_config tda9887_cfg;
  618. tda9887_cfg.tuner = TUNER_TDA9887;
  619. tda9887_cfg.priv = &config;
  620. if (t_params->port1_active &&
  621. !t_params->port1_fm_high_sensitivity)
  622. config |= TDA9887_PORT1_ACTIVE;
  623. if (t_params->port2_active &&
  624. !t_params->port2_fm_high_sensitivity)
  625. config |= TDA9887_PORT2_ACTIVE;
  626. if (t_params->intercarrier_mode)
  627. config |= TDA9887_INTERCARRIER;
  628. /* if (t_params->port1_set_for_fm_mono)
  629. config &= ~TDA9887_PORT1_ACTIVE;*/
  630. if (t_params->fm_gain_normal)
  631. config |= TDA9887_GAIN_NORMAL;
  632. if (t_params->radio_if == 2)
  633. config |= TDA9887_RIF_41_3;
  634. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  635. &tda9887_cfg);
  636. }
  637. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  638. if (4 != rc)
  639. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  640. /* Write AUX byte */
  641. switch (priv->type) {
  642. case TUNER_PHILIPS_FM1216ME_MK3:
  643. buffer[2] = 0x98;
  644. buffer[3] = 0x20; /* set TOP AGC */
  645. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  646. if (4 != rc)
  647. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  648. break;
  649. }
  650. return 0;
  651. }
  652. static int simple_set_params(struct dvb_frontend *fe,
  653. struct analog_parameters *params)
  654. {
  655. struct tuner_simple_priv *priv = fe->tuner_priv;
  656. int ret = -EINVAL;
  657. if (priv->i2c_props.adap == NULL)
  658. return -EINVAL;
  659. switch (params->mode) {
  660. case V4L2_TUNER_RADIO:
  661. priv->radio_mode = true;
  662. ret = simple_set_radio_freq(fe, params);
  663. priv->frequency = params->frequency * 125 / 2;
  664. break;
  665. case V4L2_TUNER_ANALOG_TV:
  666. case V4L2_TUNER_DIGITAL_TV:
  667. priv->radio_mode = false;
  668. ret = simple_set_tv_freq(fe, params);
  669. priv->frequency = params->frequency * 62500;
  670. break;
  671. }
  672. priv->bandwidth = 0;
  673. return ret;
  674. }
  675. static void simple_set_dvb(struct dvb_frontend *fe, u8 *buf,
  676. const u32 delsys,
  677. const u32 frequency,
  678. const u32 bandwidth)
  679. {
  680. struct tuner_simple_priv *priv = fe->tuner_priv;
  681. switch (priv->type) {
  682. case TUNER_PHILIPS_FMD1216ME_MK3:
  683. case TUNER_PHILIPS_FMD1216MEX_MK3:
  684. if (bandwidth == 8000000 &&
  685. frequency >= 158870000)
  686. buf[3] |= 0x08;
  687. break;
  688. case TUNER_PHILIPS_TD1316:
  689. /* determine band */
  690. buf[3] |= (frequency < 161000000) ? 1 :
  691. (frequency < 444000000) ? 2 : 4;
  692. /* setup PLL filter */
  693. if (bandwidth == 8000000)
  694. buf[3] |= 1 << 3;
  695. break;
  696. case TUNER_PHILIPS_TUV1236D:
  697. case TUNER_PHILIPS_FCV1236D:
  698. {
  699. unsigned int new_rf;
  700. if (dtv_input[priv->nr])
  701. new_rf = dtv_input[priv->nr];
  702. else
  703. switch (delsys) {
  704. case SYS_DVBC_ANNEX_B:
  705. new_rf = 1;
  706. break;
  707. case SYS_ATSC:
  708. default:
  709. new_rf = 0;
  710. break;
  711. }
  712. simple_set_rf_input(fe, &buf[2], &buf[3], new_rf);
  713. break;
  714. }
  715. default:
  716. break;
  717. }
  718. }
  719. static u32 simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
  720. const u32 delsys,
  721. const u32 freq,
  722. const u32 bw)
  723. {
  724. /* This function returns the tuned frequency on success, 0 on error */
  725. struct tuner_simple_priv *priv = fe->tuner_priv;
  726. struct tunertype *tun = priv->tun;
  727. static struct tuner_params *t_params;
  728. u8 config, cb;
  729. u32 div;
  730. int ret;
  731. u32 frequency = freq / 62500;
  732. if (!tun->stepsize) {
  733. /* tuner-core was loaded before the digital tuner was
  734. * configured and somehow picked the wrong tuner type */
  735. tuner_err("attempt to treat tuner %d (%s) as digital tuner "
  736. "without stepsize defined.\n",
  737. priv->type, priv->tun->name);
  738. return 0; /* failure */
  739. }
  740. t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
  741. ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
  742. if (ret < 0)
  743. return 0; /* failure */
  744. div = ((frequency + t_params->iffreq) * 62500 + offset +
  745. tun->stepsize/2) / tun->stepsize;
  746. buf[0] = div >> 8;
  747. buf[1] = div & 0xff;
  748. buf[2] = config;
  749. buf[3] = cb;
  750. simple_set_dvb(fe, buf, delsys, freq, bw);
  751. tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  752. tun->name, div, buf[0], buf[1], buf[2], buf[3]);
  753. /* calculate the frequency we set it to */
  754. return (div * tun->stepsize) - t_params->iffreq;
  755. }
  756. static int simple_dvb_calc_regs(struct dvb_frontend *fe,
  757. u8 *buf, int buf_len)
  758. {
  759. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  760. u32 delsys = c->delivery_system;
  761. u32 bw = c->bandwidth_hz;
  762. struct tuner_simple_priv *priv = fe->tuner_priv;
  763. u32 frequency;
  764. if (buf_len < 5)
  765. return -EINVAL;
  766. frequency = simple_dvb_configure(fe, buf+1, delsys, c->frequency, bw);
  767. if (frequency == 0)
  768. return -EINVAL;
  769. buf[0] = priv->i2c_props.addr;
  770. priv->frequency = frequency;
  771. priv->bandwidth = c->bandwidth_hz;
  772. return 5;
  773. }
  774. static int simple_dvb_set_params(struct dvb_frontend *fe)
  775. {
  776. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  777. u32 delsys = c->delivery_system;
  778. u32 bw = c->bandwidth_hz;
  779. u32 freq = c->frequency;
  780. struct tuner_simple_priv *priv = fe->tuner_priv;
  781. u32 frequency;
  782. u32 prev_freq, prev_bw;
  783. int ret;
  784. u8 buf[5];
  785. if (priv->i2c_props.adap == NULL)
  786. return -EINVAL;
  787. prev_freq = priv->frequency;
  788. prev_bw = priv->bandwidth;
  789. frequency = simple_dvb_configure(fe, buf+1, delsys, freq, bw);
  790. if (frequency == 0)
  791. return -EINVAL;
  792. buf[0] = priv->i2c_props.addr;
  793. priv->frequency = frequency;
  794. priv->bandwidth = bw;
  795. /* put analog demod in standby when tuning digital */
  796. if (fe->ops.analog_ops.standby)
  797. fe->ops.analog_ops.standby(fe);
  798. if (fe->ops.i2c_gate_ctrl)
  799. fe->ops.i2c_gate_ctrl(fe, 1);
  800. /* buf[0] contains the i2c address, but *
  801. * we already have it in i2c_props.addr */
  802. ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
  803. if (ret != 4)
  804. goto fail;
  805. return 0;
  806. fail:
  807. /* calc_regs sets frequency and bandwidth. if we failed, unset them */
  808. priv->frequency = prev_freq;
  809. priv->bandwidth = prev_bw;
  810. return ret;
  811. }
  812. static int simple_init(struct dvb_frontend *fe)
  813. {
  814. struct tuner_simple_priv *priv = fe->tuner_priv;
  815. if (priv->i2c_props.adap == NULL)
  816. return -EINVAL;
  817. if (priv->tun->initdata) {
  818. int ret;
  819. if (fe->ops.i2c_gate_ctrl)
  820. fe->ops.i2c_gate_ctrl(fe, 1);
  821. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  822. priv->tun->initdata + 1,
  823. priv->tun->initdata[0]);
  824. if (ret != priv->tun->initdata[0])
  825. return ret;
  826. }
  827. return 0;
  828. }
  829. static int simple_sleep(struct dvb_frontend *fe)
  830. {
  831. struct tuner_simple_priv *priv = fe->tuner_priv;
  832. if (priv->i2c_props.adap == NULL)
  833. return -EINVAL;
  834. if (priv->tun->sleepdata) {
  835. int ret;
  836. if (fe->ops.i2c_gate_ctrl)
  837. fe->ops.i2c_gate_ctrl(fe, 1);
  838. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  839. priv->tun->sleepdata + 1,
  840. priv->tun->sleepdata[0]);
  841. if (ret != priv->tun->sleepdata[0])
  842. return ret;
  843. }
  844. return 0;
  845. }
  846. static int simple_release(struct dvb_frontend *fe)
  847. {
  848. struct tuner_simple_priv *priv = fe->tuner_priv;
  849. mutex_lock(&tuner_simple_list_mutex);
  850. if (priv)
  851. hybrid_tuner_release_state(priv);
  852. mutex_unlock(&tuner_simple_list_mutex);
  853. fe->tuner_priv = NULL;
  854. return 0;
  855. }
  856. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  857. {
  858. struct tuner_simple_priv *priv = fe->tuner_priv;
  859. *frequency = priv->frequency;
  860. return 0;
  861. }
  862. static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  863. {
  864. struct tuner_simple_priv *priv = fe->tuner_priv;
  865. *bandwidth = priv->bandwidth;
  866. return 0;
  867. }
  868. static struct dvb_tuner_ops simple_tuner_ops = {
  869. .init = simple_init,
  870. .sleep = simple_sleep,
  871. .set_analog_params = simple_set_params,
  872. .set_params = simple_dvb_set_params,
  873. .calc_regs = simple_dvb_calc_regs,
  874. .release = simple_release,
  875. .get_frequency = simple_get_frequency,
  876. .get_bandwidth = simple_get_bandwidth,
  877. .get_status = simple_get_status,
  878. .get_rf_strength = simple_get_rf_strength,
  879. };
  880. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  881. struct i2c_adapter *i2c_adap,
  882. u8 i2c_addr,
  883. unsigned int type)
  884. {
  885. struct tuner_simple_priv *priv = NULL;
  886. int instance;
  887. if (type >= tuner_count) {
  888. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  889. __func__, type, tuner_count-1);
  890. return NULL;
  891. }
  892. /* If i2c_adap is set, check that the tuner is at the correct address.
  893. * Otherwise, if i2c_adap is NULL, the tuner will be programmed directly
  894. * by the digital demod via calc_regs.
  895. */
  896. if (i2c_adap != NULL) {
  897. u8 b[1];
  898. struct i2c_msg msg = {
  899. .addr = i2c_addr, .flags = I2C_M_RD,
  900. .buf = b, .len = 1,
  901. };
  902. if (fe->ops.i2c_gate_ctrl)
  903. fe->ops.i2c_gate_ctrl(fe, 1);
  904. if (1 != i2c_transfer(i2c_adap, &msg, 1))
  905. printk(KERN_WARNING "tuner-simple %d-%04x: "
  906. "unable to probe %s, proceeding anyway.",
  907. i2c_adapter_id(i2c_adap), i2c_addr,
  908. tuners[type].name);
  909. if (fe->ops.i2c_gate_ctrl)
  910. fe->ops.i2c_gate_ctrl(fe, 0);
  911. }
  912. mutex_lock(&tuner_simple_list_mutex);
  913. instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
  914. hybrid_tuner_instance_list,
  915. i2c_adap, i2c_addr,
  916. "tuner-simple");
  917. switch (instance) {
  918. case 0:
  919. mutex_unlock(&tuner_simple_list_mutex);
  920. return NULL;
  921. case 1:
  922. fe->tuner_priv = priv;
  923. priv->type = type;
  924. priv->tun = &tuners[type];
  925. priv->nr = simple_devcount++;
  926. break;
  927. default:
  928. fe->tuner_priv = priv;
  929. break;
  930. }
  931. mutex_unlock(&tuner_simple_list_mutex);
  932. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  933. sizeof(struct dvb_tuner_ops));
  934. if (type != priv->type)
  935. tuner_warn("couldn't set type to %d. Using %d (%s) instead\n",
  936. type, priv->type, priv->tun->name);
  937. else
  938. tuner_info("type set to %d (%s)\n",
  939. priv->type, priv->tun->name);
  940. if ((debug) || ((atv_input[priv->nr] > 0) ||
  941. (dtv_input[priv->nr] > 0))) {
  942. if (0 == atv_input[priv->nr])
  943. tuner_info("tuner %d atv rf input will be "
  944. "autoselected\n", priv->nr);
  945. else
  946. tuner_info("tuner %d atv rf input will be "
  947. "set to input %d (insmod option)\n",
  948. priv->nr, atv_input[priv->nr]);
  949. if (0 == dtv_input[priv->nr])
  950. tuner_info("tuner %d dtv rf input will be "
  951. "autoselected\n", priv->nr);
  952. else
  953. tuner_info("tuner %d dtv rf input will be "
  954. "set to input %d (insmod option)\n",
  955. priv->nr, dtv_input[priv->nr]);
  956. }
  957. strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  958. sizeof(fe->ops.tuner_ops.info.name));
  959. return fe;
  960. }
  961. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  962. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  963. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  964. MODULE_LICENSE("GPL");