tea575x-tuner.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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->card, sizeof(v->card));
  156. strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
  157. strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
  158. v->version = RADIO_VERSION;
  159. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  160. return 0;
  161. }
  162. static int vidioc_g_tuner(struct file *file, void *priv,
  163. struct v4l2_tuner *v)
  164. {
  165. struct snd_tea575x *tea = video_drvdata(file);
  166. if (v->index > 0)
  167. return -EINVAL;
  168. snd_tea575x_read(tea);
  169. strcpy(v->name, "FM");
  170. v->type = V4L2_TUNER_RADIO;
  171. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  172. v->rangelow = FREQ_LO;
  173. v->rangehigh = FREQ_HI;
  174. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  175. v->audmode = tea->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
  176. v->signal = tea->tuned ? 0xffff : 0;
  177. return 0;
  178. }
  179. static int vidioc_s_tuner(struct file *file, void *priv,
  180. struct v4l2_tuner *v)
  181. {
  182. if (v->index > 0)
  183. return -EINVAL;
  184. return 0;
  185. }
  186. static int vidioc_g_frequency(struct file *file, void *priv,
  187. struct v4l2_frequency *f)
  188. {
  189. struct snd_tea575x *tea = video_drvdata(file);
  190. if (f->tuner != 0)
  191. return -EINVAL;
  192. f->type = V4L2_TUNER_RADIO;
  193. snd_tea575x_get_freq(tea);
  194. f->frequency = tea->freq;
  195. return 0;
  196. }
  197. static int vidioc_s_frequency(struct file *file, void *priv,
  198. struct v4l2_frequency *f)
  199. {
  200. struct snd_tea575x *tea = video_drvdata(file);
  201. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  202. return -EINVAL;
  203. if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
  204. return -EINVAL;
  205. tea->freq = f->frequency;
  206. snd_tea575x_set_freq(tea);
  207. return 0;
  208. }
  209. static int vidioc_g_audio(struct file *file, void *priv,
  210. struct v4l2_audio *a)
  211. {
  212. if (a->index > 1)
  213. return -EINVAL;
  214. strcpy(a->name, "Radio");
  215. a->capability = V4L2_AUDCAP_STEREO;
  216. return 0;
  217. }
  218. static int vidioc_s_audio(struct file *file, void *priv,
  219. struct v4l2_audio *a)
  220. {
  221. if (a->index != 0)
  222. return -EINVAL;
  223. return 0;
  224. }
  225. static int vidioc_queryctrl(struct file *file, void *priv,
  226. struct v4l2_queryctrl *qc)
  227. {
  228. int i;
  229. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  230. if (qc->id && qc->id == radio_qctrl[i].id) {
  231. memcpy(qc, &(radio_qctrl[i]),
  232. sizeof(*qc));
  233. return 0;
  234. }
  235. }
  236. return -EINVAL;
  237. }
  238. static int vidioc_g_ctrl(struct file *file, void *priv,
  239. struct v4l2_control *ctrl)
  240. {
  241. struct snd_tea575x *tea = video_drvdata(file);
  242. switch (ctrl->id) {
  243. case V4L2_CID_AUDIO_MUTE:
  244. ctrl->value = tea->mute;
  245. return 0;
  246. }
  247. return -EINVAL;
  248. }
  249. static int vidioc_s_ctrl(struct file *file, void *priv,
  250. struct v4l2_control *ctrl)
  251. {
  252. struct snd_tea575x *tea = video_drvdata(file);
  253. switch (ctrl->id) {
  254. case V4L2_CID_AUDIO_MUTE:
  255. if (tea->mute != ctrl->value) {
  256. tea->mute = ctrl->value;
  257. snd_tea575x_set_freq(tea);
  258. }
  259. return 0;
  260. }
  261. return -EINVAL;
  262. }
  263. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  264. {
  265. *i = 0;
  266. return 0;
  267. }
  268. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  269. {
  270. if (i != 0)
  271. return -EINVAL;
  272. return 0;
  273. }
  274. static int snd_tea575x_exclusive_open(struct file *file)
  275. {
  276. struct snd_tea575x *tea = video_drvdata(file);
  277. return test_and_set_bit(0, &tea->in_use) ? -EBUSY : 0;
  278. }
  279. static int snd_tea575x_exclusive_release(struct file *file)
  280. {
  281. struct snd_tea575x *tea = video_drvdata(file);
  282. clear_bit(0, &tea->in_use);
  283. return 0;
  284. }
  285. static const struct v4l2_file_operations tea575x_fops = {
  286. .owner = THIS_MODULE,
  287. .open = snd_tea575x_exclusive_open,
  288. .release = snd_tea575x_exclusive_release,
  289. .ioctl = video_ioctl2,
  290. };
  291. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  292. .vidioc_querycap = vidioc_querycap,
  293. .vidioc_g_tuner = vidioc_g_tuner,
  294. .vidioc_s_tuner = vidioc_s_tuner,
  295. .vidioc_g_audio = vidioc_g_audio,
  296. .vidioc_s_audio = vidioc_s_audio,
  297. .vidioc_g_input = vidioc_g_input,
  298. .vidioc_s_input = vidioc_s_input,
  299. .vidioc_g_frequency = vidioc_g_frequency,
  300. .vidioc_s_frequency = vidioc_s_frequency,
  301. .vidioc_queryctrl = vidioc_queryctrl,
  302. .vidioc_g_ctrl = vidioc_g_ctrl,
  303. .vidioc_s_ctrl = vidioc_s_ctrl,
  304. };
  305. static struct video_device tea575x_radio = {
  306. .name = "tea575x-tuner",
  307. .fops = &tea575x_fops,
  308. .ioctl_ops = &tea575x_ioctl_ops,
  309. .release = video_device_release,
  310. };
  311. /*
  312. * initialize all the tea575x chips
  313. */
  314. int snd_tea575x_init(struct snd_tea575x *tea)
  315. {
  316. int retval;
  317. struct video_device *tea575x_radio_inst;
  318. tea->mute = 1;
  319. snd_tea575x_write(tea, 0x55AA);
  320. if (snd_tea575x_read(tea) != 0x55AA)
  321. return -ENODEV;
  322. tea->in_use = 0;
  323. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
  324. tea->freq = 90500 * 16; /* 90.5Mhz default */
  325. tea575x_radio_inst = video_device_alloc();
  326. if (tea575x_radio_inst == NULL) {
  327. printk(KERN_ERR "tea575x-tuner: not enough memory\n");
  328. return -ENOMEM;
  329. }
  330. memcpy(tea575x_radio_inst, &tea575x_radio, sizeof(tea575x_radio));
  331. strcpy(tea575x_radio.name, tea->tea5759 ?
  332. "TEA5759 radio" : "TEA5757 radio");
  333. video_set_drvdata(tea575x_radio_inst, tea);
  334. retval = video_register_device(tea575x_radio_inst,
  335. VFL_TYPE_RADIO, radio_nr);
  336. if (retval) {
  337. printk(KERN_ERR "tea575x-tuner: can't register video device!\n");
  338. kfree(tea575x_radio_inst);
  339. return retval;
  340. }
  341. snd_tea575x_set_freq(tea);
  342. tea->vd = tea575x_radio_inst;
  343. return 0;
  344. }
  345. void snd_tea575x_exit(struct snd_tea575x *tea)
  346. {
  347. if (tea->vd) {
  348. video_unregister_device(tea->vd);
  349. tea->vd = NULL;
  350. }
  351. }
  352. static int __init alsa_tea575x_module_init(void)
  353. {
  354. return 0;
  355. }
  356. static void __exit alsa_tea575x_module_exit(void)
  357. {
  358. }
  359. module_init(alsa_tea575x_module_init)
  360. module_exit(alsa_tea575x_module_exit)
  361. EXPORT_SYMBOL(snd_tea575x_init);
  362. EXPORT_SYMBOL(snd_tea575x_exit);