tea575x-tuner.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 snd_tea575x *tea = video_drvdata(file);
  81. void __user *arg = (void __user *)data;
  82. switch(cmd) {
  83. case VIDIOCGCAP:
  84. {
  85. struct video_capability v;
  86. v.type = VID_TYPE_TUNER;
  87. v.channels = 1;
  88. v.audios = 1;
  89. /* No we don't do pictures */
  90. v.maxwidth = 0;
  91. v.maxheight = 0;
  92. v.minwidth = 0;
  93. v.minheight = 0;
  94. strcpy(v.name, tea->tea5759 ? "TEA5759" : "TEA5757");
  95. if (copy_to_user(arg,&v,sizeof(v)))
  96. return -EFAULT;
  97. return 0;
  98. }
  99. case VIDIOCGTUNER:
  100. {
  101. struct video_tuner v;
  102. if (copy_from_user(&v, arg,sizeof(v))!=0)
  103. return -EFAULT;
  104. if (v.tuner) /* Only 1 tuner */
  105. return -EINVAL;
  106. v.rangelow = (87*16000);
  107. v.rangehigh = (108*16000);
  108. v.flags = VIDEO_TUNER_LOW;
  109. v.mode = VIDEO_MODE_AUTO;
  110. strcpy(v.name, "FM");
  111. v.signal = 0xFFFF;
  112. if (copy_to_user(arg, &v, sizeof(v)))
  113. return -EFAULT;
  114. return 0;
  115. }
  116. case VIDIOCSTUNER:
  117. {
  118. struct video_tuner v;
  119. if(copy_from_user(&v, arg, sizeof(v)))
  120. return -EFAULT;
  121. if(v.tuner!=0)
  122. return -EINVAL;
  123. /* Only 1 tuner so no setting needed ! */
  124. return 0;
  125. }
  126. case VIDIOCGFREQ:
  127. if(copy_to_user(arg, &tea->freq, sizeof(tea->freq)))
  128. return -EFAULT;
  129. return 0;
  130. case VIDIOCSFREQ:
  131. if(copy_from_user(&tea->freq, arg, sizeof(tea->freq)))
  132. return -EFAULT;
  133. snd_tea575x_set_freq(tea);
  134. return 0;
  135. case VIDIOCGAUDIO:
  136. {
  137. struct video_audio v;
  138. memset(&v, 0, sizeof(v));
  139. strcpy(v.name, "Radio");
  140. if(copy_to_user(arg,&v, sizeof(v)))
  141. return -EFAULT;
  142. return 0;
  143. }
  144. case VIDIOCSAUDIO:
  145. {
  146. struct video_audio v;
  147. if(copy_from_user(&v, arg, sizeof(v)))
  148. return -EFAULT;
  149. if (tea->ops->mute)
  150. tea->ops->mute(tea,
  151. (v.flags &
  152. VIDEO_AUDIO_MUTE) ? 1 : 0);
  153. if(v.audio)
  154. return -EINVAL;
  155. return 0;
  156. }
  157. default:
  158. return -ENOIOCTLCMD;
  159. }
  160. }
  161. static void snd_tea575x_release(struct video_device *vfd)
  162. {
  163. }
  164. static int snd_tea575x_exclusive_open(struct inode *inode, struct file *file)
  165. {
  166. struct snd_tea575x *tea = video_drvdata(file);
  167. return test_and_set_bit(0, &tea->in_use) ? -EBUSY : 0;
  168. }
  169. static int snd_tea575x_exclusive_release(struct inode *inode, struct file *file)
  170. {
  171. struct snd_tea575x *tea = video_drvdata(file);
  172. clear_bit(0, &tea->in_use);
  173. return 0;
  174. }
  175. /*
  176. * initialize all the tea575x chips
  177. */
  178. void snd_tea575x_init(struct snd_tea575x *tea)
  179. {
  180. unsigned int val;
  181. val = tea->ops->read(tea);
  182. if (val == 0x1ffffff || val == 0) {
  183. snd_printk(KERN_ERR "Cannot find TEA575x chip\n");
  184. return;
  185. }
  186. memset(&tea->vd, 0, sizeof(tea->vd));
  187. strcpy(tea->vd.name, tea->tea5759 ? "TEA5759 radio" : "TEA5757 radio");
  188. tea->vd.release = snd_tea575x_release;
  189. video_set_drvdata(&tea->vd, tea);
  190. tea->vd.fops = &tea->fops;
  191. tea->in_use = 0;
  192. tea->fops.owner = tea->card->module;
  193. tea->fops.open = snd_tea575x_exclusive_open;
  194. tea->fops.release = snd_tea575x_exclusive_release;
  195. tea->fops.ioctl = snd_tea575x_ioctl;
  196. if (video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->dev_nr - 1) < 0) {
  197. snd_printk(KERN_ERR "unable to register tea575x tuner\n");
  198. return;
  199. }
  200. tea->vd_registered = 1;
  201. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
  202. tea->freq = 90500 * 16; /* 90.5Mhz default */
  203. snd_tea575x_set_freq(tea);
  204. /* mute on init */
  205. if (tea->ops->mute)
  206. tea->ops->mute(tea, 1);
  207. }
  208. void snd_tea575x_exit(struct snd_tea575x *tea)
  209. {
  210. if (tea->vd_registered) {
  211. video_unregister_device(&tea->vd);
  212. tea->vd_registered = 0;
  213. }
  214. }
  215. static int __init alsa_tea575x_module_init(void)
  216. {
  217. return 0;
  218. }
  219. static void __exit alsa_tea575x_module_exit(void)
  220. {
  221. }
  222. module_init(alsa_tea575x_module_init)
  223. module_exit(alsa_tea575x_module_exit)
  224. EXPORT_SYMBOL(snd_tea575x_init);
  225. EXPORT_SYMBOL(snd_tea575x_exit);