tea575x-tuner.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips
  3. *
  4. * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <asm/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-dev.h>
  30. #include <media/v4l2-fh.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-event.h>
  33. #include <sound/tea575x-tuner.h>
  34. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  35. MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
  36. MODULE_LICENSE("GPL");
  37. /*
  38. * definitions
  39. */
  40. #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */
  41. #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */
  42. #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
  43. #define TEA575X_BIT_BAND_MASK (3<<20)
  44. #define TEA575X_BIT_BAND_FM (0<<20)
  45. #define TEA575X_BIT_BAND_MW (1<<20)
  46. #define TEA575X_BIT_BAND_LW (2<<20)
  47. #define TEA575X_BIT_BAND_SW (3<<20)
  48. #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */
  49. #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */
  50. #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */
  51. #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */
  52. #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */
  53. #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */
  54. #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */
  55. #define TEA575X_BIT_DUMMY (1<<15) /* buffer */
  56. #define TEA575X_BIT_FREQ_MASK 0x7fff
  57. enum { BAND_FM, BAND_FM_JAPAN, BAND_AM };
  58. static const struct v4l2_frequency_band bands[] = {
  59. {
  60. .type = V4L2_TUNER_RADIO,
  61. .index = 0,
  62. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  63. V4L2_TUNER_CAP_FREQ_BANDS,
  64. .rangelow = 87500 * 16,
  65. .rangehigh = 108000 * 16,
  66. .modulation = V4L2_BAND_MODULATION_FM,
  67. },
  68. {
  69. .type = V4L2_TUNER_RADIO,
  70. .index = 0,
  71. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  72. V4L2_TUNER_CAP_FREQ_BANDS,
  73. .rangelow = 76000 * 16,
  74. .rangehigh = 91000 * 16,
  75. .modulation = V4L2_BAND_MODULATION_FM,
  76. },
  77. {
  78. .type = V4L2_TUNER_RADIO,
  79. .index = 1,
  80. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
  81. .rangelow = 530 * 16,
  82. .rangehigh = 1710 * 16,
  83. .modulation = V4L2_BAND_MODULATION_AM,
  84. },
  85. };
  86. /*
  87. * lowlevel part
  88. */
  89. static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
  90. {
  91. u16 l;
  92. u8 data;
  93. if (tea->ops->write_val)
  94. return tea->ops->write_val(tea, val);
  95. tea->ops->set_direction(tea, 1);
  96. udelay(16);
  97. for (l = 25; l > 0; l--) {
  98. data = (val >> 24) & TEA575X_DATA;
  99. val <<= 1; /* shift data */
  100. tea->ops->set_pins(tea, data | TEA575X_WREN);
  101. udelay(2);
  102. tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
  103. udelay(2);
  104. tea->ops->set_pins(tea, data | TEA575X_WREN);
  105. udelay(2);
  106. }
  107. if (!tea->mute)
  108. tea->ops->set_pins(tea, 0);
  109. }
  110. static u32 snd_tea575x_read(struct snd_tea575x *tea)
  111. {
  112. u16 l, rdata;
  113. u32 data = 0;
  114. if (tea->ops->read_val)
  115. return tea->ops->read_val(tea);
  116. tea->ops->set_direction(tea, 0);
  117. tea->ops->set_pins(tea, 0);
  118. udelay(16);
  119. for (l = 24; l--;) {
  120. tea->ops->set_pins(tea, TEA575X_CLK);
  121. udelay(2);
  122. if (!l)
  123. tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
  124. tea->ops->set_pins(tea, 0);
  125. udelay(2);
  126. data <<= 1; /* shift data */
  127. rdata = tea->ops->get_pins(tea);
  128. if (!l)
  129. tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
  130. if (rdata & TEA575X_DATA)
  131. data++;
  132. udelay(2);
  133. }
  134. if (tea->mute)
  135. tea->ops->set_pins(tea, TEA575X_WREN);
  136. return data;
  137. }
  138. static u32 snd_tea575x_val_to_freq(struct snd_tea575x *tea, u32 val)
  139. {
  140. u32 freq = val & TEA575X_BIT_FREQ_MASK;
  141. if (freq == 0)
  142. return freq;
  143. switch (tea->band) {
  144. case BAND_FM:
  145. /* freq *= 12.5 */
  146. freq *= 125;
  147. freq /= 10;
  148. /* crystal fixup */
  149. freq -= TEA575X_FMIF;
  150. break;
  151. case BAND_FM_JAPAN:
  152. /* freq *= 12.5 */
  153. freq *= 125;
  154. freq /= 10;
  155. /* crystal fixup */
  156. freq += TEA575X_FMIF;
  157. break;
  158. case BAND_AM:
  159. /* crystal fixup */
  160. freq -= TEA575X_AMIF;
  161. break;
  162. }
  163. return clamp(freq * 16, bands[tea->band].rangelow,
  164. bands[tea->band].rangehigh); /* from kHz */
  165. }
  166. static u32 snd_tea575x_get_freq(struct snd_tea575x *tea)
  167. {
  168. return snd_tea575x_val_to_freq(tea, snd_tea575x_read(tea));
  169. }
  170. void snd_tea575x_set_freq(struct snd_tea575x *tea)
  171. {
  172. u32 freq = tea->freq / 16; /* to kHz */
  173. u32 band = 0;
  174. switch (tea->band) {
  175. case BAND_FM:
  176. band = TEA575X_BIT_BAND_FM;
  177. /* crystal fixup */
  178. freq += TEA575X_FMIF;
  179. /* freq /= 12.5 */
  180. freq *= 10;
  181. freq /= 125;
  182. break;
  183. case BAND_FM_JAPAN:
  184. band = TEA575X_BIT_BAND_FM;
  185. /* crystal fixup */
  186. freq -= TEA575X_FMIF;
  187. /* freq /= 12.5 */
  188. freq *= 10;
  189. freq /= 125;
  190. break;
  191. case BAND_AM:
  192. band = TEA575X_BIT_BAND_MW;
  193. /* crystal fixup */
  194. freq += TEA575X_AMIF;
  195. break;
  196. }
  197. tea->val &= ~(TEA575X_BIT_FREQ_MASK | TEA575X_BIT_BAND_MASK);
  198. tea->val |= band;
  199. tea->val |= freq & TEA575X_BIT_FREQ_MASK;
  200. snd_tea575x_write(tea, tea->val);
  201. tea->freq = snd_tea575x_val_to_freq(tea, tea->val);
  202. }
  203. /*
  204. * Linux Video interface
  205. */
  206. static int vidioc_querycap(struct file *file, void *priv,
  207. struct v4l2_capability *v)
  208. {
  209. struct snd_tea575x *tea = video_drvdata(file);
  210. strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
  211. strlcpy(v->card, tea->card, sizeof(v->card));
  212. strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
  213. strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
  214. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  215. if (!tea->cannot_read_data)
  216. v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
  217. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  218. return 0;
  219. }
  220. static int vidioc_enum_freq_bands(struct file *file, void *priv,
  221. struct v4l2_frequency_band *band)
  222. {
  223. struct snd_tea575x *tea = video_drvdata(file);
  224. int index;
  225. if (band->tuner != 0)
  226. return -EINVAL;
  227. switch (band->index) {
  228. case 0:
  229. if (tea->tea5759)
  230. index = BAND_FM_JAPAN;
  231. else
  232. index = BAND_FM;
  233. break;
  234. case 1:
  235. if (tea->has_am) {
  236. index = BAND_AM;
  237. break;
  238. }
  239. /* Fall through */
  240. default:
  241. return -EINVAL;
  242. }
  243. *band = bands[index];
  244. if (!tea->cannot_read_data)
  245. band->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
  246. return 0;
  247. }
  248. static int vidioc_g_tuner(struct file *file, void *priv,
  249. struct v4l2_tuner *v)
  250. {
  251. struct snd_tea575x *tea = video_drvdata(file);
  252. struct v4l2_frequency_band band_fm = { 0, };
  253. if (v->index > 0)
  254. return -EINVAL;
  255. snd_tea575x_read(tea);
  256. vidioc_enum_freq_bands(file, priv, &band_fm);
  257. memset(v, 0, sizeof(*v));
  258. strlcpy(v->name, tea->has_am ? "FM/AM" : "FM", sizeof(v->name));
  259. v->type = V4L2_TUNER_RADIO;
  260. v->capability = band_fm.capability;
  261. v->rangelow = tea->has_am ? bands[BAND_AM].rangelow : band_fm.rangelow;
  262. v->rangehigh = band_fm.rangehigh;
  263. v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  264. v->audmode = (tea->val & TEA575X_BIT_MONO) ?
  265. V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
  266. v->signal = tea->tuned ? 0xffff : 0;
  267. return 0;
  268. }
  269. static int vidioc_s_tuner(struct file *file, void *priv,
  270. struct v4l2_tuner *v)
  271. {
  272. struct snd_tea575x *tea = video_drvdata(file);
  273. u32 orig_val = tea->val;
  274. if (v->index)
  275. return -EINVAL;
  276. tea->val &= ~TEA575X_BIT_MONO;
  277. if (v->audmode == V4L2_TUNER_MODE_MONO)
  278. tea->val |= TEA575X_BIT_MONO;
  279. /* Only apply changes if currently tuning FM */
  280. if (tea->band != BAND_AM && tea->val != orig_val)
  281. snd_tea575x_set_freq(tea);
  282. return 0;
  283. }
  284. static int vidioc_g_frequency(struct file *file, void *priv,
  285. struct v4l2_frequency *f)
  286. {
  287. struct snd_tea575x *tea = video_drvdata(file);
  288. if (f->tuner != 0)
  289. return -EINVAL;
  290. f->type = V4L2_TUNER_RADIO;
  291. f->frequency = tea->freq;
  292. return 0;
  293. }
  294. static int vidioc_s_frequency(struct file *file, void *priv,
  295. struct v4l2_frequency *f)
  296. {
  297. struct snd_tea575x *tea = video_drvdata(file);
  298. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  299. return -EINVAL;
  300. if (tea->has_am && f->frequency < (20000 * 16))
  301. tea->band = BAND_AM;
  302. else if (tea->tea5759)
  303. tea->band = BAND_FM_JAPAN;
  304. else
  305. tea->band = BAND_FM;
  306. tea->freq = clamp(f->frequency, bands[tea->band].rangelow,
  307. bands[tea->band].rangehigh);
  308. snd_tea575x_set_freq(tea);
  309. return 0;
  310. }
  311. static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
  312. const struct v4l2_hw_freq_seek *a)
  313. {
  314. struct snd_tea575x *tea = video_drvdata(file);
  315. unsigned long timeout;
  316. int i, spacing;
  317. if (tea->cannot_read_data)
  318. return -ENOTTY;
  319. if (a->tuner || a->wrap_around)
  320. return -EINVAL;
  321. if (file->f_flags & O_NONBLOCK)
  322. return -EWOULDBLOCK;
  323. if (a->rangelow || a->rangehigh) {
  324. for (i = 0; i < ARRAY_SIZE(bands); i++) {
  325. if ((i == BAND_FM && tea->tea5759) ||
  326. (i == BAND_FM_JAPAN && !tea->tea5759) ||
  327. (i == BAND_AM && !tea->has_am))
  328. continue;
  329. if (bands[i].rangelow == a->rangelow &&
  330. bands[i].rangehigh == a->rangehigh)
  331. break;
  332. }
  333. if (i == ARRAY_SIZE(bands))
  334. return -EINVAL; /* No matching band found */
  335. if (i != tea->band) {
  336. tea->band = i;
  337. tea->freq = clamp(tea->freq, bands[i].rangelow,
  338. bands[i].rangehigh);
  339. snd_tea575x_set_freq(tea);
  340. }
  341. }
  342. spacing = (tea->band == BAND_AM) ? 5 : 50; /* kHz */
  343. /* clear the frequency, HW will fill it in */
  344. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  345. tea->val |= TEA575X_BIT_SEARCH;
  346. if (a->seek_upward)
  347. tea->val |= TEA575X_BIT_UPDOWN;
  348. else
  349. tea->val &= ~TEA575X_BIT_UPDOWN;
  350. snd_tea575x_write(tea, tea->val);
  351. timeout = jiffies + msecs_to_jiffies(10000);
  352. for (;;) {
  353. if (time_after(jiffies, timeout))
  354. break;
  355. if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
  356. /* some signal arrived, stop search */
  357. tea->val &= ~TEA575X_BIT_SEARCH;
  358. snd_tea575x_set_freq(tea);
  359. return -ERESTARTSYS;
  360. }
  361. if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
  362. u32 freq;
  363. /* Found a frequency, wait until it can be read */
  364. for (i = 0; i < 100; i++) {
  365. msleep(10);
  366. freq = snd_tea575x_get_freq(tea);
  367. if (freq) /* available */
  368. break;
  369. }
  370. if (freq == 0) /* shouldn't happen */
  371. break;
  372. /*
  373. * if we moved by less than the spacing, or in the
  374. * wrong direction, continue seeking
  375. */
  376. if (abs(tea->freq - freq) < 16 * spacing ||
  377. (a->seek_upward && freq < tea->freq) ||
  378. (!a->seek_upward && freq > tea->freq)) {
  379. snd_tea575x_write(tea, tea->val);
  380. continue;
  381. }
  382. tea->freq = freq;
  383. tea->val &= ~TEA575X_BIT_SEARCH;
  384. return 0;
  385. }
  386. }
  387. tea->val &= ~TEA575X_BIT_SEARCH;
  388. snd_tea575x_set_freq(tea);
  389. return -ENODATA;
  390. }
  391. static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
  392. {
  393. struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
  394. switch (ctrl->id) {
  395. case V4L2_CID_AUDIO_MUTE:
  396. tea->mute = ctrl->val;
  397. snd_tea575x_set_freq(tea);
  398. return 0;
  399. }
  400. return -EINVAL;
  401. }
  402. static const struct v4l2_file_operations tea575x_fops = {
  403. .unlocked_ioctl = video_ioctl2,
  404. .open = v4l2_fh_open,
  405. .release = v4l2_fh_release,
  406. .poll = v4l2_ctrl_poll,
  407. };
  408. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  409. .vidioc_querycap = vidioc_querycap,
  410. .vidioc_g_tuner = vidioc_g_tuner,
  411. .vidioc_s_tuner = vidioc_s_tuner,
  412. .vidioc_g_frequency = vidioc_g_frequency,
  413. .vidioc_s_frequency = vidioc_s_frequency,
  414. .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
  415. .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
  416. .vidioc_log_status = v4l2_ctrl_log_status,
  417. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  418. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  419. };
  420. static const struct video_device tea575x_radio = {
  421. .ioctl_ops = &tea575x_ioctl_ops,
  422. .release = video_device_release_empty,
  423. };
  424. static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
  425. .s_ctrl = tea575x_s_ctrl,
  426. };
  427. /*
  428. * initialize all the tea575x chips
  429. */
  430. int snd_tea575x_init(struct snd_tea575x *tea, struct module *owner)
  431. {
  432. int retval;
  433. tea->mute = true;
  434. /* Not all devices can or know how to read the data back.
  435. Such devices can set cannot_read_data to true. */
  436. if (!tea->cannot_read_data) {
  437. snd_tea575x_write(tea, 0x55AA);
  438. if (snd_tea575x_read(tea) != 0x55AA)
  439. return -ENODEV;
  440. }
  441. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
  442. tea->freq = 90500 * 16; /* 90.5Mhz default */
  443. snd_tea575x_set_freq(tea);
  444. tea->vd = tea575x_radio;
  445. video_set_drvdata(&tea->vd, tea);
  446. mutex_init(&tea->mutex);
  447. strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
  448. tea->vd.lock = &tea->mutex;
  449. tea->vd.v4l2_dev = tea->v4l2_dev;
  450. tea->fops = tea575x_fops;
  451. tea->fops.owner = owner;
  452. tea->vd.fops = &tea->fops;
  453. set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
  454. /* disable hw_freq_seek if we can't use it */
  455. if (tea->cannot_read_data)
  456. v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK);
  457. if (!tea->cannot_mute) {
  458. tea->vd.ctrl_handler = &tea->ctrl_handler;
  459. v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
  460. v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
  461. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  462. retval = tea->ctrl_handler.error;
  463. if (retval) {
  464. v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
  465. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  466. return retval;
  467. }
  468. if (tea->ext_init) {
  469. retval = tea->ext_init(tea);
  470. if (retval) {
  471. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  472. return retval;
  473. }
  474. }
  475. v4l2_ctrl_handler_setup(&tea->ctrl_handler);
  476. }
  477. retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
  478. if (retval) {
  479. v4l2_err(tea->v4l2_dev, "can't register video device!\n");
  480. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  481. return retval;
  482. }
  483. return 0;
  484. }
  485. void snd_tea575x_exit(struct snd_tea575x *tea)
  486. {
  487. video_unregister_device(&tea->vd);
  488. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  489. }
  490. static int __init alsa_tea575x_module_init(void)
  491. {
  492. return 0;
  493. }
  494. static void __exit alsa_tea575x_module_exit(void)
  495. {
  496. }
  497. module_init(alsa_tea575x_module_init)
  498. module_exit(alsa_tea575x_module_exit)
  499. EXPORT_SYMBOL(snd_tea575x_init);
  500. EXPORT_SYMBOL(snd_tea575x_exit);
  501. EXPORT_SYMBOL(snd_tea575x_set_freq);