tea575x-tuner.c 10 KB

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