tuner-simple.c 28 KB

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