tea575x-tuner.c 9.2 KB

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