tea575x-tuner.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/interrupt.h>
  25. #include <linux/init.h>
  26. #include <sound/core.h>
  27. #include <sound/tea575x-tuner.h>
  28. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  29. MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
  30. MODULE_LICENSE("GPL");
  31. /*
  32. * definitions
  33. */
  34. #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */
  35. #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */
  36. #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
  37. #define TEA575X_BIT_BAND_MASK (3<<20)
  38. #define TEA575X_BIT_BAND_FM (0<<20)
  39. #define TEA575X_BIT_BAND_MW (1<<20)
  40. #define TEA575X_BIT_BAND_LW (1<<21)
  41. #define TEA575X_BIT_BAND_SW (1<<22)
  42. #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */
  43. #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */
  44. #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */
  45. #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */
  46. #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */
  47. #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */
  48. #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */
  49. #define TEA575X_BIT_DUMMY (1<<15) /* buffer */
  50. #define TEA575X_BIT_FREQ_MASK 0x7fff
  51. /*
  52. * lowlevel part
  53. */
  54. static void snd_tea575x_set_freq(struct snd_tea575x *tea)
  55. {
  56. unsigned long freq;
  57. freq = tea->freq / 16; /* to kHz */
  58. if (freq > 108000)
  59. freq = 108000;
  60. if (freq < 87000)
  61. freq = 87000;
  62. /* crystal fixup */
  63. if (tea->tea5759)
  64. freq -= tea->freq_fixup;
  65. else
  66. freq += tea->freq_fixup;
  67. /* freq /= 12.5 */
  68. freq *= 10;
  69. freq /= 125;
  70. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  71. tea->val |= freq & TEA575X_BIT_FREQ_MASK;
  72. tea->ops->write(tea, tea->val);
  73. }
  74. /*
  75. * Linux Video interface
  76. */
  77. static int snd_tea575x_ioctl(struct inode *inode, struct file *file,
  78. unsigned int cmd, unsigned long data)
  79. {
  80. struct video_device *dev = video_devdata(file);
  81. struct snd_tea575x *tea = video_get_drvdata(dev);
  82. void __user *arg = (void __user *)data;
  83. switch(cmd) {
  84. case VIDIOCGCAP:
  85. {
  86. struct video_capability v;
  87. v.type = VID_TYPE_TUNER;
  88. v.channels = 1;
  89. v.audios = 1;
  90. /* No we don't do pictures */
  91. v.maxwidth = 0;
  92. v.maxheight = 0;
  93. v.minwidth = 0;
  94. v.minheight = 0;
  95. strcpy(v.name, tea->tea5759 ? "TEA5759" : "TEA5757");
  96. if (copy_to_user(arg,&v,sizeof(v)))
  97. return -EFAULT;
  98. return 0;
  99. }
  100. case VIDIOCGTUNER:
  101. {
  102. struct video_tuner v;
  103. if (copy_from_user(&v, arg,sizeof(v))!=0)
  104. return -EFAULT;
  105. if (v.tuner) /* Only 1 tuner */
  106. return -EINVAL;
  107. v.rangelow = (87*16000);
  108. v.rangehigh = (108*16000);
  109. v.flags = VIDEO_TUNER_LOW;
  110. v.mode = VIDEO_MODE_AUTO;
  111. strcpy(v.name, "FM");
  112. v.signal = 0xFFFF;
  113. if (copy_to_user(arg, &v, sizeof(v)))
  114. return -EFAULT;
  115. return 0;
  116. }
  117. case VIDIOCSTUNER:
  118. {
  119. struct video_tuner v;
  120. if(copy_from_user(&v, arg, sizeof(v)))
  121. return -EFAULT;
  122. if(v.tuner!=0)
  123. return -EINVAL;
  124. /* Only 1 tuner so no setting needed ! */
  125. return 0;
  126. }
  127. case VIDIOCGFREQ:
  128. if(copy_to_user(arg, &tea->freq, sizeof(tea->freq)))
  129. return -EFAULT;
  130. return 0;
  131. case VIDIOCSFREQ:
  132. if(copy_from_user(&tea->freq, arg, sizeof(tea->freq)))
  133. return -EFAULT;
  134. snd_tea575x_set_freq(tea);
  135. return 0;
  136. case VIDIOCGAUDIO:
  137. {
  138. struct video_audio v;
  139. memset(&v, 0, sizeof(v));
  140. strcpy(v.name, "Radio");
  141. if(copy_to_user(arg,&v, sizeof(v)))
  142. return -EFAULT;
  143. return 0;
  144. }
  145. case VIDIOCSAUDIO:
  146. {
  147. struct video_audio v;
  148. if(copy_from_user(&v, arg, sizeof(v)))
  149. return -EFAULT;
  150. if (tea->ops->mute)
  151. tea->ops->mute(tea,
  152. (v.flags &
  153. VIDEO_AUDIO_MUTE) ? 1 : 0);
  154. if(v.audio)
  155. return -EINVAL;
  156. return 0;
  157. }
  158. default:
  159. return -ENOIOCTLCMD;
  160. }
  161. }
  162. static void snd_tea575x_release(struct video_device *vfd)
  163. {
  164. }
  165. /*
  166. * initialize all the tea575x chips
  167. */
  168. void snd_tea575x_init(struct snd_tea575x *tea)
  169. {
  170. unsigned int val;
  171. val = tea->ops->read(tea);
  172. if (val == 0x1ffffff || val == 0) {
  173. snd_printk(KERN_ERR "Cannot find TEA575x chip\n");
  174. return;
  175. }
  176. memset(&tea->vd, 0, sizeof(tea->vd));
  177. strcpy(tea->vd.name, tea->tea5759 ? "TEA5759 radio" : "TEA5757 radio");
  178. tea->vd.release = snd_tea575x_release;
  179. video_set_drvdata(&tea->vd, tea);
  180. tea->vd.fops = &tea->fops;
  181. tea->fops.owner = tea->card->module;
  182. tea->fops.open = video_exclusive_open;
  183. tea->fops.release = video_exclusive_release;
  184. tea->fops.ioctl = snd_tea575x_ioctl;
  185. if (video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->dev_nr - 1) < 0) {
  186. snd_printk(KERN_ERR "unable to register tea575x tuner\n");
  187. return;
  188. }
  189. tea->vd_registered = 1;
  190. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
  191. tea->freq = 90500 * 16; /* 90.5Mhz default */
  192. snd_tea575x_set_freq(tea);
  193. /* mute on init */
  194. if (tea->ops->mute)
  195. tea->ops->mute(tea, 1);
  196. }
  197. void snd_tea575x_exit(struct snd_tea575x *tea)
  198. {
  199. if (tea->vd_registered) {
  200. video_unregister_device(&tea->vd);
  201. tea->vd_registered = 0;
  202. }
  203. }
  204. static int __init alsa_tea575x_module_init(void)
  205. {
  206. return 0;
  207. }
  208. static void __exit alsa_tea575x_module_exit(void)
  209. {
  210. }
  211. module_init(alsa_tea575x_module_init)
  212. module_exit(alsa_tea575x_module_exit)
  213. EXPORT_SYMBOL(snd_tea575x_init);
  214. EXPORT_SYMBOL(snd_tea575x_exit);