tea575x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. */
  18. #include <linux/delay.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched.h>
  23. #include <asm/io.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-dev.h>
  26. #include <media/v4l2-fh.h>
  27. #include <media/v4l2-ioctl.h>
  28. #include <media/v4l2-event.h>
  29. #include <media/drv-intf/tea575x.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. /*
  34. * definitions
  35. */
  36. #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */
  37. #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */
  38. #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
  39. #define TEA575X_BIT_BAND_MASK (3<<20)
  40. #define TEA575X_BIT_BAND_FM (0<<20)
  41. #define TEA575X_BIT_BAND_MW (1<<20)
  42. #define TEA575X_BIT_BAND_LW (2<<20)
  43. #define TEA575X_BIT_BAND_SW (3<<20)
  44. #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */
  45. #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */
  46. #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */
  47. #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */
  48. #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */
  49. #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */
  50. #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */
  51. #define TEA575X_BIT_DUMMY (1<<15) /* buffer */
  52. #define TEA575X_BIT_FREQ_MASK 0x7fff
  53. enum { BAND_FM, BAND_FM_JAPAN, BAND_AM };
  54. static const struct v4l2_frequency_band bands[] = {
  55. {
  56. .type = V4L2_TUNER_RADIO,
  57. .index = 0,
  58. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  59. V4L2_TUNER_CAP_FREQ_BANDS,
  60. .rangelow = 87500 * 16,
  61. .rangehigh = 108000 * 16,
  62. .modulation = V4L2_BAND_MODULATION_FM,
  63. },
  64. {
  65. .type = V4L2_TUNER_RADIO,
  66. .index = 0,
  67. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  68. V4L2_TUNER_CAP_FREQ_BANDS,
  69. .rangelow = 76000 * 16,
  70. .rangehigh = 91000 * 16,
  71. .modulation = V4L2_BAND_MODULATION_FM,
  72. },
  73. {
  74. .type = V4L2_TUNER_RADIO,
  75. .index = 1,
  76. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
  77. .rangelow = 530 * 16,
  78. .rangehigh = 1710 * 16,
  79. .modulation = V4L2_BAND_MODULATION_AM,
  80. },
  81. };
  82. /*
  83. * lowlevel part
  84. */
  85. static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
  86. {
  87. u16 l;
  88. u8 data;
  89. if (tea->ops->write_val)
  90. return tea->ops->write_val(tea, val);
  91. tea->ops->set_direction(tea, 1);
  92. udelay(16);
  93. for (l = 25; l > 0; l--) {
  94. data = (val >> 24) & TEA575X_DATA;
  95. val <<= 1; /* shift data */
  96. tea->ops->set_pins(tea, data | TEA575X_WREN);
  97. udelay(2);
  98. tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
  99. udelay(2);
  100. tea->ops->set_pins(tea, data | TEA575X_WREN);
  101. udelay(2);
  102. }
  103. if (!tea->mute)
  104. tea->ops->set_pins(tea, 0);
  105. }
  106. static u32 snd_tea575x_read(struct snd_tea575x *tea)
  107. {
  108. u16 l, rdata;
  109. u32 data = 0;
  110. if (tea->ops->read_val)
  111. return tea->ops->read_val(tea);
  112. tea->ops->set_direction(tea, 0);
  113. tea->ops->set_pins(tea, 0);
  114. udelay(16);
  115. for (l = 24; l--;) {
  116. tea->ops->set_pins(tea, TEA575X_CLK);
  117. udelay(2);
  118. if (!l)
  119. tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
  120. tea->ops->set_pins(tea, 0);
  121. udelay(2);
  122. data <<= 1; /* shift data */
  123. rdata = tea->ops->get_pins(tea);
  124. if (!l)
  125. tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
  126. if (rdata & TEA575X_DATA)
  127. data++;
  128. udelay(2);
  129. }
  130. if (tea->mute)
  131. tea->ops->set_pins(tea, TEA575X_WREN);
  132. return data;
  133. }
  134. static u32 snd_tea575x_val_to_freq(struct snd_tea575x *tea, u32 val)
  135. {
  136. u32 freq = val & TEA575X_BIT_FREQ_MASK;
  137. if (freq == 0)
  138. return freq;
  139. switch (tea->band) {
  140. case BAND_FM:
  141. /* freq *= 12.5 */
  142. freq *= 125;
  143. freq /= 10;
  144. /* crystal fixup */
  145. freq -= TEA575X_FMIF;
  146. break;
  147. case BAND_FM_JAPAN:
  148. /* freq *= 12.5 */
  149. freq *= 125;
  150. freq /= 10;
  151. /* crystal fixup */
  152. freq += TEA575X_FMIF;
  153. break;
  154. case BAND_AM:
  155. /* crystal fixup */
  156. freq -= TEA575X_AMIF;
  157. break;
  158. }
  159. return clamp(freq * 16, bands[tea->band].rangelow,
  160. bands[tea->band].rangehigh); /* from kHz */
  161. }
  162. static u32 snd_tea575x_get_freq(struct snd_tea575x *tea)
  163. {
  164. return snd_tea575x_val_to_freq(tea, snd_tea575x_read(tea));
  165. }
  166. void snd_tea575x_set_freq(struct snd_tea575x *tea)
  167. {
  168. u32 freq = tea->freq / 16; /* to kHz */
  169. u32 band = 0;
  170. switch (tea->band) {
  171. case BAND_FM:
  172. band = TEA575X_BIT_BAND_FM;
  173. /* crystal fixup */
  174. freq += TEA575X_FMIF;
  175. /* freq /= 12.5 */
  176. freq *= 10;
  177. freq /= 125;
  178. break;
  179. case BAND_FM_JAPAN:
  180. band = TEA575X_BIT_BAND_FM;
  181. /* crystal fixup */
  182. freq -= TEA575X_FMIF;
  183. /* freq /= 12.5 */
  184. freq *= 10;
  185. freq /= 125;
  186. break;
  187. case BAND_AM:
  188. band = TEA575X_BIT_BAND_MW;
  189. /* crystal fixup */
  190. freq += TEA575X_AMIF;
  191. break;
  192. }
  193. tea->val &= ~(TEA575X_BIT_FREQ_MASK | TEA575X_BIT_BAND_MASK);
  194. tea->val |= band;
  195. tea->val |= freq & TEA575X_BIT_FREQ_MASK;
  196. snd_tea575x_write(tea, tea->val);
  197. tea->freq = snd_tea575x_val_to_freq(tea, tea->val);
  198. }
  199. EXPORT_SYMBOL(snd_tea575x_set_freq);
  200. /*
  201. * Linux Video interface
  202. */
  203. static int vidioc_querycap(struct file *file, void *priv,
  204. struct v4l2_capability *v)
  205. {
  206. struct snd_tea575x *tea = video_drvdata(file);
  207. strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
  208. strlcpy(v->card, tea->card, sizeof(v->card));
  209. strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
  210. strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
  211. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  212. if (!tea->cannot_read_data)
  213. v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
  214. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  215. return 0;
  216. }
  217. int snd_tea575x_enum_freq_bands(struct snd_tea575x *tea,
  218. struct v4l2_frequency_band *band)
  219. {
  220. int index;
  221. if (band->tuner != 0)
  222. return -EINVAL;
  223. switch (band->index) {
  224. case 0:
  225. if (tea->tea5759)
  226. index = BAND_FM_JAPAN;
  227. else
  228. index = BAND_FM;
  229. break;
  230. case 1:
  231. if (tea->has_am) {
  232. index = BAND_AM;
  233. break;
  234. }
  235. /* Fall through */
  236. default:
  237. return -EINVAL;
  238. }
  239. *band = bands[index];
  240. if (!tea->cannot_read_data)
  241. band->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(snd_tea575x_enum_freq_bands);
  245. static int vidioc_enum_freq_bands(struct file *file, void *priv,
  246. struct v4l2_frequency_band *band)
  247. {
  248. struct snd_tea575x *tea = video_drvdata(file);
  249. return snd_tea575x_enum_freq_bands(tea, band);
  250. }
  251. int snd_tea575x_g_tuner(struct snd_tea575x *tea, struct v4l2_tuner *v)
  252. {
  253. struct v4l2_frequency_band band_fm = { 0, };
  254. if (v->index > 0)
  255. return -EINVAL;
  256. snd_tea575x_read(tea);
  257. snd_tea575x_enum_freq_bands(tea, &band_fm);
  258. memset(v, 0, sizeof(*v));
  259. strlcpy(v->name, tea->has_am ? "FM/AM" : "FM", sizeof(v->name));
  260. v->type = V4L2_TUNER_RADIO;
  261. v->capability = band_fm.capability;
  262. v->rangelow = tea->has_am ? bands[BAND_AM].rangelow : band_fm.rangelow;
  263. v->rangehigh = band_fm.rangehigh;
  264. v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  265. v->audmode = (tea->val & TEA575X_BIT_MONO) ?
  266. V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
  267. v->signal = tea->tuned ? 0xffff : 0;
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(snd_tea575x_g_tuner);
  271. static int vidioc_g_tuner(struct file *file, void *priv,
  272. struct v4l2_tuner *v)
  273. {
  274. struct snd_tea575x *tea = video_drvdata(file);
  275. return snd_tea575x_g_tuner(tea, v);
  276. }
  277. static int vidioc_s_tuner(struct file *file, void *priv,
  278. const struct v4l2_tuner *v)
  279. {
  280. struct snd_tea575x *tea = video_drvdata(file);
  281. u32 orig_val = tea->val;
  282. if (v->index)
  283. return -EINVAL;
  284. tea->val &= ~TEA575X_BIT_MONO;
  285. if (v->audmode == V4L2_TUNER_MODE_MONO)
  286. tea->val |= TEA575X_BIT_MONO;
  287. /* Only apply changes if currently tuning FM */
  288. if (tea->band != BAND_AM && tea->val != orig_val)
  289. snd_tea575x_set_freq(tea);
  290. return 0;
  291. }
  292. static int vidioc_g_frequency(struct file *file, void *priv,
  293. struct v4l2_frequency *f)
  294. {
  295. struct snd_tea575x *tea = video_drvdata(file);
  296. if (f->tuner != 0)
  297. return -EINVAL;
  298. f->type = V4L2_TUNER_RADIO;
  299. f->frequency = tea->freq;
  300. return 0;
  301. }
  302. static int vidioc_s_frequency(struct file *file, void *priv,
  303. const struct v4l2_frequency *f)
  304. {
  305. struct snd_tea575x *tea = video_drvdata(file);
  306. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  307. return -EINVAL;
  308. if (tea->has_am && f->frequency < (20000 * 16))
  309. tea->band = BAND_AM;
  310. else if (tea->tea5759)
  311. tea->band = BAND_FM_JAPAN;
  312. else
  313. tea->band = BAND_FM;
  314. tea->freq = clamp_t(u32, f->frequency, bands[tea->band].rangelow,
  315. bands[tea->band].rangehigh);
  316. snd_tea575x_set_freq(tea);
  317. return 0;
  318. }
  319. int snd_tea575x_s_hw_freq_seek(struct file *file, struct snd_tea575x *tea,
  320. const struct v4l2_hw_freq_seek *a)
  321. {
  322. unsigned long timeout;
  323. int i, spacing;
  324. if (tea->cannot_read_data)
  325. return -ENOTTY;
  326. if (a->tuner || a->wrap_around)
  327. return -EINVAL;
  328. if (file->f_flags & O_NONBLOCK)
  329. return -EWOULDBLOCK;
  330. if (a->rangelow || a->rangehigh) {
  331. for (i = 0; i < ARRAY_SIZE(bands); i++) {
  332. if ((i == BAND_FM && tea->tea5759) ||
  333. (i == BAND_FM_JAPAN && !tea->tea5759) ||
  334. (i == BAND_AM && !tea->has_am))
  335. continue;
  336. if (bands[i].rangelow == a->rangelow &&
  337. bands[i].rangehigh == a->rangehigh)
  338. break;
  339. }
  340. if (i == ARRAY_SIZE(bands))
  341. return -EINVAL; /* No matching band found */
  342. if (i != tea->band) {
  343. tea->band = i;
  344. tea->freq = clamp(tea->freq, bands[i].rangelow,
  345. bands[i].rangehigh);
  346. snd_tea575x_set_freq(tea);
  347. }
  348. }
  349. spacing = (tea->band == BAND_AM) ? 5 : 50; /* kHz */
  350. /* clear the frequency, HW will fill it in */
  351. tea->val &= ~TEA575X_BIT_FREQ_MASK;
  352. tea->val |= TEA575X_BIT_SEARCH;
  353. if (a->seek_upward)
  354. tea->val |= TEA575X_BIT_UPDOWN;
  355. else
  356. tea->val &= ~TEA575X_BIT_UPDOWN;
  357. snd_tea575x_write(tea, tea->val);
  358. timeout = jiffies + msecs_to_jiffies(10000);
  359. for (;;) {
  360. if (time_after(jiffies, timeout))
  361. break;
  362. if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
  363. /* some signal arrived, stop search */
  364. tea->val &= ~TEA575X_BIT_SEARCH;
  365. snd_tea575x_set_freq(tea);
  366. return -ERESTARTSYS;
  367. }
  368. if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
  369. u32 freq;
  370. /* Found a frequency, wait until it can be read */
  371. for (i = 0; i < 100; i++) {
  372. msleep(10);
  373. freq = snd_tea575x_get_freq(tea);
  374. if (freq) /* available */
  375. break;
  376. }
  377. if (freq == 0) /* shouldn't happen */
  378. break;
  379. /*
  380. * if we moved by less than the spacing, or in the
  381. * wrong direction, continue seeking
  382. */
  383. if (abs(tea->freq - freq) < 16 * spacing ||
  384. (a->seek_upward && freq < tea->freq) ||
  385. (!a->seek_upward && freq > tea->freq)) {
  386. snd_tea575x_write(tea, tea->val);
  387. continue;
  388. }
  389. tea->freq = freq;
  390. tea->val &= ~TEA575X_BIT_SEARCH;
  391. return 0;
  392. }
  393. }
  394. tea->val &= ~TEA575X_BIT_SEARCH;
  395. snd_tea575x_set_freq(tea);
  396. return -ENODATA;
  397. }
  398. EXPORT_SYMBOL(snd_tea575x_s_hw_freq_seek);
  399. static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
  400. const struct v4l2_hw_freq_seek *a)
  401. {
  402. struct snd_tea575x *tea = video_drvdata(file);
  403. return snd_tea575x_s_hw_freq_seek(file, tea, a);
  404. }
  405. static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
  406. {
  407. struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
  408. switch (ctrl->id) {
  409. case V4L2_CID_AUDIO_MUTE:
  410. tea->mute = ctrl->val;
  411. snd_tea575x_set_freq(tea);
  412. return 0;
  413. }
  414. return -EINVAL;
  415. }
  416. static const struct v4l2_file_operations tea575x_fops = {
  417. .unlocked_ioctl = video_ioctl2,
  418. .open = v4l2_fh_open,
  419. .release = v4l2_fh_release,
  420. .poll = v4l2_ctrl_poll,
  421. };
  422. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  423. .vidioc_querycap = vidioc_querycap,
  424. .vidioc_g_tuner = vidioc_g_tuner,
  425. .vidioc_s_tuner = vidioc_s_tuner,
  426. .vidioc_g_frequency = vidioc_g_frequency,
  427. .vidioc_s_frequency = vidioc_s_frequency,
  428. .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
  429. .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
  430. .vidioc_log_status = v4l2_ctrl_log_status,
  431. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  432. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  433. };
  434. static const struct video_device tea575x_radio = {
  435. .ioctl_ops = &tea575x_ioctl_ops,
  436. .release = video_device_release_empty,
  437. };
  438. static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
  439. .s_ctrl = tea575x_s_ctrl,
  440. };
  441. int snd_tea575x_hw_init(struct snd_tea575x *tea)
  442. {
  443. tea->mute = true;
  444. /* Not all devices can or know how to read the data back.
  445. Such devices can set cannot_read_data to true. */
  446. if (!tea->cannot_read_data) {
  447. snd_tea575x_write(tea, 0x55AA);
  448. if (snd_tea575x_read(tea) != 0x55AA)
  449. return -ENODEV;
  450. }
  451. tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
  452. tea->freq = 90500 * 16; /* 90.5Mhz default */
  453. snd_tea575x_set_freq(tea);
  454. return 0;
  455. }
  456. EXPORT_SYMBOL(snd_tea575x_hw_init);
  457. int snd_tea575x_init(struct snd_tea575x *tea, struct module *owner)
  458. {
  459. int retval = snd_tea575x_hw_init(tea);
  460. if (retval)
  461. return retval;
  462. tea->vd = tea575x_radio;
  463. video_set_drvdata(&tea->vd, tea);
  464. mutex_init(&tea->mutex);
  465. strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
  466. tea->vd.lock = &tea->mutex;
  467. tea->vd.v4l2_dev = tea->v4l2_dev;
  468. tea->fops = tea575x_fops;
  469. tea->fops.owner = owner;
  470. tea->vd.fops = &tea->fops;
  471. /* disable hw_freq_seek if we can't use it */
  472. if (tea->cannot_read_data)
  473. v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK);
  474. if (!tea->cannot_mute) {
  475. tea->vd.ctrl_handler = &tea->ctrl_handler;
  476. v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
  477. v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
  478. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  479. retval = tea->ctrl_handler.error;
  480. if (retval) {
  481. v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
  482. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  483. return retval;
  484. }
  485. if (tea->ext_init) {
  486. retval = tea->ext_init(tea);
  487. if (retval) {
  488. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  489. return retval;
  490. }
  491. }
  492. v4l2_ctrl_handler_setup(&tea->ctrl_handler);
  493. }
  494. retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
  495. if (retval) {
  496. v4l2_err(tea->v4l2_dev, "can't register video device!\n");
  497. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  498. return retval;
  499. }
  500. return 0;
  501. }
  502. EXPORT_SYMBOL(snd_tea575x_init);
  503. void snd_tea575x_exit(struct snd_tea575x *tea)
  504. {
  505. video_unregister_device(&tea->vd);
  506. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  507. }
  508. EXPORT_SYMBOL(snd_tea575x_exit);