tea575x-tuner.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 ((tea->tea5759 ? 760 : 875) * 1600U)
  38. #define FREQ_HI ((tea->tea5759 ? 910 : 1080) * 1600U)
  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. if (tea->ops->write_val)
  67. return tea->ops->write_val(tea, val);
  68. tea->ops->set_direction(tea, 1);
  69. udelay(16);
  70. for (l = 25; l > 0; l--) {
  71. data = (val >> 24) & TEA575X_DATA;
  72. val <<= 1; /* shift data */
  73. tea->ops->set_pins(tea, data | TEA575X_WREN);
  74. udelay(2);
  75. tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
  76. udelay(2);
  77. tea->ops->set_pins(tea, data | TEA575X_WREN);
  78. udelay(2);
  79. }
  80. if (!tea->mute)
  81. tea->ops->set_pins(tea, 0);
  82. }
  83. static u32 snd_tea575x_read(struct snd_tea575x *tea)
  84. {
  85. u16 l, rdata;
  86. u32 data = 0;
  87. if (tea->ops->read_val)
  88. return tea->ops->read_val(tea);
  89. tea->ops->set_direction(tea, 0);
  90. tea->ops->set_pins(tea, 0);
  91. udelay(16);
  92. for (l = 24; l--;) {
  93. tea->ops->set_pins(tea, TEA575X_CLK);
  94. udelay(2);
  95. if (!l)
  96. tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
  97. tea->ops->set_pins(tea, 0);
  98. udelay(2);
  99. data <<= 1; /* shift data */
  100. rdata = tea->ops->get_pins(tea);
  101. if (!l)
  102. tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
  103. if (rdata & TEA575X_DATA)
  104. data++;
  105. udelay(2);
  106. }
  107. if (tea->mute)
  108. tea->ops->set_pins(tea, TEA575X_WREN);
  109. return data;
  110. }
  111. static u32 snd_tea575x_val_to_freq(struct snd_tea575x *tea, u32 val)
  112. {
  113. u32 freq = val & TEA575X_BIT_FREQ_MASK;
  114. if (freq == 0)
  115. return freq;
  116. /* freq *= 12.5 */
  117. freq *= 125;
  118. freq /= 10;
  119. /* crystal fixup */
  120. if (tea->tea5759)
  121. freq += TEA575X_FMIF;
  122. else
  123. freq -= TEA575X_FMIF;
  124. return clamp(freq * 16, FREQ_LO, FREQ_HI); /* from kHz */
  125. }
  126. static u32 snd_tea575x_get_freq(struct snd_tea575x *tea)
  127. {
  128. return snd_tea575x_val_to_freq(tea, snd_tea575x_read(tea));
  129. }
  130. static void snd_tea575x_set_freq(struct snd_tea575x *tea)
  131. {
  132. u32 freq = tea->freq;
  133. freq /= 16; /* to kHz */
  134. /* crystal fixup */
  135. if (tea->tea5759)
  136. freq -= TEA575X_FMIF;
  137. else
  138. freq += TEA575X_FMIF;
  139. /* freq /= 12.5 */
  140. freq *= 10;
  141. freq /= 125;
  142. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  143. tea->val |= freq & TEA575X_BIT_FREQ_MASK;
  144. snd_tea575x_write(tea, tea->val);
  145. tea->freq = snd_tea575x_val_to_freq(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, tea->v4l2_dev->name, 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->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  159. if (!tea->cannot_read_data)
  160. v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
  161. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  162. return 0;
  163. }
  164. static int vidioc_g_tuner(struct file *file, void *priv,
  165. struct v4l2_tuner *v)
  166. {
  167. struct snd_tea575x *tea = video_drvdata(file);
  168. if (v->index > 0)
  169. return -EINVAL;
  170. snd_tea575x_read(tea);
  171. strcpy(v->name, "FM");
  172. v->type = V4L2_TUNER_RADIO;
  173. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  174. if (!tea->cannot_read_data)
  175. v->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
  176. v->rangelow = FREQ_LO;
  177. v->rangehigh = FREQ_HI;
  178. v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  179. v->audmode = (tea->val & TEA575X_BIT_MONO) ?
  180. V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
  181. v->signal = tea->tuned ? 0xffff : 0;
  182. return 0;
  183. }
  184. static int vidioc_s_tuner(struct file *file, void *priv,
  185. struct v4l2_tuner *v)
  186. {
  187. struct snd_tea575x *tea = video_drvdata(file);
  188. if (v->index)
  189. return -EINVAL;
  190. tea->val &= ~TEA575X_BIT_MONO;
  191. if (v->audmode == V4L2_TUNER_MODE_MONO)
  192. tea->val |= TEA575X_BIT_MONO;
  193. snd_tea575x_write(tea, tea->val);
  194. return 0;
  195. }
  196. static int vidioc_g_frequency(struct file *file, void *priv,
  197. struct v4l2_frequency *f)
  198. {
  199. struct snd_tea575x *tea = video_drvdata(file);
  200. if (f->tuner != 0)
  201. return -EINVAL;
  202. f->type = V4L2_TUNER_RADIO;
  203. f->frequency = tea->freq;
  204. return 0;
  205. }
  206. static int vidioc_s_frequency(struct file *file, void *priv,
  207. struct v4l2_frequency *f)
  208. {
  209. struct snd_tea575x *tea = video_drvdata(file);
  210. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  211. return -EINVAL;
  212. tea->val &= ~TEA575X_BIT_SEARCH;
  213. tea->freq = clamp(f->frequency, FREQ_LO, FREQ_HI);
  214. snd_tea575x_set_freq(tea);
  215. return 0;
  216. }
  217. static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
  218. struct v4l2_hw_freq_seek *a)
  219. {
  220. struct snd_tea575x *tea = video_drvdata(file);
  221. unsigned long timeout;
  222. int i;
  223. if (tea->cannot_read_data)
  224. return -ENOTTY;
  225. if (a->tuner || a->wrap_around)
  226. return -EINVAL;
  227. /* clear the frequency, HW will fill it in */
  228. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  229. tea->val |= TEA575X_BIT_SEARCH;
  230. if (a->seek_upward)
  231. tea->val |= TEA575X_BIT_UPDOWN;
  232. else
  233. tea->val &= ~TEA575X_BIT_UPDOWN;
  234. snd_tea575x_write(tea, tea->val);
  235. timeout = jiffies + msecs_to_jiffies(10000);
  236. for (;;) {
  237. if (time_after(jiffies, timeout))
  238. break;
  239. if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
  240. /* some signal arrived, stop search */
  241. tea->val &= ~TEA575X_BIT_SEARCH;
  242. snd_tea575x_set_freq(tea);
  243. return -ERESTARTSYS;
  244. }
  245. if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
  246. u32 freq;
  247. /* Found a frequency, wait until it can be read */
  248. for (i = 0; i < 100; i++) {
  249. msleep(10);
  250. freq = snd_tea575x_get_freq(tea);
  251. if (freq) /* available */
  252. break;
  253. }
  254. if (freq == 0) /* shouldn't happen */
  255. break;
  256. /*
  257. * if we moved by less than 50 kHz, or in the wrong
  258. * direction, continue seeking
  259. */
  260. if (abs(tea->freq - freq) < 16 * 50 ||
  261. (a->seek_upward && freq < tea->freq) ||
  262. (!a->seek_upward && freq > tea->freq)) {
  263. snd_tea575x_write(tea, tea->val);
  264. continue;
  265. }
  266. tea->freq = freq;
  267. tea->val &= ~TEA575X_BIT_SEARCH;
  268. return 0;
  269. }
  270. }
  271. tea->val &= ~TEA575X_BIT_SEARCH;
  272. snd_tea575x_set_freq(tea);
  273. return -ENODATA;
  274. }
  275. static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
  276. {
  277. struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
  278. switch (ctrl->id) {
  279. case V4L2_CID_AUDIO_MUTE:
  280. tea->mute = ctrl->val;
  281. snd_tea575x_set_freq(tea);
  282. return 0;
  283. }
  284. return -EINVAL;
  285. }
  286. static const struct v4l2_file_operations tea575x_fops = {
  287. .unlocked_ioctl = video_ioctl2,
  288. .open = v4l2_fh_open,
  289. .release = v4l2_fh_release,
  290. .poll = v4l2_ctrl_poll,
  291. };
  292. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  293. .vidioc_querycap = vidioc_querycap,
  294. .vidioc_g_tuner = vidioc_g_tuner,
  295. .vidioc_s_tuner = vidioc_s_tuner,
  296. .vidioc_g_frequency = vidioc_g_frequency,
  297. .vidioc_s_frequency = vidioc_s_frequency,
  298. .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
  299. .vidioc_log_status = v4l2_ctrl_log_status,
  300. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  301. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  302. };
  303. static const struct video_device tea575x_radio = {
  304. .ioctl_ops = &tea575x_ioctl_ops,
  305. .release = video_device_release_empty,
  306. };
  307. static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
  308. .s_ctrl = tea575x_s_ctrl,
  309. };
  310. /*
  311. * initialize all the tea575x chips
  312. */
  313. int snd_tea575x_init(struct snd_tea575x *tea, struct module *owner)
  314. {
  315. int retval;
  316. tea->mute = true;
  317. /* Not all devices can or know how to read the data back.
  318. Such devices can set cannot_read_data to true. */
  319. if (!tea->cannot_read_data) {
  320. snd_tea575x_write(tea, 0x55AA);
  321. if (snd_tea575x_read(tea) != 0x55AA)
  322. return -ENODEV;
  323. }
  324. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
  325. tea->freq = 90500 * 16; /* 90.5Mhz default */
  326. snd_tea575x_set_freq(tea);
  327. tea->vd = tea575x_radio;
  328. video_set_drvdata(&tea->vd, tea);
  329. mutex_init(&tea->mutex);
  330. strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
  331. tea->vd.lock = &tea->mutex;
  332. tea->vd.v4l2_dev = tea->v4l2_dev;
  333. tea->fops = tea575x_fops;
  334. tea->fops.owner = owner;
  335. tea->vd.fops = &tea->fops;
  336. set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
  337. /* disable hw_freq_seek if we can't use it */
  338. if (tea->cannot_read_data)
  339. v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK);
  340. if (!tea->cannot_mute) {
  341. tea->vd.ctrl_handler = &tea->ctrl_handler;
  342. v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
  343. v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
  344. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  345. retval = tea->ctrl_handler.error;
  346. if (retval) {
  347. v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
  348. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  349. return retval;
  350. }
  351. if (tea->ext_init) {
  352. retval = tea->ext_init(tea);
  353. if (retval) {
  354. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  355. return retval;
  356. }
  357. }
  358. v4l2_ctrl_handler_setup(&tea->ctrl_handler);
  359. }
  360. retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
  361. if (retval) {
  362. v4l2_err(tea->v4l2_dev, "can't register video device!\n");
  363. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  364. return retval;
  365. }
  366. return 0;
  367. }
  368. void snd_tea575x_exit(struct snd_tea575x *tea)
  369. {
  370. video_unregister_device(&tea->vd);
  371. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  372. }
  373. static int __init alsa_tea575x_module_init(void)
  374. {
  375. return 0;
  376. }
  377. static void __exit alsa_tea575x_module_exit(void)
  378. {
  379. }
  380. module_init(alsa_tea575x_module_init)
  381. module_exit(alsa_tea575x_module_exit)
  382. EXPORT_SYMBOL(snd_tea575x_init);
  383. EXPORT_SYMBOL(snd_tea575x_exit);