tea575x-tuner.c 6.0 KB

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