tea575x-tuner.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/version.h>
  27. #include <media/v4l2-dev.h>
  28. #include <media/v4l2-ioctl.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. /*
  59. * lowlevel part
  60. */
  61. static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
  62. {
  63. u16 l;
  64. u8 data;
  65. tea->ops->set_direction(tea, 1);
  66. udelay(16);
  67. for (l = 25; l > 0; l--) {
  68. data = (val >> 24) & TEA575X_DATA;
  69. val <<= 1; /* shift data */
  70. tea->ops->set_pins(tea, data | TEA575X_WREN);
  71. udelay(2);
  72. tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
  73. udelay(2);
  74. tea->ops->set_pins(tea, data | TEA575X_WREN);
  75. udelay(2);
  76. }
  77. if (!tea->mute)
  78. tea->ops->set_pins(tea, 0);
  79. }
  80. static unsigned int snd_tea575x_read(struct snd_tea575x *tea)
  81. {
  82. u16 l, rdata;
  83. u32 data = 0;
  84. tea->ops->set_direction(tea, 0);
  85. tea->ops->set_pins(tea, 0);
  86. udelay(16);
  87. for (l = 24; l--;) {
  88. tea->ops->set_pins(tea, TEA575X_CLK);
  89. udelay(2);
  90. if (!l)
  91. tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
  92. tea->ops->set_pins(tea, 0);
  93. udelay(2);
  94. data <<= 1; /* shift data */
  95. rdata = tea->ops->get_pins(tea);
  96. if (!l)
  97. tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
  98. if (rdata & TEA575X_DATA)
  99. data++;
  100. udelay(2);
  101. }
  102. if (tea->mute)
  103. tea->ops->set_pins(tea, TEA575X_WREN);
  104. return data;
  105. }
  106. static void snd_tea575x_get_freq(struct snd_tea575x *tea)
  107. {
  108. unsigned long freq;
  109. freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK;
  110. /* freq *= 12.5 */
  111. freq *= 125;
  112. freq /= 10;
  113. /* crystal fixup */
  114. if (tea->tea5759)
  115. freq += TEA575X_FMIF;
  116. else
  117. freq -= TEA575X_FMIF;
  118. tea->freq = freq * 16; /* from kHz */
  119. }
  120. static void snd_tea575x_set_freq(struct snd_tea575x *tea)
  121. {
  122. unsigned long freq;
  123. freq = clamp(tea->freq, FREQ_LO, FREQ_HI);
  124. freq /= 16; /* to kHz */
  125. /* crystal fixup */
  126. if (tea->tea5759)
  127. freq -= TEA575X_FMIF;
  128. else
  129. freq += TEA575X_FMIF;
  130. /* freq /= 12.5 */
  131. freq *= 10;
  132. freq /= 125;
  133. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  134. tea->val |= freq & TEA575X_BIT_FREQ_MASK;
  135. snd_tea575x_write(tea, tea->val);
  136. }
  137. /*
  138. * Linux Video interface
  139. */
  140. static int vidioc_querycap(struct file *file, void *priv,
  141. struct v4l2_capability *v)
  142. {
  143. struct snd_tea575x *tea = video_drvdata(file);
  144. strlcpy(v->driver, "tea575x-tuner", sizeof(v->driver));
  145. strlcpy(v->card, tea->card, sizeof(v->card));
  146. strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
  147. strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
  148. v->version = RADIO_VERSION;
  149. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  150. return 0;
  151. }
  152. static int vidioc_g_tuner(struct file *file, void *priv,
  153. struct v4l2_tuner *v)
  154. {
  155. struct snd_tea575x *tea = video_drvdata(file);
  156. if (v->index > 0)
  157. return -EINVAL;
  158. snd_tea575x_read(tea);
  159. strcpy(v->name, "FM");
  160. v->type = V4L2_TUNER_RADIO;
  161. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  162. v->rangelow = FREQ_LO;
  163. v->rangehigh = FREQ_HI;
  164. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  165. v->audmode = tea->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
  166. v->signal = tea->tuned ? 0xffff : 0;
  167. return 0;
  168. }
  169. static int vidioc_s_tuner(struct file *file, void *priv,
  170. struct v4l2_tuner *v)
  171. {
  172. if (v->index > 0)
  173. return -EINVAL;
  174. return 0;
  175. }
  176. static int vidioc_g_frequency(struct file *file, void *priv,
  177. struct v4l2_frequency *f)
  178. {
  179. struct snd_tea575x *tea = video_drvdata(file);
  180. if (f->tuner != 0)
  181. return -EINVAL;
  182. f->type = V4L2_TUNER_RADIO;
  183. snd_tea575x_get_freq(tea);
  184. f->frequency = tea->freq;
  185. return 0;
  186. }
  187. static int vidioc_s_frequency(struct file *file, void *priv,
  188. struct v4l2_frequency *f)
  189. {
  190. struct snd_tea575x *tea = video_drvdata(file);
  191. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  192. return -EINVAL;
  193. if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
  194. return -EINVAL;
  195. tea->freq = f->frequency;
  196. snd_tea575x_set_freq(tea);
  197. return 0;
  198. }
  199. static int vidioc_g_audio(struct file *file, void *priv,
  200. struct v4l2_audio *a)
  201. {
  202. if (a->index > 1)
  203. return -EINVAL;
  204. strcpy(a->name, "Radio");
  205. a->capability = V4L2_AUDCAP_STEREO;
  206. return 0;
  207. }
  208. static int vidioc_s_audio(struct file *file, void *priv,
  209. struct v4l2_audio *a)
  210. {
  211. if (a->index != 0)
  212. return -EINVAL;
  213. return 0;
  214. }
  215. static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
  216. {
  217. struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
  218. switch (ctrl->id) {
  219. case V4L2_CID_AUDIO_MUTE:
  220. tea->mute = ctrl->val;
  221. snd_tea575x_set_freq(tea);
  222. return 0;
  223. }
  224. return -EINVAL;
  225. }
  226. static const struct v4l2_file_operations tea575x_fops = {
  227. .owner = THIS_MODULE,
  228. .unlocked_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_frequency = vidioc_g_frequency,
  237. .vidioc_s_frequency = vidioc_s_frequency,
  238. };
  239. static struct video_device tea575x_radio = {
  240. .name = "tea575x-tuner",
  241. .fops = &tea575x_fops,
  242. .ioctl_ops = &tea575x_ioctl_ops,
  243. .release = video_device_release_empty,
  244. };
  245. static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
  246. .s_ctrl = tea575x_s_ctrl,
  247. };
  248. /*
  249. * initialize all the tea575x chips
  250. */
  251. int snd_tea575x_init(struct snd_tea575x *tea)
  252. {
  253. int retval;
  254. tea->mute = 1;
  255. snd_tea575x_write(tea, 0x55AA);
  256. if (snd_tea575x_read(tea) != 0x55AA)
  257. return -ENODEV;
  258. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
  259. tea->freq = 90500 * 16; /* 90.5Mhz default */
  260. snd_tea575x_set_freq(tea);
  261. tea->vd = tea575x_radio;
  262. video_set_drvdata(&tea->vd, tea);
  263. mutex_init(&tea->mutex);
  264. tea->vd.lock = &tea->mutex;
  265. v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
  266. tea->vd.ctrl_handler = &tea->ctrl_handler;
  267. v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  268. retval = tea->ctrl_handler.error;
  269. if (retval) {
  270. printk(KERN_ERR "tea575x-tuner: can't initialize controls\n");
  271. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  272. return retval;
  273. }
  274. if (tea->ext_init) {
  275. retval = tea->ext_init(tea);
  276. if (retval) {
  277. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  278. return retval;
  279. }
  280. }
  281. v4l2_ctrl_handler_setup(&tea->ctrl_handler);
  282. retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, radio_nr);
  283. if (retval) {
  284. printk(KERN_ERR "tea575x-tuner: can't register video device!\n");
  285. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  286. return retval;
  287. }
  288. return 0;
  289. }
  290. void snd_tea575x_exit(struct snd_tea575x *tea)
  291. {
  292. video_unregister_device(&tea->vd);
  293. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  294. }
  295. static int __init alsa_tea575x_module_init(void)
  296. {
  297. return 0;
  298. }
  299. static void __exit alsa_tea575x_module_exit(void)
  300. {
  301. }
  302. module_init(alsa_tea575x_module_init)
  303. module_exit(alsa_tea575x_module_exit)
  304. EXPORT_SYMBOL(snd_tea575x_init);
  305. EXPORT_SYMBOL(snd_tea575x_exit);