tea575x-tuner.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. if (!tea->cannot_read_data)
  166. v->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
  167. v->rangelow = FREQ_LO;
  168. v->rangehigh = FREQ_HI;
  169. v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  170. v->audmode = (tea->val & TEA575X_BIT_MONO) ?
  171. V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
  172. v->signal = tea->tuned ? 0xffff : 0;
  173. return 0;
  174. }
  175. static int vidioc_s_tuner(struct file *file, void *priv,
  176. struct v4l2_tuner *v)
  177. {
  178. struct snd_tea575x *tea = video_drvdata(file);
  179. if (v->index)
  180. return -EINVAL;
  181. tea->val &= ~TEA575X_BIT_MONO;
  182. if (v->audmode == V4L2_TUNER_MODE_MONO)
  183. tea->val |= TEA575X_BIT_MONO;
  184. snd_tea575x_write(tea, tea->val);
  185. return 0;
  186. }
  187. static int vidioc_g_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)
  192. return -EINVAL;
  193. f->type = V4L2_TUNER_RADIO;
  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. tea->val &= ~TEA575X_BIT_SEARCH;
  204. tea->freq = clamp(f->frequency, FREQ_LO, FREQ_HI);
  205. snd_tea575x_set_freq(tea);
  206. return 0;
  207. }
  208. static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
  209. struct v4l2_hw_freq_seek *a)
  210. {
  211. struct snd_tea575x *tea = video_drvdata(file);
  212. unsigned long timeout;
  213. int i;
  214. if (tea->cannot_read_data)
  215. return -ENOTTY;
  216. if (a->tuner || a->wrap_around)
  217. return -EINVAL;
  218. /* clear the frequency, HW will fill it in */
  219. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  220. tea->val |= TEA575X_BIT_SEARCH;
  221. if (a->seek_upward)
  222. tea->val |= TEA575X_BIT_UPDOWN;
  223. else
  224. tea->val &= ~TEA575X_BIT_UPDOWN;
  225. snd_tea575x_write(tea, tea->val);
  226. timeout = jiffies + msecs_to_jiffies(10000);
  227. for (;;) {
  228. if (time_after(jiffies, timeout))
  229. break;
  230. if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
  231. /* some signal arrived, stop search */
  232. tea->val &= ~TEA575X_BIT_SEARCH;
  233. snd_tea575x_set_freq(tea);
  234. return -ERESTARTSYS;
  235. }
  236. if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
  237. u32 freq;
  238. /* Found a frequency, wait until it can be read */
  239. for (i = 0; i < 100; i++) {
  240. msleep(10);
  241. freq = snd_tea575x_get_freq(tea);
  242. if (freq) /* available */
  243. break;
  244. }
  245. if (freq == 0) /* shouldn't happen */
  246. break;
  247. /*
  248. * if we moved by less than 50 kHz, or in the wrong
  249. * direction, continue seeking
  250. */
  251. if (abs(tea->freq - freq) < 16 * 50 ||
  252. (a->seek_upward && freq < tea->freq) ||
  253. (!a->seek_upward && freq > tea->freq)) {
  254. snd_tea575x_write(tea, tea->val);
  255. continue;
  256. }
  257. tea->freq = freq;
  258. tea->val &= ~TEA575X_BIT_SEARCH;
  259. return 0;
  260. }
  261. }
  262. tea->val &= ~TEA575X_BIT_SEARCH;
  263. snd_tea575x_set_freq(tea);
  264. return -ENODATA;
  265. }
  266. static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
  267. {
  268. struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
  269. switch (ctrl->id) {
  270. case V4L2_CID_AUDIO_MUTE:
  271. tea->mute = ctrl->val;
  272. snd_tea575x_set_freq(tea);
  273. return 0;
  274. }
  275. return -EINVAL;
  276. }
  277. static const struct v4l2_file_operations tea575x_fops = {
  278. .owner = THIS_MODULE,
  279. .unlocked_ioctl = video_ioctl2,
  280. .open = v4l2_fh_open,
  281. .release = v4l2_fh_release,
  282. .poll = v4l2_ctrl_poll,
  283. };
  284. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  285. .vidioc_querycap = vidioc_querycap,
  286. .vidioc_g_tuner = vidioc_g_tuner,
  287. .vidioc_s_tuner = vidioc_s_tuner,
  288. .vidioc_g_frequency = vidioc_g_frequency,
  289. .vidioc_s_frequency = vidioc_s_frequency,
  290. .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
  291. .vidioc_log_status = v4l2_ctrl_log_status,
  292. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  293. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  294. };
  295. static const struct video_device tea575x_radio = {
  296. .fops = &tea575x_fops,
  297. .ioctl_ops = &tea575x_ioctl_ops,
  298. .release = video_device_release_empty,
  299. };
  300. static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
  301. .s_ctrl = tea575x_s_ctrl,
  302. };
  303. /*
  304. * initialize all the tea575x chips
  305. */
  306. int snd_tea575x_init(struct snd_tea575x *tea)
  307. {
  308. int retval;
  309. tea->mute = true;
  310. /* Not all devices can or know how to read the data back.
  311. Such devices can set cannot_read_data to true. */
  312. if (!tea->cannot_read_data) {
  313. snd_tea575x_write(tea, 0x55AA);
  314. if (snd_tea575x_read(tea) != 0x55AA)
  315. return -ENODEV;
  316. }
  317. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
  318. tea->freq = 90500 * 16; /* 90.5Mhz default */
  319. snd_tea575x_set_freq(tea);
  320. tea->vd = tea575x_radio;
  321. video_set_drvdata(&tea->vd, tea);
  322. mutex_init(&tea->mutex);
  323. strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
  324. tea->vd.lock = &tea->mutex;
  325. tea->vd.v4l2_dev = tea->v4l2_dev;
  326. tea->vd.ctrl_handler = &tea->ctrl_handler;
  327. set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
  328. /* disable hw_freq_seek if we can't use it */
  329. if (tea->cannot_read_data)
  330. v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK);
  331. v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
  332. v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  333. retval = tea->ctrl_handler.error;
  334. if (retval) {
  335. v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
  336. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  337. return retval;
  338. }
  339. if (tea->ext_init) {
  340. retval = tea->ext_init(tea);
  341. if (retval) {
  342. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  343. return retval;
  344. }
  345. }
  346. v4l2_ctrl_handler_setup(&tea->ctrl_handler);
  347. retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
  348. if (retval) {
  349. v4l2_err(tea->v4l2_dev, "can't register video device!\n");
  350. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  351. return retval;
  352. }
  353. return 0;
  354. }
  355. void snd_tea575x_exit(struct snd_tea575x *tea)
  356. {
  357. video_unregister_device(&tea->vd);
  358. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  359. }
  360. static int __init alsa_tea575x_module_init(void)
  361. {
  362. return 0;
  363. }
  364. static void __exit alsa_tea575x_module_exit(void)
  365. {
  366. }
  367. module_init(alsa_tea575x_module_init)
  368. module_exit(alsa_tea575x_module_exit)
  369. EXPORT_SYMBOL(snd_tea575x_init);
  370. EXPORT_SYMBOL(snd_tea575x_exit);