tea575x-tuner.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <linux/version.h>
  27. #include <sound/core.h>
  28. #include <sound/tea575x-tuner.h>
  29. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  30. MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
  31. MODULE_LICENSE("GPL");
  32. static int radio_nr = -1;
  33. module_param(radio_nr, int, 0);
  34. #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
  35. #define FREQ_LO (87 * 16000)
  36. #define FREQ_HI (108 * 16000)
  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 (1<<21)
  47. #define TEA575X_BIT_BAND_SW (1<<22)
  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. static struct v4l2_queryctrl radio_qctrl[] = {
  58. {
  59. .id = V4L2_CID_AUDIO_MUTE,
  60. .name = "Mute",
  61. .minimum = 0,
  62. .maximum = 1,
  63. .default_value = 1,
  64. .type = V4L2_CTRL_TYPE_BOOLEAN,
  65. }
  66. };
  67. /*
  68. * lowlevel part
  69. */
  70. static void snd_tea575x_set_freq(struct snd_tea575x *tea)
  71. {
  72. unsigned long freq;
  73. freq = tea->freq / 16; /* to kHz */
  74. if (freq > 108000)
  75. freq = 108000;
  76. if (freq < 87000)
  77. freq = 87000;
  78. /* crystal fixup */
  79. if (tea->tea5759)
  80. freq -= tea->freq_fixup;
  81. else
  82. freq += tea->freq_fixup;
  83. /* freq /= 12.5 */
  84. freq *= 10;
  85. freq /= 125;
  86. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  87. tea->val |= freq & TEA575X_BIT_FREQ_MASK;
  88. tea->ops->write(tea, tea->val);
  89. }
  90. /*
  91. * Linux Video interface
  92. */
  93. static int vidioc_querycap(struct file *file, void *priv,
  94. struct v4l2_capability *v)
  95. {
  96. struct snd_tea575x *tea = video_drvdata(file);
  97. strcpy(v->card, tea->tea5759 ? "TEA5759" : "TEA5757");
  98. strlcpy(v->driver, "tea575x-tuner", sizeof(v->driver));
  99. strlcpy(v->card, "Maestro Radio", sizeof(v->card));
  100. sprintf(v->bus_info, "PCI");
  101. v->version = RADIO_VERSION;
  102. v->capabilities = V4L2_CAP_TUNER;
  103. return 0;
  104. }
  105. static int vidioc_g_tuner(struct file *file, void *priv,
  106. struct v4l2_tuner *v)
  107. {
  108. if (v->index > 0)
  109. return -EINVAL;
  110. strcpy(v->name, "FM");
  111. v->type = V4L2_TUNER_RADIO;
  112. v->rangelow = FREQ_LO;
  113. v->rangehigh = FREQ_HI;
  114. v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  115. v->capability = V4L2_TUNER_CAP_LOW;
  116. v->audmode = V4L2_TUNER_MODE_MONO;
  117. v->signal = 0xffff;
  118. return 0;
  119. }
  120. static int vidioc_s_tuner(struct file *file, void *priv,
  121. struct v4l2_tuner *v)
  122. {
  123. if (v->index > 0)
  124. return -EINVAL;
  125. return 0;
  126. }
  127. static int vidioc_g_frequency(struct file *file, void *priv,
  128. struct v4l2_frequency *f)
  129. {
  130. struct snd_tea575x *tea = video_drvdata(file);
  131. f->type = V4L2_TUNER_RADIO;
  132. f->frequency = tea->freq;
  133. return 0;
  134. }
  135. static int vidioc_s_frequency(struct file *file, void *priv,
  136. struct v4l2_frequency *f)
  137. {
  138. struct snd_tea575x *tea = video_drvdata(file);
  139. if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
  140. return -EINVAL;
  141. tea->freq = f->frequency;
  142. snd_tea575x_set_freq(tea);
  143. return 0;
  144. }
  145. static int vidioc_g_audio(struct file *file, void *priv,
  146. struct v4l2_audio *a)
  147. {
  148. if (a->index > 1)
  149. return -EINVAL;
  150. strcpy(a->name, "Radio");
  151. a->capability = V4L2_AUDCAP_STEREO;
  152. return 0;
  153. }
  154. static int vidioc_s_audio(struct file *file, void *priv,
  155. struct v4l2_audio *a)
  156. {
  157. if (a->index != 0)
  158. return -EINVAL;
  159. return 0;
  160. }
  161. static int vidioc_queryctrl(struct file *file, void *priv,
  162. struct v4l2_queryctrl *qc)
  163. {
  164. int i;
  165. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  166. if (qc->id && qc->id == radio_qctrl[i].id) {
  167. memcpy(qc, &(radio_qctrl[i]),
  168. sizeof(*qc));
  169. return 0;
  170. }
  171. }
  172. return -EINVAL;
  173. }
  174. static int vidioc_g_ctrl(struct file *file, void *priv,
  175. struct v4l2_control *ctrl)
  176. {
  177. struct snd_tea575x *tea = video_drvdata(file);
  178. switch (ctrl->id) {
  179. case V4L2_CID_AUDIO_MUTE:
  180. if (tea->ops->mute) {
  181. ctrl->value = tea->mute;
  182. return 0;
  183. }
  184. }
  185. return -EINVAL;
  186. }
  187. static int vidioc_s_ctrl(struct file *file, void *priv,
  188. struct v4l2_control *ctrl)
  189. {
  190. struct snd_tea575x *tea = video_drvdata(file);
  191. switch (ctrl->id) {
  192. case V4L2_CID_AUDIO_MUTE:
  193. if (tea->ops->mute) {
  194. tea->ops->mute(tea, ctrl->value);
  195. tea->mute = 1;
  196. return 0;
  197. }
  198. }
  199. return -EINVAL;
  200. }
  201. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  202. {
  203. *i = 0;
  204. return 0;
  205. }
  206. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  207. {
  208. if (i != 0)
  209. return -EINVAL;
  210. return 0;
  211. }
  212. static int snd_tea575x_exclusive_open(struct file *file)
  213. {
  214. struct snd_tea575x *tea = video_drvdata(file);
  215. return test_and_set_bit(0, &tea->in_use) ? -EBUSY : 0;
  216. }
  217. static int snd_tea575x_exclusive_release(struct file *file)
  218. {
  219. struct snd_tea575x *tea = video_drvdata(file);
  220. clear_bit(0, &tea->in_use);
  221. return 0;
  222. }
  223. static const struct v4l2_file_operations tea575x_fops = {
  224. .owner = THIS_MODULE,
  225. .open = snd_tea575x_exclusive_open,
  226. .release = snd_tea575x_exclusive_release,
  227. .ioctl = video_ioctl2,
  228. };
  229. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  230. .vidioc_querycap = vidioc_querycap,
  231. .vidioc_g_tuner = vidioc_g_tuner,
  232. .vidioc_s_tuner = vidioc_s_tuner,
  233. .vidioc_g_audio = vidioc_g_audio,
  234. .vidioc_s_audio = vidioc_s_audio,
  235. .vidioc_g_input = vidioc_g_input,
  236. .vidioc_s_input = vidioc_s_input,
  237. .vidioc_g_frequency = vidioc_g_frequency,
  238. .vidioc_s_frequency = vidioc_s_frequency,
  239. .vidioc_queryctrl = vidioc_queryctrl,
  240. .vidioc_g_ctrl = vidioc_g_ctrl,
  241. .vidioc_s_ctrl = vidioc_s_ctrl,
  242. };
  243. static struct video_device tea575x_radio = {
  244. .name = "tea575x-tuner",
  245. .fops = &tea575x_fops,
  246. .ioctl_ops = &tea575x_ioctl_ops,
  247. .release = video_device_release,
  248. };
  249. /*
  250. * initialize all the tea575x chips
  251. */
  252. void snd_tea575x_init(struct snd_tea575x *tea)
  253. {
  254. int retval;
  255. unsigned int val;
  256. struct video_device *tea575x_radio_inst;
  257. val = tea->ops->read(tea);
  258. if (val == 0x1ffffff || val == 0) {
  259. snd_printk(KERN_ERR
  260. "tea575x-tuner: Cannot find TEA575x chip\n");
  261. return;
  262. }
  263. tea->in_use = 0;
  264. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
  265. tea->freq = 90500 * 16; /* 90.5Mhz default */
  266. tea575x_radio_inst = video_device_alloc();
  267. if (tea575x_radio_inst == NULL) {
  268. printk(KERN_ERR "tea575x-tuner: not enough memory\n");
  269. return;
  270. }
  271. memcpy(tea575x_radio_inst, &tea575x_radio, sizeof(tea575x_radio));
  272. strcpy(tea575x_radio.name, tea->tea5759 ?
  273. "TEA5759 radio" : "TEA5757 radio");
  274. video_set_drvdata(tea575x_radio_inst, tea);
  275. retval = video_register_device(tea575x_radio_inst,
  276. VFL_TYPE_RADIO, radio_nr);
  277. if (retval) {
  278. printk(KERN_ERR "tea575x-tuner: can't register video device!\n");
  279. kfree(tea575x_radio_inst);
  280. return;
  281. }
  282. snd_tea575x_set_freq(tea);
  283. /* mute on init */
  284. if (tea->ops->mute) {
  285. tea->ops->mute(tea, 1);
  286. tea->mute = 1;
  287. }
  288. tea->vd = tea575x_radio_inst;
  289. }
  290. void snd_tea575x_exit(struct snd_tea575x *tea)
  291. {
  292. if (tea->vd) {
  293. video_unregister_device(tea->vd);
  294. tea->vd = NULL;
  295. }
  296. }
  297. static int __init alsa_tea575x_module_init(void)
  298. {
  299. return 0;
  300. }
  301. static void __exit alsa_tea575x_module_exit(void)
  302. {
  303. }
  304. module_init(alsa_tea575x_module_init)
  305. module_exit(alsa_tea575x_module_exit)
  306. EXPORT_SYMBOL(snd_tea575x_init);
  307. EXPORT_SYMBOL(snd_tea575x_exit);