tuner-simple.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. u8 tuneraddr;
  265. int rc;
  266. /* tv norm specific stuff for multi-norm tuners */
  267. switch (priv->type) {
  268. case TUNER_PHILIPS_SECAM: /* FI1216MF */
  269. /* 0x01 -> ??? no change ??? */
  270. /* 0x02 -> PAL BDGHI / SECAM L */
  271. /* 0x04 -> ??? PAL others / SECAM others ??? */
  272. *cb &= ~0x03;
  273. if (params->std & V4L2_STD_SECAM_L)
  274. /* also valid for V4L2_STD_SECAM */
  275. *cb |= PHILIPS_MF_SET_STD_L;
  276. else if (params->std & V4L2_STD_SECAM_LC)
  277. *cb |= PHILIPS_MF_SET_STD_LC;
  278. else /* V4L2_STD_B|V4L2_STD_GH */
  279. *cb |= PHILIPS_MF_SET_STD_BG;
  280. break;
  281. case TUNER_TEMIC_4046FM5:
  282. *cb &= ~0x0f;
  283. if (params->std & V4L2_STD_PAL_BG) {
  284. *cb |= TEMIC_SET_PAL_BG;
  285. } else if (params->std & V4L2_STD_PAL_I) {
  286. *cb |= TEMIC_SET_PAL_I;
  287. } else if (params->std & V4L2_STD_PAL_DK) {
  288. *cb |= TEMIC_SET_PAL_DK;
  289. } else if (params->std & V4L2_STD_SECAM_L) {
  290. *cb |= TEMIC_SET_PAL_L;
  291. }
  292. break;
  293. case TUNER_PHILIPS_FQ1216ME:
  294. *cb &= ~0x0f;
  295. if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  296. *cb |= PHILIPS_SET_PAL_BGDK;
  297. } else if (params->std & V4L2_STD_PAL_I) {
  298. *cb |= PHILIPS_SET_PAL_I;
  299. } else if (params->std & V4L2_STD_SECAM_L) {
  300. *cb |= PHILIPS_SET_PAL_L;
  301. }
  302. break;
  303. case TUNER_PHILIPS_FCV1236D:
  304. /* 0x00 -> ATSC antenna input 1 */
  305. /* 0x01 -> ATSC antenna input 2 */
  306. /* 0x02 -> NTSC antenna input 1 */
  307. /* 0x03 -> NTSC antenna input 2 */
  308. *cb &= ~0x03;
  309. if (!(params->std & V4L2_STD_ATSC))
  310. *cb |= 2;
  311. break;
  312. case TUNER_MICROTUNE_4042FI5:
  313. /* Set the charge pump for fast tuning */
  314. *config |= TUNER_CHARGE_PUMP;
  315. break;
  316. case TUNER_PHILIPS_TUV1236D:
  317. {
  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. tuneraddr = priv->i2c_props.addr;
  330. priv->i2c_props.addr = 0x0a;
  331. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[0], 2);
  332. if (2 != rc)
  333. tuner_warn("i2c i/o error: rc == %d "
  334. "(should be 2)\n", rc);
  335. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[2], 2);
  336. if (2 != rc)
  337. tuner_warn("i2c i/o error: rc == %d "
  338. "(should be 2)\n", rc);
  339. priv->i2c_props.addr = tuneraddr;
  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_post_tune(struct dvb_frontend *fe, u8 *buffer,
  348. u16 div, u8 config, u8 cb)
  349. {
  350. struct tuner_simple_priv *priv = fe->tuner_priv;
  351. int rc;
  352. switch (priv->type) {
  353. case TUNER_LG_TDVS_H06XF:
  354. /* Set the Auxiliary Byte. */
  355. buffer[0] = buffer[2];
  356. buffer[0] &= ~0x20;
  357. buffer[0] |= 0x18;
  358. buffer[1] = 0x20;
  359. tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  360. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
  361. if (2 != rc)
  362. tuner_warn("i2c i/o error: rc == %d "
  363. "(should be 2)\n", rc);
  364. break;
  365. case TUNER_MICROTUNE_4042FI5:
  366. {
  367. /* FIXME - this may also work for other tuners */
  368. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  369. u8 status_byte = 0;
  370. /* Wait until the PLL locks */
  371. for (;;) {
  372. if (time_after(jiffies, timeout))
  373. return 0;
  374. rc = tuner_i2c_xfer_recv(&priv->i2c_props,
  375. &status_byte, 1);
  376. if (1 != rc) {
  377. tuner_warn("i2c i/o read error: rc == %d "
  378. "(should be 1)\n", rc);
  379. break;
  380. }
  381. if (status_byte & TUNER_PLL_LOCKED)
  382. break;
  383. udelay(10);
  384. }
  385. /* Set the charge pump for optimized phase noise figure */
  386. config &= ~TUNER_CHARGE_PUMP;
  387. buffer[0] = (div>>8) & 0x7f;
  388. buffer[1] = div & 0xff;
  389. buffer[2] = config;
  390. buffer[3] = cb;
  391. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  392. buffer[0], buffer[1], buffer[2], buffer[3]);
  393. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  394. if (4 != rc)
  395. tuner_warn("i2c i/o error: rc == %d "
  396. "(should be 4)\n", rc);
  397. break;
  398. }
  399. }
  400. return 0;
  401. }
  402. static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
  403. {
  404. struct tuner_simple_priv *priv = fe->tuner_priv;
  405. switch (priv->type) {
  406. case TUNER_TENA_9533_DI:
  407. case TUNER_YMEC_TVF_5533MF:
  408. tuner_dbg("This tuner doesn't have FM. "
  409. "Most cards have a TEA5767 for FM\n");
  410. return 0;
  411. case TUNER_PHILIPS_FM1216ME_MK3:
  412. case TUNER_PHILIPS_FM1236_MK3:
  413. case TUNER_PHILIPS_FMD1216ME_MK3:
  414. case TUNER_LG_NTSC_TAPE:
  415. case TUNER_PHILIPS_FM1256_IH3:
  416. case TUNER_TCL_MF02GIP_5N:
  417. buffer[3] = 0x19;
  418. break;
  419. case TUNER_TNF_5335MF:
  420. buffer[3] = 0x11;
  421. break;
  422. case TUNER_LG_PAL_FM:
  423. buffer[3] = 0xa5;
  424. break;
  425. case TUNER_THOMSON_DTT761X:
  426. buffer[3] = 0x39;
  427. break;
  428. case TUNER_MICROTUNE_4049FM5:
  429. default:
  430. buffer[3] = 0xa4;
  431. break;
  432. }
  433. return 0;
  434. }
  435. /* ---------------------------------------------------------------------- */
  436. static int simple_set_tv_freq(struct dvb_frontend *fe,
  437. struct analog_parameters *params)
  438. {
  439. struct tuner_simple_priv *priv = fe->tuner_priv;
  440. u8 config, cb;
  441. u16 div;
  442. struct tunertype *tun;
  443. u8 buffer[4];
  444. int rc, IFPCoff, i;
  445. enum param_type desired_type;
  446. struct tuner_params *t_params;
  447. tun = priv->tun;
  448. /* IFPCoff = Video Intermediate Frequency - Vif:
  449. 940 =16*58.75 NTSC/J (Japan)
  450. 732 =16*45.75 M/N STD
  451. 704 =16*44 ATSC (at DVB code)
  452. 632 =16*39.50 I U.K.
  453. 622.4=16*38.90 B/G D/K I, L STD
  454. 592 =16*37.00 D China
  455. 590 =16.36.875 B Australia
  456. 543.2=16*33.95 L' STD
  457. 171.2=16*10.70 FM Radio (at set_radio_freq)
  458. */
  459. if (params->std == V4L2_STD_NTSC_M_JP) {
  460. IFPCoff = 940;
  461. desired_type = TUNER_PARAM_TYPE_NTSC;
  462. } else if ((params->std & V4L2_STD_MN) &&
  463. !(params->std & ~V4L2_STD_MN)) {
  464. IFPCoff = 732;
  465. desired_type = TUNER_PARAM_TYPE_NTSC;
  466. } else if (params->std == V4L2_STD_SECAM_LC) {
  467. IFPCoff = 543;
  468. desired_type = TUNER_PARAM_TYPE_SECAM;
  469. } else {
  470. IFPCoff = 623;
  471. desired_type = TUNER_PARAM_TYPE_PAL;
  472. }
  473. t_params = simple_tuner_params(fe, desired_type);
  474. i = simple_config_lookup(fe, t_params, &params->frequency,
  475. &config, &cb);
  476. div = params->frequency + IFPCoff + offset;
  477. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
  478. "Offset=%d.%02d MHz, div=%0d\n",
  479. params->frequency / 16, params->frequency % 16 * 100 / 16,
  480. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  481. offset / 16, offset % 16 * 100 / 16, div);
  482. /* tv norm specific stuff for multi-norm tuners */
  483. simple_std_setup(fe, params, &config, &cb);
  484. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  485. buffer[0] = config;
  486. buffer[1] = cb;
  487. buffer[2] = (div>>8) & 0x7f;
  488. buffer[3] = div & 0xff;
  489. } else {
  490. buffer[0] = (div>>8) & 0x7f;
  491. buffer[1] = div & 0xff;
  492. buffer[2] = config;
  493. buffer[3] = cb;
  494. }
  495. priv->last_div = div;
  496. if (t_params->has_tda9887) {
  497. struct v4l2_priv_tun_config tda9887_cfg;
  498. int tda_config = 0;
  499. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  500. V4L2_STD_SECAM_LC)) &&
  501. !(params->std & ~(V4L2_STD_SECAM_L |
  502. V4L2_STD_SECAM_LC));
  503. tda9887_cfg.tuner = TUNER_TDA9887;
  504. tda9887_cfg.priv = &tda_config;
  505. if (params->std == V4L2_STD_SECAM_LC) {
  506. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  507. tda_config |= TDA9887_PORT1_ACTIVE;
  508. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  509. tda_config |= TDA9887_PORT2_ACTIVE;
  510. } else {
  511. if (t_params->port1_active)
  512. tda_config |= TDA9887_PORT1_ACTIVE;
  513. if (t_params->port2_active)
  514. tda_config |= TDA9887_PORT2_ACTIVE;
  515. }
  516. if (t_params->intercarrier_mode)
  517. tda_config |= TDA9887_INTERCARRIER;
  518. if (is_secam_l) {
  519. if (i == 0 && t_params->default_top_secam_low)
  520. tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
  521. else if (i == 1 && t_params->default_top_secam_mid)
  522. tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
  523. else if (t_params->default_top_secam_high)
  524. tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
  525. } else {
  526. if (i == 0 && t_params->default_top_low)
  527. tda_config |= TDA9887_TOP(t_params->default_top_low);
  528. else if (i == 1 && t_params->default_top_mid)
  529. tda_config |= TDA9887_TOP(t_params->default_top_mid);
  530. else if (t_params->default_top_high)
  531. tda_config |= TDA9887_TOP(t_params->default_top_high);
  532. }
  533. if (t_params->default_pll_gating_18)
  534. tda_config |= TDA9887_GATING_18;
  535. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  536. &tda9887_cfg);
  537. }
  538. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  539. buffer[0], buffer[1], buffer[2], buffer[3]);
  540. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  541. if (4 != rc)
  542. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  543. simple_post_tune(fe, &buffer[0], div, config, cb);
  544. return 0;
  545. }
  546. static int simple_set_radio_freq(struct dvb_frontend *fe,
  547. struct analog_parameters *params)
  548. {
  549. struct tunertype *tun;
  550. struct tuner_simple_priv *priv = fe->tuner_priv;
  551. u8 buffer[4];
  552. u16 div;
  553. int rc, j;
  554. struct tuner_params *t_params;
  555. unsigned int freq = params->frequency;
  556. tun = priv->tun;
  557. for (j = tun->count-1; j > 0; j--)
  558. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  559. break;
  560. /* default t_params (j=0) will be used if desired type wasn't found */
  561. t_params = &tun->params[j];
  562. /* Select Radio 1st IF used */
  563. switch (t_params->radio_if) {
  564. case 0: /* 10.7 MHz */
  565. freq += (unsigned int)(10.7*16000);
  566. break;
  567. case 1: /* 33.3 MHz */
  568. freq += (unsigned int)(33.3*16000);
  569. break;
  570. case 2: /* 41.3 MHz */
  571. freq += (unsigned int)(41.3*16000);
  572. break;
  573. default:
  574. tuner_warn("Unsupported radio_if value %d\n",
  575. t_params->radio_if);
  576. return 0;
  577. }
  578. /* Bandswitch byte */
  579. simple_radio_bandswitch(fe, &buffer[0]);
  580. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  581. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  582. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  583. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  584. freq * (1/800) */
  585. div = (freq + 400) / 800;
  586. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  587. buffer[0] = buffer[2];
  588. buffer[1] = buffer[3];
  589. buffer[2] = (div>>8) & 0x7f;
  590. buffer[3] = div & 0xff;
  591. } else {
  592. buffer[0] = (div>>8) & 0x7f;
  593. buffer[1] = div & 0xff;
  594. }
  595. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  596. buffer[0], buffer[1], buffer[2], buffer[3]);
  597. priv->last_div = div;
  598. if (t_params->has_tda9887) {
  599. int config = 0;
  600. struct v4l2_priv_tun_config tda9887_cfg;
  601. tda9887_cfg.tuner = TUNER_TDA9887;
  602. tda9887_cfg.priv = &config;
  603. if (t_params->port1_active &&
  604. !t_params->port1_fm_high_sensitivity)
  605. config |= TDA9887_PORT1_ACTIVE;
  606. if (t_params->port2_active &&
  607. !t_params->port2_fm_high_sensitivity)
  608. config |= TDA9887_PORT2_ACTIVE;
  609. if (t_params->intercarrier_mode)
  610. config |= TDA9887_INTERCARRIER;
  611. /* if (t_params->port1_set_for_fm_mono)
  612. config &= ~TDA9887_PORT1_ACTIVE;*/
  613. if (t_params->fm_gain_normal)
  614. config |= TDA9887_GAIN_NORMAL;
  615. if (t_params->radio_if == 2)
  616. config |= TDA9887_RIF_41_3;
  617. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  618. &tda9887_cfg);
  619. }
  620. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  621. if (4 != rc)
  622. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  623. return 0;
  624. }
  625. static int simple_set_params(struct dvb_frontend *fe,
  626. struct analog_parameters *params)
  627. {
  628. struct tuner_simple_priv *priv = fe->tuner_priv;
  629. int ret = -EINVAL;
  630. if (priv->i2c_props.adap == NULL)
  631. return -EINVAL;
  632. switch (params->mode) {
  633. case V4L2_TUNER_RADIO:
  634. ret = simple_set_radio_freq(fe, params);
  635. priv->frequency = params->frequency * 125 / 2;
  636. break;
  637. case V4L2_TUNER_ANALOG_TV:
  638. case V4L2_TUNER_DIGITAL_TV:
  639. ret = simple_set_tv_freq(fe, params);
  640. priv->frequency = params->frequency * 62500;
  641. break;
  642. }
  643. priv->bandwidth = 0;
  644. return ret;
  645. }
  646. static void simple_set_dvb(struct dvb_frontend *fe, u8 *buf,
  647. const struct dvb_frontend_parameters *params)
  648. {
  649. struct tuner_simple_priv *priv = fe->tuner_priv;
  650. switch (priv->type) {
  651. case TUNER_PHILIPS_FMD1216ME_MK3:
  652. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
  653. params->frequency >= 158870000)
  654. buf[3] |= 0x08;
  655. break;
  656. case TUNER_PHILIPS_TD1316:
  657. /* determine band */
  658. buf[3] |= (params->frequency < 161000000) ? 1 :
  659. (params->frequency < 444000000) ? 2 : 4;
  660. /* setup PLL filter */
  661. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  662. buf[3] |= 1 << 3;
  663. break;
  664. case TUNER_PHILIPS_TUV1236D:
  665. case TUNER_PHILIPS_FCV1236D:
  666. {
  667. unsigned int new_rf;
  668. if (dtv_input[priv->nr])
  669. new_rf = dtv_input[priv->nr];
  670. else
  671. switch (params->u.vsb.modulation) {
  672. case QAM_64:
  673. case QAM_256:
  674. new_rf = 1;
  675. break;
  676. case VSB_8:
  677. default:
  678. new_rf = 0;
  679. break;
  680. }
  681. simple_set_rf_input(fe, &buf[2], &buf[3], new_rf);
  682. break;
  683. }
  684. default:
  685. break;
  686. }
  687. }
  688. static u32 simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
  689. const struct dvb_frontend_parameters *params)
  690. {
  691. /* This function returns the tuned frequency on success, 0 on error */
  692. struct tuner_simple_priv *priv = fe->tuner_priv;
  693. struct tunertype *tun = priv->tun;
  694. static struct tuner_params *t_params;
  695. u8 config, cb;
  696. u32 div;
  697. int ret;
  698. unsigned frequency = params->frequency / 62500;
  699. t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
  700. ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
  701. if (ret < 0)
  702. return 0; /* failure */
  703. div = ((frequency + t_params->iffreq) * 62500 + offset +
  704. tun->stepsize/2) / tun->stepsize;
  705. buf[0] = div >> 8;
  706. buf[1] = div & 0xff;
  707. buf[2] = config;
  708. buf[3] = cb;
  709. simple_set_dvb(fe, buf, params);
  710. tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  711. tun->name, div, buf[0], buf[1], buf[2], buf[3]);
  712. /* calculate the frequency we set it to */
  713. return (div * tun->stepsize) - t_params->iffreq;
  714. }
  715. static int simple_dvb_calc_regs(struct dvb_frontend *fe,
  716. struct dvb_frontend_parameters *params,
  717. u8 *buf, int buf_len)
  718. {
  719. struct tuner_simple_priv *priv = fe->tuner_priv;
  720. u32 frequency;
  721. if (buf_len < 5)
  722. return -EINVAL;
  723. frequency = simple_dvb_configure(fe, buf+1, params);
  724. if (frequency == 0)
  725. return -EINVAL;
  726. buf[0] = priv->i2c_props.addr;
  727. priv->frequency = frequency;
  728. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
  729. params->u.ofdm.bandwidth : 0;
  730. return 5;
  731. }
  732. static int simple_dvb_set_params(struct dvb_frontend *fe,
  733. struct dvb_frontend_parameters *params)
  734. {
  735. struct tuner_simple_priv *priv = fe->tuner_priv;
  736. u32 prev_freq, prev_bw;
  737. int ret;
  738. u8 buf[5];
  739. if (priv->i2c_props.adap == NULL)
  740. return -EINVAL;
  741. prev_freq = priv->frequency;
  742. prev_bw = priv->bandwidth;
  743. ret = simple_dvb_calc_regs(fe, params, buf, 5);
  744. if (ret != 5)
  745. goto fail;
  746. /* put analog demod in standby when tuning digital */
  747. if (fe->ops.analog_ops.standby)
  748. fe->ops.analog_ops.standby(fe);
  749. if (fe->ops.i2c_gate_ctrl)
  750. fe->ops.i2c_gate_ctrl(fe, 1);
  751. /* buf[0] contains the i2c address, but *
  752. * we already have it in i2c_props.addr */
  753. ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
  754. if (ret != 4)
  755. goto fail;
  756. return 0;
  757. fail:
  758. /* calc_regs sets frequency and bandwidth. if we failed, unset them */
  759. priv->frequency = prev_freq;
  760. priv->bandwidth = prev_bw;
  761. return ret;
  762. }
  763. static int simple_init(struct dvb_frontend *fe)
  764. {
  765. struct tuner_simple_priv *priv = fe->tuner_priv;
  766. if (priv->i2c_props.adap == NULL)
  767. return -EINVAL;
  768. if (priv->tun->initdata) {
  769. int ret;
  770. if (fe->ops.i2c_gate_ctrl)
  771. fe->ops.i2c_gate_ctrl(fe, 1);
  772. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  773. priv->tun->initdata + 1,
  774. priv->tun->initdata[0]);
  775. if (ret != priv->tun->initdata[0])
  776. return ret;
  777. }
  778. return 0;
  779. }
  780. static int simple_sleep(struct dvb_frontend *fe)
  781. {
  782. struct tuner_simple_priv *priv = fe->tuner_priv;
  783. if (priv->i2c_props.adap == NULL)
  784. return -EINVAL;
  785. if (priv->tun->sleepdata) {
  786. int ret;
  787. if (fe->ops.i2c_gate_ctrl)
  788. fe->ops.i2c_gate_ctrl(fe, 1);
  789. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  790. priv->tun->sleepdata + 1,
  791. priv->tun->sleepdata[0]);
  792. if (ret != priv->tun->sleepdata[0])
  793. return ret;
  794. }
  795. return 0;
  796. }
  797. static int simple_release(struct dvb_frontend *fe)
  798. {
  799. struct tuner_simple_priv *priv = fe->tuner_priv;
  800. mutex_lock(&tuner_simple_list_mutex);
  801. if (priv)
  802. hybrid_tuner_release_state(priv);
  803. mutex_unlock(&tuner_simple_list_mutex);
  804. fe->tuner_priv = NULL;
  805. return 0;
  806. }
  807. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  808. {
  809. struct tuner_simple_priv *priv = fe->tuner_priv;
  810. *frequency = priv->frequency;
  811. return 0;
  812. }
  813. static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  814. {
  815. struct tuner_simple_priv *priv = fe->tuner_priv;
  816. *bandwidth = priv->bandwidth;
  817. return 0;
  818. }
  819. static struct dvb_tuner_ops simple_tuner_ops = {
  820. .init = simple_init,
  821. .sleep = simple_sleep,
  822. .set_analog_params = simple_set_params,
  823. .set_params = simple_dvb_set_params,
  824. .calc_regs = simple_dvb_calc_regs,
  825. .release = simple_release,
  826. .get_frequency = simple_get_frequency,
  827. .get_bandwidth = simple_get_bandwidth,
  828. .get_status = simple_get_status,
  829. .get_rf_strength = simple_get_rf_strength,
  830. };
  831. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  832. struct i2c_adapter *i2c_adap,
  833. u8 i2c_addr,
  834. unsigned int type)
  835. {
  836. struct tuner_simple_priv *priv = NULL;
  837. int instance;
  838. if (type >= tuner_count) {
  839. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  840. __func__, type, tuner_count-1);
  841. return NULL;
  842. }
  843. /* If i2c_adap is set, check that the tuner is at the correct address.
  844. * Otherwise, if i2c_adap is NULL, the tuner will be programmed directly
  845. * by the digital demod via calc_regs.
  846. */
  847. if (i2c_adap != NULL) {
  848. u8 b[1];
  849. struct i2c_msg msg = {
  850. .addr = i2c_addr, .flags = I2C_M_RD,
  851. .buf = b, .len = 1,
  852. };
  853. if (fe->ops.i2c_gate_ctrl)
  854. fe->ops.i2c_gate_ctrl(fe, 1);
  855. if (1 != i2c_transfer(i2c_adap, &msg, 1))
  856. printk(KERN_WARNING "tuner-simple %d-%04x: "
  857. "unable to probe %s, proceeding anyway.",
  858. i2c_adapter_id(i2c_adap), i2c_addr,
  859. tuners[type].name);
  860. if (fe->ops.i2c_gate_ctrl)
  861. fe->ops.i2c_gate_ctrl(fe, 0);
  862. }
  863. mutex_lock(&tuner_simple_list_mutex);
  864. instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
  865. hybrid_tuner_instance_list,
  866. i2c_adap, i2c_addr,
  867. "tuner-simple");
  868. switch (instance) {
  869. case 0:
  870. mutex_unlock(&tuner_simple_list_mutex);
  871. return NULL;
  872. case 1:
  873. fe->tuner_priv = priv;
  874. priv->type = type;
  875. priv->tun = &tuners[type];
  876. priv->nr = simple_devcount++;
  877. break;
  878. default:
  879. fe->tuner_priv = priv;
  880. break;
  881. }
  882. mutex_unlock(&tuner_simple_list_mutex);
  883. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  884. sizeof(struct dvb_tuner_ops));
  885. tuner_info("type set to %d (%s)\n", type, priv->tun->name);
  886. if ((debug) || ((atv_input[priv->nr] > 0) ||
  887. (dtv_input[priv->nr] > 0))) {
  888. if (0 == atv_input[priv->nr])
  889. tuner_info("tuner %d atv rf input will be "
  890. "autoselected\n", priv->nr);
  891. else
  892. tuner_info("tuner %d atv rf input will be "
  893. "set to input %d (insmod option)\n",
  894. priv->nr, atv_input[priv->nr]);
  895. if (0 == dtv_input[priv->nr])
  896. tuner_info("tuner %d dtv rf input will be "
  897. "autoselected\n", priv->nr);
  898. else
  899. tuner_info("tuner %d dtv rf input will be "
  900. "set to input %d (insmod option)\n",
  901. priv->nr, dtv_input[priv->nr]);
  902. }
  903. strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  904. sizeof(fe->ops.tuner_ops.info.name));
  905. return fe;
  906. }
  907. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  908. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  909. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  910. MODULE_LICENSE("GPL");
  911. /*
  912. * Overrides for Emacs so that we follow Linus's tabbing style.
  913. * ---------------------------------------------------------------------------
  914. * Local variables:
  915. * c-basic-offset: 8
  916. * End:
  917. */