tuner-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * $Id: tuner-core.c,v 1.29 2005/06/21 15:40:33 mchehab Exp $
  3. *
  4. * i2c tv tuner chip device driver
  5. * core core, i.e. kernel interfaces, registering and so on
  6. */
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/string.h>
  12. #include <linux/timer.h>
  13. #include <linux/delay.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/poll.h>
  17. #include <linux/i2c.h>
  18. #include <linux/types.h>
  19. #include <linux/videodev.h>
  20. #include <linux/init.h>
  21. #include <media/tuner.h>
  22. #include <media/audiochip.h>
  23. /*
  24. * comment line bellow to return to old behavor, where only one I2C device is supported
  25. */
  26. #define UNSET (-1U)
  27. /* standard i2c insmod options */
  28. static unsigned short normal_i2c[] = {
  29. 0x4b, /* tda8290 */
  30. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  31. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  32. I2C_CLIENT_END
  33. };
  34. I2C_CLIENT_INSMOD;
  35. /* insmod options used at init time => read/only */
  36. static unsigned int addr = 0;
  37. module_param(addr, int, 0444);
  38. /* insmod options used at runtime => read/write */
  39. unsigned int tuner_debug = 0;
  40. module_param(tuner_debug, int, 0644);
  41. static unsigned int tv_range[2] = { 44, 958 };
  42. static unsigned int radio_range[2] = { 65, 108 };
  43. module_param_array(tv_range, int, NULL, 0644);
  44. module_param_array(radio_range, int, NULL, 0644);
  45. MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
  46. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  47. MODULE_LICENSE("GPL");
  48. static int this_adap;
  49. static unsigned short first_tuner, tv_tuner, radio_tuner;
  50. static struct i2c_driver driver;
  51. static struct i2c_client client_template;
  52. /* ---------------------------------------------------------------------- */
  53. /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
  54. static void set_tv_freq(struct i2c_client *c, unsigned int freq)
  55. {
  56. struct tuner *t = i2c_get_clientdata(c);
  57. if (t->type == UNSET) {
  58. tuner_info("tuner type not set\n");
  59. return;
  60. }
  61. if (NULL == t->tv_freq) {
  62. tuner_info("Huh? tv_set is NULL?\n");
  63. return;
  64. }
  65. if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
  66. tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
  67. freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
  68. }
  69. t->tv_freq(c,freq);
  70. }
  71. static void set_radio_freq(struct i2c_client *c, unsigned int freq)
  72. {
  73. struct tuner *t = i2c_get_clientdata(c);
  74. if (t->type == UNSET) {
  75. tuner_info("tuner type not set\n");
  76. return;
  77. }
  78. if (NULL == t->radio_freq) {
  79. tuner_info("no radio tuning for this one, sorry.\n");
  80. return;
  81. }
  82. if (freq >= radio_range[0]*16000 && freq <= radio_range[1]*16000) {
  83. if (tuner_debug)
  84. tuner_info("radio freq step 62.5Hz (%d.%06d)\n",
  85. freq/16000,freq%16000*1000/16);
  86. t->radio_freq(c,freq);
  87. } else {
  88. tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
  89. freq/16,freq%16*100/16,
  90. radio_range[0],radio_range[1]);
  91. }
  92. return;
  93. }
  94. static void set_freq(struct i2c_client *c, unsigned long freq)
  95. {
  96. struct tuner *t = i2c_get_clientdata(c);
  97. switch (t->mode) {
  98. case V4L2_TUNER_RADIO:
  99. tuner_dbg("radio freq set to %lu.%02lu\n",
  100. freq/16,freq%16*100/16);
  101. set_radio_freq(c,freq);
  102. break;
  103. case V4L2_TUNER_ANALOG_TV:
  104. case V4L2_TUNER_DIGITAL_TV:
  105. tuner_dbg("tv freq set to %lu.%02lu\n",
  106. freq/16,freq%16*100/16);
  107. set_tv_freq(c, freq);
  108. break;
  109. }
  110. t->freq = freq;
  111. }
  112. static void set_type(struct i2c_client *c, unsigned int type)
  113. {
  114. struct tuner *t = i2c_get_clientdata(c);
  115. unsigned char buffer[4];
  116. /* sanity check */
  117. if (type == UNSET || type == TUNER_ABSENT)
  118. return;
  119. if (type >= tuner_count)
  120. return;
  121. if (NULL == t->i2c.dev.driver) {
  122. /* not registered yet */
  123. t->type = type;
  124. return;
  125. }
  126. if ((t->initialized) && (t->type == type))
  127. /* run only once except type change Hac 04/05*/
  128. return;
  129. t->initialized = 1;
  130. t->type = type;
  131. switch (t->type) {
  132. case TUNER_MT2032:
  133. microtune_init(c);
  134. break;
  135. case TUNER_PHILIPS_TDA8290:
  136. tda8290_init(c);
  137. break;
  138. case TUNER_TEA5767:
  139. if (tea5767_tuner_init(c)==EINVAL) t->type=TUNER_ABSENT;
  140. break;
  141. case TUNER_PHILIPS_FMD1216ME_MK3:
  142. buffer[0] = 0x0b;
  143. buffer[1] = 0xdc;
  144. buffer[2] = 0x9c;
  145. buffer[3] = 0x60;
  146. i2c_master_send(c,buffer,4);
  147. mdelay(1);
  148. buffer[2] = 0x86;
  149. buffer[3] = 0x54;
  150. i2c_master_send(c,buffer,4);
  151. default_tuner_init(c);
  152. break;
  153. default:
  154. /* TEA5767 autodetection code */
  155. if (tea5767_tuner_init(c)!=EINVAL) {
  156. t->type = TUNER_TEA5767;
  157. if (first_tuner == 0x60)
  158. first_tuner++;
  159. break;
  160. }
  161. default_tuner_init(c);
  162. break;
  163. }
  164. tuner_dbg ("I2C addr 0x%02x with type %d\n",c->addr<<1,type);
  165. }
  166. #define CHECK_ADDR(tp,cmd,tun) if (client->addr!=tp) { \
  167. return 0; } else if (tuner_debug) \
  168. tuner_info ("Cmd %s accepted to "tun"\n",cmd);
  169. #define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
  170. CHECK_ADDR(radio_tuner,cmd,"radio") } else \
  171. { CHECK_ADDR(tv_tuner,cmd,"TV"); }
  172. static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
  173. {
  174. /* ADDR_UNSET defaults to first available tuner */
  175. if ( tun_addr->addr == ADDR_UNSET ) {
  176. if (first_tuner != c->addr)
  177. return;
  178. switch (tun_addr->v4l2_tuner) {
  179. case V4L2_TUNER_RADIO:
  180. radio_tuner=c->addr;
  181. break;
  182. default:
  183. tv_tuner=c->addr;
  184. break;
  185. }
  186. } else {
  187. /* Sets tuner to its configured value */
  188. switch (tun_addr->v4l2_tuner) {
  189. case V4L2_TUNER_RADIO:
  190. radio_tuner=tun_addr->addr;
  191. if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
  192. return;
  193. default:
  194. tv_tuner=tun_addr->addr;
  195. if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
  196. return;
  197. }
  198. }
  199. set_type(c,tun_addr->type);
  200. }
  201. static char pal[] = "-";
  202. module_param_string(pal, pal, sizeof(pal), 0644);
  203. static int tuner_fixup_std(struct tuner *t)
  204. {
  205. if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
  206. /* get more precise norm info from insmod option */
  207. switch (pal[0]) {
  208. case 'b':
  209. case 'B':
  210. case 'g':
  211. case 'G':
  212. tuner_dbg("insmod fixup: PAL => PAL-BG\n");
  213. t->std = V4L2_STD_PAL_BG;
  214. break;
  215. case 'i':
  216. case 'I':
  217. tuner_dbg("insmod fixup: PAL => PAL-I\n");
  218. t->std = V4L2_STD_PAL_I;
  219. break;
  220. case 'd':
  221. case 'D':
  222. case 'k':
  223. case 'K':
  224. tuner_dbg("insmod fixup: PAL => PAL-DK\n");
  225. t->std = V4L2_STD_PAL_DK;
  226. break;
  227. }
  228. }
  229. return 0;
  230. }
  231. /* ---------------------------------------------------------------------- */
  232. static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
  233. {
  234. struct tuner *t;
  235. /* by default, first I2C card is both tv and radio tuner */
  236. if (this_adap == 0) {
  237. first_tuner = addr;
  238. tv_tuner = addr;
  239. radio_tuner = addr;
  240. }
  241. this_adap++;
  242. client_template.adapter = adap;
  243. client_template.addr = addr;
  244. t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
  245. if (NULL == t)
  246. return -ENOMEM;
  247. memset(t,0,sizeof(struct tuner));
  248. memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
  249. i2c_set_clientdata(&t->i2c, t);
  250. t->type = UNSET;
  251. t->radio_if2 = 10700*1000; /* 10.7MHz - FM radio */
  252. t->audmode = V4L2_TUNER_MODE_STEREO;
  253. i2c_attach_client(&t->i2c);
  254. tuner_info("chip found @ 0x%x (%s)\n",
  255. addr << 1, adap->name);
  256. set_type(&t->i2c, t->type);
  257. return 0;
  258. }
  259. static int tuner_probe(struct i2c_adapter *adap)
  260. {
  261. if (0 != addr) {
  262. normal_i2c[0] = addr;
  263. normal_i2c[1] = I2C_CLIENT_END;
  264. }
  265. this_adap = 0;
  266. first_tuner = 0;
  267. tv_tuner = 0;
  268. radio_tuner = 0;
  269. if (adap->class & I2C_CLASS_TV_ANALOG)
  270. return i2c_probe(adap, &addr_data, tuner_attach);
  271. return 0;
  272. }
  273. static int tuner_detach(struct i2c_client *client)
  274. {
  275. struct tuner *t = i2c_get_clientdata(client);
  276. int err;
  277. err=i2c_detach_client(&t->i2c);
  278. if (err) {
  279. tuner_warn ("Client deregistration failed, client not detached.\n");
  280. return err;
  281. }
  282. kfree(t);
  283. return 0;
  284. }
  285. #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
  286. tuner_info("switching to v4l2\n"); \
  287. t->using_v4l2 = 1;
  288. #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
  289. tuner_info("ignore v4l1 call\n"); \
  290. return 0; }
  291. static int
  292. tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
  293. {
  294. struct tuner *t = i2c_get_clientdata(client);
  295. unsigned int *iarg = (int*)arg;
  296. switch (cmd) {
  297. /* --- configuration --- */
  298. case TUNER_SET_TYPE:
  299. set_type(client,*iarg);
  300. break;
  301. case TUNER_SET_TYPE_ADDR:
  302. set_addr(client,(struct tuner_addr *)arg);
  303. break;
  304. case AUDC_SET_RADIO:
  305. t->mode = V4L2_TUNER_RADIO;
  306. CHECK_ADDR(tv_tuner,"AUDC_SET_RADIO","TV");
  307. if (V4L2_TUNER_RADIO != t->mode) {
  308. set_tv_freq(client,400 * 16);
  309. }
  310. break;
  311. case AUDC_CONFIG_PINNACLE:
  312. CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE","TV");
  313. switch (*iarg) {
  314. case 2:
  315. tuner_dbg("pinnacle pal\n");
  316. t->radio_if2 = 33300 * 1000;
  317. break;
  318. case 3:
  319. tuner_dbg("pinnacle ntsc\n");
  320. t->radio_if2 = 41300 * 1000;
  321. break;
  322. }
  323. break;
  324. /* --- v4l ioctls --- */
  325. /* take care: bttv does userspace copying, we'll get a
  326. kernel pointer here... */
  327. case VIDIOCSCHAN:
  328. {
  329. static const v4l2_std_id map[] = {
  330. [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
  331. [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
  332. [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
  333. [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
  334. [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
  335. [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
  336. };
  337. struct video_channel *vc = arg;
  338. CHECK_V4L2;
  339. t->mode = V4L2_TUNER_ANALOG_TV;
  340. CHECK_ADDR(tv_tuner,"VIDIOCSCHAN","TV");
  341. if (vc->norm < ARRAY_SIZE(map))
  342. t->std = map[vc->norm];
  343. tuner_fixup_std(t);
  344. if (t->freq)
  345. set_tv_freq(client,t->freq);
  346. return 0;
  347. }
  348. case VIDIOCSFREQ:
  349. {
  350. unsigned long *v = arg;
  351. CHECK_MODE("VIDIOCSFREQ");
  352. CHECK_V4L2;
  353. set_freq(client,*v);
  354. return 0;
  355. }
  356. case VIDIOCGTUNER:
  357. {
  358. struct video_tuner *vt = arg;
  359. CHECK_ADDR(radio_tuner,"VIDIOCGTUNER","radio");
  360. CHECK_V4L2;
  361. if (V4L2_TUNER_RADIO == t->mode) {
  362. if (t->has_signal)
  363. vt->signal = t->has_signal(client);
  364. if (t->is_stereo) {
  365. if (t->is_stereo(client))
  366. vt->flags |= VIDEO_TUNER_STEREO_ON;
  367. else
  368. vt->flags &= ~VIDEO_TUNER_STEREO_ON;
  369. }
  370. vt->flags |= V4L2_TUNER_CAP_LOW; /* Allow freqs at 62.5 Hz */
  371. vt->rangelow = radio_range[0] * 16000;
  372. vt->rangehigh = radio_range[1] * 16000;
  373. } else {
  374. vt->rangelow = tv_range[0] * 16;
  375. vt->rangehigh = tv_range[1] * 16;
  376. }
  377. return 0;
  378. }
  379. case VIDIOCGAUDIO:
  380. {
  381. struct video_audio *va = arg;
  382. CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO","radio");
  383. CHECK_V4L2;
  384. if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
  385. va->mode = t->is_stereo(client)
  386. ? VIDEO_SOUND_STEREO
  387. : VIDEO_SOUND_MONO;
  388. return 0;
  389. }
  390. case VIDIOC_S_STD:
  391. {
  392. v4l2_std_id *id = arg;
  393. SWITCH_V4L2;
  394. t->mode = V4L2_TUNER_ANALOG_TV;
  395. CHECK_ADDR(tv_tuner,"VIDIOC_S_STD","TV");
  396. t->std = *id;
  397. tuner_fixup_std(t);
  398. if (t->freq)
  399. set_freq(client,t->freq);
  400. break;
  401. }
  402. case VIDIOC_S_FREQUENCY:
  403. {
  404. struct v4l2_frequency *f = arg;
  405. CHECK_MODE("VIDIOC_S_FREQUENCY");
  406. SWITCH_V4L2;
  407. if (V4L2_TUNER_RADIO == f->type &&
  408. V4L2_TUNER_RADIO != t->mode)
  409. set_tv_freq(client,400*16);
  410. t->mode = f->type;
  411. set_freq(client,f->frequency);
  412. break;
  413. }
  414. case VIDIOC_G_FREQUENCY:
  415. {
  416. struct v4l2_frequency *f = arg;
  417. CHECK_MODE("VIDIOC_G_FREQUENCY");
  418. SWITCH_V4L2;
  419. f->type = t->mode;
  420. f->frequency = t->freq;
  421. break;
  422. }
  423. case VIDIOC_G_TUNER:
  424. {
  425. struct v4l2_tuner *tuner = arg;
  426. CHECK_MODE("VIDIOC_G_TUNER");
  427. SWITCH_V4L2;
  428. if (V4L2_TUNER_RADIO == t->mode) {
  429. if (t->has_signal)
  430. tuner -> signal = t->has_signal(client);
  431. if (t->is_stereo) {
  432. if (t->is_stereo(client)) {
  433. tuner -> rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
  434. } else {
  435. tuner -> rxsubchans = V4L2_TUNER_SUB_MONO;
  436. }
  437. }
  438. tuner->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  439. tuner->audmode = t->audmode;
  440. tuner->rangelow = radio_range[0] * 16000;
  441. tuner->rangehigh = radio_range[1] * 16000;
  442. } else {
  443. tuner->rangelow = tv_range[0] * 16;
  444. tuner->rangehigh = tv_range[1] * 16;
  445. }
  446. break;
  447. }
  448. case VIDIOC_S_TUNER: /* Allow changing radio range and audio mode */
  449. {
  450. struct v4l2_tuner *tuner = arg;
  451. CHECK_ADDR(radio_tuner,"VIDIOC_S_TUNER","radio");
  452. SWITCH_V4L2;
  453. /* To switch the audio mode, applications initialize the
  454. index and audmode fields and the reserved array and
  455. call the VIDIOC_S_TUNER ioctl. */
  456. /* rxsubchannels: V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
  457. V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
  458. V4L2_TUNER_MODE_SAP */
  459. if (tuner->audmode == V4L2_TUNER_MODE_MONO)
  460. t->audmode = V4L2_TUNER_MODE_MONO;
  461. else
  462. t->audmode = V4L2_TUNER_MODE_STEREO;
  463. set_radio_freq(client, t->freq);
  464. break;
  465. }
  466. case TDA9887_SET_CONFIG: /* Nothing to do on tuner-core */
  467. break;
  468. default:
  469. tuner_dbg ("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
  470. /* nothing */
  471. break;
  472. }
  473. return 0;
  474. }
  475. static int tuner_suspend(struct device * dev, u32 state, u32 level)
  476. {
  477. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  478. struct tuner *t = i2c_get_clientdata(c);
  479. tuner_dbg("suspend\n");
  480. /* FIXME: power down ??? */
  481. return 0;
  482. }
  483. static int tuner_resume(struct device * dev, u32 level)
  484. {
  485. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  486. struct tuner *t = i2c_get_clientdata(c);
  487. tuner_dbg("resume\n");
  488. if (t->freq)
  489. set_freq(c,t->freq);
  490. return 0;
  491. }
  492. /* ----------------------------------------------------------------------- */
  493. static struct i2c_driver driver = {
  494. .owner = THIS_MODULE,
  495. .name = "tuner",
  496. .id = I2C_DRIVERID_TUNER,
  497. .flags = I2C_DF_NOTIFY,
  498. .attach_adapter = tuner_probe,
  499. .detach_client = tuner_detach,
  500. .command = tuner_command,
  501. .driver = {
  502. .suspend = tuner_suspend,
  503. .resume = tuner_resume,
  504. },
  505. };
  506. static struct i2c_client client_template =
  507. {
  508. I2C_DEVNAME("(tuner unset)"),
  509. .flags = I2C_CLIENT_ALLOW_USE,
  510. .driver = &driver,
  511. };
  512. static int __init tuner_init_module(void)
  513. {
  514. return i2c_add_driver(&driver);
  515. }
  516. static void __exit tuner_cleanup_module(void)
  517. {
  518. i2c_del_driver(&driver);
  519. }
  520. module_init(tuner_init_module);
  521. module_exit(tuner_cleanup_module);
  522. /*
  523. * Overrides for Emacs so that we follow Linus's tabbing style.
  524. * ---------------------------------------------------------------------------
  525. * Local variables:
  526. * c-basic-offset: 8
  527. * End:
  528. */