tea575x.c 14 KB

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