tea575x-tuner.c 8.9 KB

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