tuner-core.c 14 KB

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