tea575x-tuner.c 8.6 KB

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