tea575x-tuner.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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/sched.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-dev.h>
  30. #include <media/v4l2-fh.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-event.h>
  33. #include <sound/tea575x-tuner.h>
  34. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  35. MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
  36. MODULE_LICENSE("GPL");
  37. #define FREQ_LO (76U * 16000)
  38. #define FREQ_HI (108U * 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 u32 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 u32 snd_tea575x_get_freq(struct snd_tea575x *tea)
  108. {
  109. u32 freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK;
  110. if (freq == 0)
  111. return freq;
  112. /* freq *= 12.5 */
  113. freq *= 125;
  114. freq /= 10;
  115. /* crystal fixup */
  116. if (tea->tea5759)
  117. freq += TEA575X_FMIF;
  118. else
  119. freq -= TEA575X_FMIF;
  120. return clamp(freq * 16, FREQ_LO, FREQ_HI); /* from kHz */
  121. }
  122. static void snd_tea575x_set_freq(struct snd_tea575x *tea)
  123. {
  124. u32 freq = tea->freq;
  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, tea->v4l2_dev->name, 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->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  150. if (!tea->cannot_read_data)
  151. v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
  152. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  153. return 0;
  154. }
  155. static int vidioc_g_tuner(struct file *file, void *priv,
  156. struct v4l2_tuner *v)
  157. {
  158. struct snd_tea575x *tea = video_drvdata(file);
  159. if (v->index > 0)
  160. return -EINVAL;
  161. snd_tea575x_read(tea);
  162. strcpy(v->name, "FM");
  163. v->type = V4L2_TUNER_RADIO;
  164. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  165. v->rangelow = FREQ_LO;
  166. v->rangehigh = FREQ_HI;
  167. v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  168. v->audmode = (tea->val & TEA575X_BIT_MONO) ?
  169. V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
  170. v->signal = tea->tuned ? 0xffff : 0;
  171. return 0;
  172. }
  173. static int vidioc_s_tuner(struct file *file, void *priv,
  174. struct v4l2_tuner *v)
  175. {
  176. struct snd_tea575x *tea = video_drvdata(file);
  177. if (v->index)
  178. return -EINVAL;
  179. tea->val &= ~TEA575X_BIT_MONO;
  180. if (v->audmode == V4L2_TUNER_MODE_MONO)
  181. tea->val |= TEA575X_BIT_MONO;
  182. snd_tea575x_write(tea, tea->val);
  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. f->frequency = tea->freq;
  193. return 0;
  194. }
  195. static int vidioc_s_frequency(struct file *file, void *priv,
  196. struct v4l2_frequency *f)
  197. {
  198. struct snd_tea575x *tea = video_drvdata(file);
  199. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  200. return -EINVAL;
  201. tea->val &= ~TEA575X_BIT_SEARCH;
  202. tea->freq = clamp(f->frequency, FREQ_LO, FREQ_HI);
  203. snd_tea575x_set_freq(tea);
  204. return 0;
  205. }
  206. static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
  207. struct v4l2_hw_freq_seek *a)
  208. {
  209. struct snd_tea575x *tea = video_drvdata(file);
  210. unsigned long timeout;
  211. int i;
  212. if (tea->cannot_read_data)
  213. return -ENOTTY;
  214. if (a->tuner || a->wrap_around)
  215. return -EINVAL;
  216. /* clear the frequency, HW will fill it in */
  217. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  218. tea->val |= TEA575X_BIT_SEARCH;
  219. if (a->seek_upward)
  220. tea->val |= TEA575X_BIT_UPDOWN;
  221. else
  222. tea->val &= ~TEA575X_BIT_UPDOWN;
  223. snd_tea575x_write(tea, tea->val);
  224. timeout = jiffies + msecs_to_jiffies(10000);
  225. for (;;) {
  226. if (time_after(jiffies, timeout))
  227. break;
  228. if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
  229. /* some signal arrived, stop search */
  230. tea->val &= ~TEA575X_BIT_SEARCH;
  231. snd_tea575x_set_freq(tea);
  232. return -ERESTARTSYS;
  233. }
  234. if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
  235. u32 freq;
  236. /* Found a frequency, wait until it can be read */
  237. for (i = 0; i < 100; i++) {
  238. msleep(10);
  239. freq = snd_tea575x_get_freq(tea);
  240. if (freq) /* available */
  241. break;
  242. }
  243. if (freq == 0) /* shouldn't happen */
  244. break;
  245. /*
  246. * if we moved by less than 50 kHz, or in the wrong
  247. * direction, continue seeking
  248. */
  249. if (abs(tea->freq - freq) < 16 * 50 ||
  250. (a->seek_upward && freq < tea->freq) ||
  251. (!a->seek_upward && freq > tea->freq)) {
  252. snd_tea575x_write(tea, tea->val);
  253. continue;
  254. }
  255. tea->freq = freq;
  256. tea->val &= ~TEA575X_BIT_SEARCH;
  257. return 0;
  258. }
  259. }
  260. tea->val &= ~TEA575X_BIT_SEARCH;
  261. snd_tea575x_set_freq(tea);
  262. return -EAGAIN;
  263. }
  264. static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
  265. {
  266. struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
  267. switch (ctrl->id) {
  268. case V4L2_CID_AUDIO_MUTE:
  269. tea->mute = ctrl->val;
  270. snd_tea575x_set_freq(tea);
  271. return 0;
  272. }
  273. return -EINVAL;
  274. }
  275. static const struct v4l2_file_operations tea575x_fops = {
  276. .owner = THIS_MODULE,
  277. .unlocked_ioctl = video_ioctl2,
  278. .open = v4l2_fh_open,
  279. .release = v4l2_fh_release,
  280. .poll = v4l2_ctrl_poll,
  281. };
  282. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  283. .vidioc_querycap = vidioc_querycap,
  284. .vidioc_g_tuner = vidioc_g_tuner,
  285. .vidioc_s_tuner = vidioc_s_tuner,
  286. .vidioc_g_frequency = vidioc_g_frequency,
  287. .vidioc_s_frequency = vidioc_s_frequency,
  288. .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
  289. .vidioc_log_status = v4l2_ctrl_log_status,
  290. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  291. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  292. };
  293. static const struct video_device tea575x_radio = {
  294. .fops = &tea575x_fops,
  295. .ioctl_ops = &tea575x_ioctl_ops,
  296. .release = video_device_release_empty,
  297. };
  298. static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
  299. .s_ctrl = tea575x_s_ctrl,
  300. };
  301. /*
  302. * initialize all the tea575x chips
  303. */
  304. int snd_tea575x_init(struct snd_tea575x *tea)
  305. {
  306. int retval;
  307. tea->mute = true;
  308. /* Not all devices can or know how to read the data back.
  309. Such devices can set cannot_read_data to true. */
  310. if (!tea->cannot_read_data) {
  311. snd_tea575x_write(tea, 0x55AA);
  312. if (snd_tea575x_read(tea) != 0x55AA)
  313. return -ENODEV;
  314. }
  315. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
  316. tea->freq = 90500 * 16; /* 90.5Mhz default */
  317. snd_tea575x_set_freq(tea);
  318. tea->vd = tea575x_radio;
  319. video_set_drvdata(&tea->vd, tea);
  320. mutex_init(&tea->mutex);
  321. strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
  322. tea->vd.lock = &tea->mutex;
  323. tea->vd.v4l2_dev = tea->v4l2_dev;
  324. tea->vd.ctrl_handler = &tea->ctrl_handler;
  325. set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
  326. v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
  327. v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  328. retval = tea->ctrl_handler.error;
  329. if (retval) {
  330. v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
  331. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  332. return retval;
  333. }
  334. if (tea->ext_init) {
  335. retval = tea->ext_init(tea);
  336. if (retval) {
  337. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  338. return retval;
  339. }
  340. }
  341. v4l2_ctrl_handler_setup(&tea->ctrl_handler);
  342. retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
  343. if (retval) {
  344. v4l2_err(tea->v4l2_dev, "can't register video device!\n");
  345. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  346. return retval;
  347. }
  348. return 0;
  349. }
  350. void snd_tea575x_exit(struct snd_tea575x *tea)
  351. {
  352. video_unregister_device(&tea->vd);
  353. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  354. }
  355. static int __init alsa_tea575x_module_init(void)
  356. {
  357. return 0;
  358. }
  359. static void __exit alsa_tea575x_module_exit(void)
  360. {
  361. }
  362. module_init(alsa_tea575x_module_init)
  363. module_exit(alsa_tea575x_module_exit)
  364. EXPORT_SYMBOL(snd_tea575x_init);
  365. EXPORT_SYMBOL(snd_tea575x_exit);