tuner-simple.c 29 KB

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