tuner-simple.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * i2c tv tuner chip device driver
  4. * controls all those simple 4-control-bytes style tuners.
  5. *
  6. * This "tuner-simple" module was split apart from the original "tuner" module.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/i2c.h>
  10. #include <linux/videodev2.h>
  11. #include <media/tuner.h>
  12. #include <media/v4l2-common.h>
  13. #include <media/tuner-types.h>
  14. #include "tuner-i2c.h"
  15. #include "tuner-simple.h"
  16. static int debug;
  17. module_param(debug, int, 0644);
  18. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  19. #define TUNER_SIMPLE_MAX 64
  20. static unsigned int simple_devcount;
  21. static int offset;
  22. module_param(offset, int, 0664);
  23. MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
  24. static unsigned int atv_input[TUNER_SIMPLE_MAX] = \
  25. { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
  26. static unsigned int dtv_input[TUNER_SIMPLE_MAX] = \
  27. { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
  28. module_param_array(atv_input, int, NULL, 0644);
  29. module_param_array(dtv_input, int, NULL, 0644);
  30. MODULE_PARM_DESC(atv_input, "specify atv rf input, 0 for autoselect");
  31. MODULE_PARM_DESC(dtv_input, "specify dtv rf input, 0 for autoselect");
  32. /* ---------------------------------------------------------------------- */
  33. /* tv standard selection for Temic 4046 FM5
  34. this value takes the low bits of control byte 2
  35. from datasheet Rev.01, Feb.00
  36. standard BG I L L2 D
  37. picture IF 38.9 38.9 38.9 33.95 38.9
  38. sound 1 33.4 32.9 32.4 40.45 32.4
  39. sound 2 33.16
  40. NICAM 33.05 32.348 33.05 33.05
  41. */
  42. #define TEMIC_SET_PAL_I 0x05
  43. #define TEMIC_SET_PAL_DK 0x09
  44. #define TEMIC_SET_PAL_L 0x0a /* SECAM ? */
  45. #define TEMIC_SET_PAL_L2 0x0b /* change IF ! */
  46. #define TEMIC_SET_PAL_BG 0x0c
  47. /* tv tuner system standard selection for Philips FQ1216ME
  48. this value takes the low bits of control byte 2
  49. from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
  50. standard BG DK I L L`
  51. picture carrier 38.90 38.90 38.90 38.90 33.95
  52. colour 34.47 34.47 34.47 34.47 38.38
  53. sound 1 33.40 32.40 32.90 32.40 40.45
  54. sound 2 33.16 - - - -
  55. NICAM 33.05 33.05 32.35 33.05 39.80
  56. */
  57. #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
  58. #define PHILIPS_SET_PAL_BGDK 0x09
  59. #define PHILIPS_SET_PAL_L2 0x0a
  60. #define PHILIPS_SET_PAL_L 0x0b
  61. /* system switching for Philips FI1216MF MK2
  62. from datasheet "1996 Jul 09",
  63. standard BG L L'
  64. picture carrier 38.90 38.90 33.95
  65. colour 34.47 34.37 38.38
  66. sound 1 33.40 32.40 40.45
  67. sound 2 33.16 - -
  68. NICAM 33.05 33.05 39.80
  69. */
  70. #define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
  71. #define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
  72. #define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
  73. /* Control byte */
  74. #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
  75. #define TUNER_RATIO_SELECT_50 0x00
  76. #define TUNER_RATIO_SELECT_32 0x02
  77. #define TUNER_RATIO_SELECT_166 0x04
  78. #define TUNER_RATIO_SELECT_62 0x06
  79. #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
  80. /* Status byte */
  81. #define TUNER_POR 0x80
  82. #define TUNER_FL 0x40
  83. #define TUNER_MODE 0x38
  84. #define TUNER_AFC 0x07
  85. #define TUNER_SIGNAL 0x07
  86. #define TUNER_STEREO 0x10
  87. #define TUNER_PLL_LOCKED 0x40
  88. #define TUNER_STEREO_MK3 0x04
  89. static DEFINE_MUTEX(tuner_simple_list_mutex);
  90. static LIST_HEAD(hybrid_tuner_instance_list);
  91. struct tuner_simple_priv {
  92. unsigned int nr;
  93. u16 last_div;
  94. struct tuner_i2c_props i2c_props;
  95. struct list_head hybrid_tuner_instance_list;
  96. unsigned int type;
  97. struct tunertype *tun;
  98. u32 frequency;
  99. u32 bandwidth;
  100. bool radio_mode;
  101. };
  102. /* ---------------------------------------------------------------------- */
  103. static int tuner_read_status(struct dvb_frontend *fe)
  104. {
  105. struct tuner_simple_priv *priv = fe->tuner_priv;
  106. unsigned char byte;
  107. if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
  108. return 0;
  109. return byte;
  110. }
  111. static inline int tuner_signal(const int status)
  112. {
  113. return (status & TUNER_SIGNAL) << 13;
  114. }
  115. static inline int tuner_stereo(const int type, const int status)
  116. {
  117. switch (type) {
  118. case TUNER_PHILIPS_FM1216ME_MK3:
  119. case TUNER_PHILIPS_FM1236_MK3:
  120. case TUNER_PHILIPS_FM1256_IH3:
  121. case TUNER_LG_NTSC_TAPE:
  122. case TUNER_TCL_MF02GIP_5N:
  123. return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  124. case TUNER_PHILIPS_FM1216MK5:
  125. return status | TUNER_STEREO;
  126. default:
  127. return status & TUNER_STEREO;
  128. }
  129. }
  130. static inline int tuner_islocked(const int status)
  131. {
  132. return (status & TUNER_FL);
  133. }
  134. static inline int tuner_afcstatus(const int status)
  135. {
  136. return (status & TUNER_AFC) - 2;
  137. }
  138. static int simple_get_status(struct dvb_frontend *fe, u32 *status)
  139. {
  140. struct tuner_simple_priv *priv = fe->tuner_priv;
  141. int tuner_status;
  142. if (priv->i2c_props.adap == NULL)
  143. return -EINVAL;
  144. tuner_status = tuner_read_status(fe);
  145. *status = 0;
  146. if (tuner_islocked(tuner_status))
  147. *status = TUNER_STATUS_LOCKED;
  148. if (tuner_stereo(priv->type, tuner_status))
  149. *status |= TUNER_STATUS_STEREO;
  150. tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
  151. return 0;
  152. }
  153. static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  154. {
  155. struct tuner_simple_priv *priv = fe->tuner_priv;
  156. int signal;
  157. if (priv->i2c_props.adap == NULL || !priv->radio_mode)
  158. return -EINVAL;
  159. signal = tuner_signal(tuner_read_status(fe));
  160. *strength = signal;
  161. tuner_dbg("Signal strength: %d\n", signal);
  162. return 0;
  163. }
  164. /* ---------------------------------------------------------------------- */
  165. static inline char *tuner_param_name(enum param_type type)
  166. {
  167. char *name;
  168. switch (type) {
  169. case TUNER_PARAM_TYPE_RADIO:
  170. name = "radio";
  171. break;
  172. case TUNER_PARAM_TYPE_PAL:
  173. name = "pal";
  174. break;
  175. case TUNER_PARAM_TYPE_SECAM:
  176. name = "secam";
  177. break;
  178. case TUNER_PARAM_TYPE_NTSC:
  179. name = "ntsc";
  180. break;
  181. case TUNER_PARAM_TYPE_DIGITAL:
  182. name = "digital";
  183. break;
  184. default:
  185. name = "unknown";
  186. break;
  187. }
  188. return name;
  189. }
  190. static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
  191. enum param_type desired_type)
  192. {
  193. struct tuner_simple_priv *priv = fe->tuner_priv;
  194. struct tunertype *tun = priv->tun;
  195. int i;
  196. for (i = 0; i < tun->count; i++)
  197. if (desired_type == tun->params[i].type)
  198. break;
  199. /* use default tuner params if desired_type not available */
  200. if (i == tun->count) {
  201. tuner_dbg("desired params (%s) undefined for tuner %d\n",
  202. tuner_param_name(desired_type), priv->type);
  203. i = 0;
  204. }
  205. tuner_dbg("using tuner params #%d (%s)\n", i,
  206. tuner_param_name(tun->params[i].type));
  207. return &tun->params[i];
  208. }
  209. static int simple_config_lookup(struct dvb_frontend *fe,
  210. struct tuner_params *t_params,
  211. unsigned *frequency, u8 *config, u8 *cb)
  212. {
  213. struct tuner_simple_priv *priv = fe->tuner_priv;
  214. int i;
  215. for (i = 0; i < t_params->count; i++) {
  216. if (*frequency > t_params->ranges[i].limit)
  217. continue;
  218. break;
  219. }
  220. if (i == t_params->count) {
  221. tuner_dbg("frequency out of range (%d > %d)\n",
  222. *frequency, t_params->ranges[i - 1].limit);
  223. *frequency = t_params->ranges[--i].limit;
  224. }
  225. *config = t_params->ranges[i].config;
  226. *cb = t_params->ranges[i].cb;
  227. tuner_dbg("freq = %d.%02d (%d), range = %d, 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 (should be 2)\n",
  336. rc);
  337. rc = tuner_i2c_xfer_send(&i2c, &buffer[2], 2);
  338. if (2 != rc)
  339. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",
  340. 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 (should be 1)\n",
  386. 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 (should be 4)\n",
  404. 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. Most cards have a TEA5767 for FM\n");
  417. return -EINVAL;
  418. case TUNER_PHILIPS_FM1216ME_MK3:
  419. case TUNER_PHILIPS_FM1236_MK3:
  420. case TUNER_PHILIPS_FMD1216ME_MK3:
  421. case TUNER_PHILIPS_FMD1216MEX_MK3:
  422. case TUNER_LG_NTSC_TAPE:
  423. case TUNER_PHILIPS_FM1256_IH3:
  424. case TUNER_TCL_MF02GIP_5N:
  425. buffer[3] = 0x19;
  426. break;
  427. case TUNER_PHILIPS_FM1216MK5:
  428. buffer[2] = 0x88;
  429. buffer[3] = 0x09;
  430. break;
  431. case TUNER_TNF_5335MF:
  432. buffer[3] = 0x11;
  433. break;
  434. case TUNER_LG_PAL_FM:
  435. buffer[3] = 0xa5;
  436. break;
  437. case TUNER_THOMSON_DTT761X:
  438. buffer[3] = 0x39;
  439. break;
  440. case TUNER_PHILIPS_FQ1216LME_MK3:
  441. case TUNER_PHILIPS_FQ1236_MK5:
  442. tuner_err("This tuner doesn't have FM\n");
  443. /* Set the low band for sanity, since it covers 88-108 MHz */
  444. buffer[3] = 0x01;
  445. break;
  446. case TUNER_MICROTUNE_4049FM5:
  447. default:
  448. buffer[3] = 0xa4;
  449. break;
  450. }
  451. return 0;
  452. }
  453. /* ---------------------------------------------------------------------- */
  454. static int simple_set_tv_freq(struct dvb_frontend *fe,
  455. struct analog_parameters *params)
  456. {
  457. struct tuner_simple_priv *priv = fe->tuner_priv;
  458. u8 config, cb;
  459. u16 div;
  460. u8 buffer[4];
  461. int rc, IFPCoff, i;
  462. enum param_type desired_type;
  463. struct tuner_params *t_params;
  464. /* IFPCoff = Video Intermediate Frequency - Vif:
  465. 940 =16*58.75 NTSC/J (Japan)
  466. 732 =16*45.75 M/N STD
  467. 704 =16*44 ATSC (at DVB code)
  468. 632 =16*39.50 I U.K.
  469. 622.4=16*38.90 B/G D/K I, L STD
  470. 592 =16*37.00 D China
  471. 590 =16.36.875 B Australia
  472. 543.2=16*33.95 L' STD
  473. 171.2=16*10.70 FM Radio (at set_radio_freq)
  474. */
  475. if (params->std == V4L2_STD_NTSC_M_JP) {
  476. IFPCoff = 940;
  477. desired_type = TUNER_PARAM_TYPE_NTSC;
  478. } else if ((params->std & V4L2_STD_MN) &&
  479. !(params->std & ~V4L2_STD_MN)) {
  480. IFPCoff = 732;
  481. desired_type = TUNER_PARAM_TYPE_NTSC;
  482. } else if (params->std == V4L2_STD_SECAM_LC) {
  483. IFPCoff = 543;
  484. desired_type = TUNER_PARAM_TYPE_SECAM;
  485. } else {
  486. IFPCoff = 623;
  487. desired_type = TUNER_PARAM_TYPE_PAL;
  488. }
  489. t_params = simple_tuner_params(fe, desired_type);
  490. i = simple_config_lookup(fe, t_params, &params->frequency,
  491. &config, &cb);
  492. div = params->frequency + IFPCoff + offset;
  493. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
  494. params->frequency / 16, params->frequency % 16 * 100 / 16,
  495. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  496. offset / 16, offset % 16 * 100 / 16, div);
  497. /* tv norm specific stuff for multi-norm tuners */
  498. simple_std_setup(fe, params, &config, &cb);
  499. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  500. buffer[0] = config;
  501. buffer[1] = cb;
  502. buffer[2] = (div>>8) & 0x7f;
  503. buffer[3] = div & 0xff;
  504. } else {
  505. buffer[0] = (div>>8) & 0x7f;
  506. buffer[1] = div & 0xff;
  507. buffer[2] = config;
  508. buffer[3] = cb;
  509. }
  510. priv->last_div = div;
  511. if (t_params->has_tda9887) {
  512. struct v4l2_priv_tun_config tda9887_cfg;
  513. int tda_config = 0;
  514. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  515. V4L2_STD_SECAM_LC)) &&
  516. !(params->std & ~(V4L2_STD_SECAM_L |
  517. V4L2_STD_SECAM_LC));
  518. tda9887_cfg.tuner = TUNER_TDA9887;
  519. tda9887_cfg.priv = &tda_config;
  520. if (params->std == V4L2_STD_SECAM_LC) {
  521. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  522. tda_config |= TDA9887_PORT1_ACTIVE;
  523. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  524. tda_config |= TDA9887_PORT2_ACTIVE;
  525. } else {
  526. if (t_params->port1_active)
  527. tda_config |= TDA9887_PORT1_ACTIVE;
  528. if (t_params->port2_active)
  529. tda_config |= TDA9887_PORT2_ACTIVE;
  530. }
  531. if (t_params->intercarrier_mode)
  532. tda_config |= TDA9887_INTERCARRIER;
  533. if (is_secam_l) {
  534. if (i == 0 && t_params->default_top_secam_low)
  535. tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
  536. else if (i == 1 && t_params->default_top_secam_mid)
  537. tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
  538. else if (t_params->default_top_secam_high)
  539. tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
  540. } else {
  541. if (i == 0 && t_params->default_top_low)
  542. tda_config |= TDA9887_TOP(t_params->default_top_low);
  543. else if (i == 1 && t_params->default_top_mid)
  544. tda_config |= TDA9887_TOP(t_params->default_top_mid);
  545. else if (t_params->default_top_high)
  546. tda_config |= TDA9887_TOP(t_params->default_top_high);
  547. }
  548. if (t_params->default_pll_gating_18)
  549. tda_config |= TDA9887_GATING_18;
  550. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  551. &tda9887_cfg);
  552. }
  553. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  554. buffer[0], buffer[1], buffer[2], buffer[3]);
  555. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  556. if (4 != rc)
  557. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  558. simple_post_tune(fe, &buffer[0], div, config, cb);
  559. return 0;
  560. }
  561. static int simple_set_radio_freq(struct dvb_frontend *fe,
  562. struct analog_parameters *params)
  563. {
  564. struct tunertype *tun;
  565. struct tuner_simple_priv *priv = fe->tuner_priv;
  566. u8 buffer[4];
  567. u16 div;
  568. int rc, j;
  569. struct tuner_params *t_params;
  570. unsigned int freq = params->frequency;
  571. bool mono = params->audmode == V4L2_TUNER_MODE_MONO;
  572. tun = priv->tun;
  573. for (j = tun->count-1; j > 0; j--)
  574. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  575. break;
  576. /* default t_params (j=0) will be used if desired type wasn't found */
  577. t_params = &tun->params[j];
  578. /* Select Radio 1st IF used */
  579. switch (t_params->radio_if) {
  580. case 0: /* 10.7 MHz */
  581. freq += (unsigned int)(10.7*16000);
  582. break;
  583. case 1: /* 33.3 MHz */
  584. freq += (unsigned int)(33.3*16000);
  585. break;
  586. case 2: /* 41.3 MHz */
  587. freq += (unsigned int)(41.3*16000);
  588. break;
  589. default:
  590. tuner_warn("Unsupported radio_if value %d\n",
  591. t_params->radio_if);
  592. return 0;
  593. }
  594. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  595. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  596. /* Bandswitch byte */
  597. if (simple_radio_bandswitch(fe, &buffer[0]))
  598. return 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 && 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. 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 without stepsize defined.\n",
  736. priv->type, priv->tun->name);
  737. return 0; /* failure */
  738. }
  739. t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
  740. ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
  741. if (ret < 0)
  742. return 0; /* failure */
  743. div = ((frequency + t_params->iffreq) * 62500 + offset +
  744. tun->stepsize/2) / tun->stepsize;
  745. buf[0] = div >> 8;
  746. buf[1] = div & 0xff;
  747. buf[2] = config;
  748. buf[3] = cb;
  749. simple_set_dvb(fe, buf, delsys, freq, bw);
  750. tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  751. tun->name, div, buf[0], buf[1], buf[2], buf[3]);
  752. /* calculate the frequency we set it to */
  753. return (div * tun->stepsize) - t_params->iffreq;
  754. }
  755. static int simple_dvb_calc_regs(struct dvb_frontend *fe,
  756. u8 *buf, int buf_len)
  757. {
  758. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  759. u32 delsys = c->delivery_system;
  760. u32 bw = c->bandwidth_hz;
  761. struct tuner_simple_priv *priv = fe->tuner_priv;
  762. u32 frequency;
  763. if (buf_len < 5)
  764. return -EINVAL;
  765. frequency = simple_dvb_configure(fe, buf+1, delsys, c->frequency, bw);
  766. if (frequency == 0)
  767. return -EINVAL;
  768. buf[0] = priv->i2c_props.addr;
  769. priv->frequency = frequency;
  770. priv->bandwidth = c->bandwidth_hz;
  771. return 5;
  772. }
  773. static int simple_dvb_set_params(struct dvb_frontend *fe)
  774. {
  775. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  776. u32 delsys = c->delivery_system;
  777. u32 bw = c->bandwidth_hz;
  778. u32 freq = c->frequency;
  779. struct tuner_simple_priv *priv = fe->tuner_priv;
  780. u32 frequency;
  781. u32 prev_freq, prev_bw;
  782. int ret;
  783. u8 buf[5];
  784. if (priv->i2c_props.adap == NULL)
  785. return -EINVAL;
  786. prev_freq = priv->frequency;
  787. prev_bw = priv->bandwidth;
  788. frequency = simple_dvb_configure(fe, buf+1, delsys, freq, bw);
  789. if (frequency == 0)
  790. return -EINVAL;
  791. buf[0] = priv->i2c_props.addr;
  792. priv->frequency = frequency;
  793. priv->bandwidth = bw;
  794. /* put analog demod in standby when tuning digital */
  795. if (fe->ops.analog_ops.standby)
  796. fe->ops.analog_ops.standby(fe);
  797. if (fe->ops.i2c_gate_ctrl)
  798. fe->ops.i2c_gate_ctrl(fe, 1);
  799. /* buf[0] contains the i2c address, but *
  800. * we already have it in i2c_props.addr */
  801. ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
  802. if (ret != 4)
  803. goto fail;
  804. return 0;
  805. fail:
  806. /* calc_regs sets frequency and bandwidth. if we failed, unset them */
  807. priv->frequency = prev_freq;
  808. priv->bandwidth = prev_bw;
  809. return ret;
  810. }
  811. static int simple_init(struct dvb_frontend *fe)
  812. {
  813. struct tuner_simple_priv *priv = fe->tuner_priv;
  814. if (priv->i2c_props.adap == NULL)
  815. return -EINVAL;
  816. if (priv->tun->initdata) {
  817. int ret;
  818. if (fe->ops.i2c_gate_ctrl)
  819. fe->ops.i2c_gate_ctrl(fe, 1);
  820. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  821. priv->tun->initdata + 1,
  822. priv->tun->initdata[0]);
  823. if (ret != priv->tun->initdata[0])
  824. return ret;
  825. }
  826. return 0;
  827. }
  828. static int simple_sleep(struct dvb_frontend *fe)
  829. {
  830. struct tuner_simple_priv *priv = fe->tuner_priv;
  831. if (priv->i2c_props.adap == NULL)
  832. return -EINVAL;
  833. if (priv->tun->sleepdata) {
  834. int ret;
  835. if (fe->ops.i2c_gate_ctrl)
  836. fe->ops.i2c_gate_ctrl(fe, 1);
  837. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  838. priv->tun->sleepdata + 1,
  839. priv->tun->sleepdata[0]);
  840. if (ret != priv->tun->sleepdata[0])
  841. return ret;
  842. }
  843. return 0;
  844. }
  845. static void simple_release(struct dvb_frontend *fe)
  846. {
  847. struct tuner_simple_priv *priv = fe->tuner_priv;
  848. mutex_lock(&tuner_simple_list_mutex);
  849. if (priv)
  850. hybrid_tuner_release_state(priv);
  851. mutex_unlock(&tuner_simple_list_mutex);
  852. fe->tuner_priv = NULL;
  853. }
  854. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  855. {
  856. struct tuner_simple_priv *priv = fe->tuner_priv;
  857. *frequency = priv->frequency;
  858. return 0;
  859. }
  860. static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  861. {
  862. struct tuner_simple_priv *priv = fe->tuner_priv;
  863. *bandwidth = priv->bandwidth;
  864. return 0;
  865. }
  866. static const struct dvb_tuner_ops simple_tuner_ops = {
  867. .init = simple_init,
  868. .sleep = simple_sleep,
  869. .set_analog_params = simple_set_params,
  870. .set_params = simple_dvb_set_params,
  871. .calc_regs = simple_dvb_calc_regs,
  872. .release = simple_release,
  873. .get_frequency = simple_get_frequency,
  874. .get_bandwidth = simple_get_bandwidth,
  875. .get_status = simple_get_status,
  876. .get_rf_strength = simple_get_rf_strength,
  877. };
  878. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  879. struct i2c_adapter *i2c_adap,
  880. u8 i2c_addr,
  881. unsigned int type)
  882. {
  883. struct tuner_simple_priv *priv = NULL;
  884. int instance;
  885. if (type >= tuner_count) {
  886. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  887. __func__, type, tuner_count-1);
  888. return NULL;
  889. }
  890. /* If i2c_adap is set, check that the tuner is at the correct address.
  891. * Otherwise, if i2c_adap is NULL, the tuner will be programmed directly
  892. * by the digital demod via calc_regs.
  893. */
  894. if (i2c_adap != NULL) {
  895. u8 b[1];
  896. struct i2c_msg msg = {
  897. .addr = i2c_addr, .flags = I2C_M_RD,
  898. .buf = b, .len = 1,
  899. };
  900. if (fe->ops.i2c_gate_ctrl)
  901. fe->ops.i2c_gate_ctrl(fe, 1);
  902. if (1 != i2c_transfer(i2c_adap, &msg, 1))
  903. printk(KERN_WARNING "tuner-simple %d-%04x: unable to probe %s, proceeding anyway.",
  904. i2c_adapter_id(i2c_adap), i2c_addr,
  905. tuners[type].name);
  906. if (fe->ops.i2c_gate_ctrl)
  907. fe->ops.i2c_gate_ctrl(fe, 0);
  908. }
  909. mutex_lock(&tuner_simple_list_mutex);
  910. instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
  911. hybrid_tuner_instance_list,
  912. i2c_adap, i2c_addr,
  913. "tuner-simple");
  914. switch (instance) {
  915. case 0:
  916. mutex_unlock(&tuner_simple_list_mutex);
  917. return NULL;
  918. case 1:
  919. fe->tuner_priv = priv;
  920. priv->type = type;
  921. priv->tun = &tuners[type];
  922. priv->nr = simple_devcount++;
  923. break;
  924. default:
  925. fe->tuner_priv = priv;
  926. break;
  927. }
  928. mutex_unlock(&tuner_simple_list_mutex);
  929. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  930. sizeof(struct dvb_tuner_ops));
  931. if (type != priv->type)
  932. tuner_warn("couldn't set type to %d. Using %d (%s) instead\n",
  933. type, priv->type, priv->tun->name);
  934. else
  935. tuner_info("type set to %d (%s)\n",
  936. priv->type, priv->tun->name);
  937. if ((debug) || ((atv_input[priv->nr] > 0) ||
  938. (dtv_input[priv->nr] > 0))) {
  939. if (0 == atv_input[priv->nr])
  940. tuner_info("tuner %d atv rf input will be autoselected\n",
  941. priv->nr);
  942. else
  943. tuner_info("tuner %d atv rf input will be set to input %d (insmod option)\n",
  944. priv->nr, atv_input[priv->nr]);
  945. if (0 == dtv_input[priv->nr])
  946. tuner_info("tuner %d dtv rf input will be autoselected\n",
  947. priv->nr);
  948. else
  949. tuner_info("tuner %d dtv rf input will be set to input %d (insmod option)\n",
  950. priv->nr, dtv_input[priv->nr]);
  951. }
  952. strscpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  953. sizeof(fe->ops.tuner_ops.info.name));
  954. return fe;
  955. }
  956. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  957. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  958. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  959. MODULE_LICENSE("GPL");