radio-tea5777.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * v4l2 driver for TEA5777 Philips AM/FM radio tuner chips
  3. *
  4. * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * Based on the ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips:
  7. *
  8. * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/v4l2-fh.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-event.h>
  35. #include "radio-tea5777.h"
  36. MODULE_AUTHOR("Hans de Goede <perex@perex.cz>");
  37. MODULE_DESCRIPTION("Routines for control of TEA5777 Philips AM/FM radio tuner chips");
  38. MODULE_LICENSE("GPL");
  39. /* Fixed FM only band for now, will implement multi-band support when the
  40. VIDIOC_ENUM_FREQ_BANDS API is upstream */
  41. #define TEA5777_FM_RANGELOW (76000 * 16)
  42. #define TEA5777_FM_RANGEHIGH (108000 * 16)
  43. #define TEA5777_FM_IF 150 /* kHz */
  44. #define TEA5777_FM_FREQ_STEP 50 /* kHz */
  45. /* Write reg, common bits */
  46. #define TEA5777_W_MUTE_MASK (1LL << 47)
  47. #define TEA5777_W_MUTE_SHIFT 47
  48. #define TEA5777_W_AM_FM_MASK (1LL << 46)
  49. #define TEA5777_W_AM_FM_SHIFT 46
  50. #define TEA5777_W_STB_MASK (1LL << 45)
  51. #define TEA5777_W_STB_SHIFT 45
  52. #define TEA5777_W_IFCE_MASK (1LL << 29)
  53. #define TEA5777_W_IFCE_SHIFT 29
  54. #define TEA5777_W_IFW_MASK (1LL << 28)
  55. #define TEA5777_W_IFW_SHIFT 28
  56. #define TEA5777_W_HILO_MASK (1LL << 27)
  57. #define TEA5777_W_HILO_SHIFT 27
  58. #define TEA5777_W_DBUS_MASK (1LL << 26)
  59. #define TEA5777_W_DBUS_SHIFT 26
  60. #define TEA5777_W_INTEXT_MASK (1LL << 24)
  61. #define TEA5777_W_INTEXT_SHIFT 24
  62. #define TEA5777_W_P1_MASK (1LL << 23)
  63. #define TEA5777_W_P1_SHIFT 23
  64. #define TEA5777_W_P0_MASK (1LL << 22)
  65. #define TEA5777_W_P0_SHIFT 22
  66. #define TEA5777_W_PEN1_MASK (1LL << 21)
  67. #define TEA5777_W_PEN1_SHIFT 21
  68. #define TEA5777_W_PEN0_MASK (1LL << 20)
  69. #define TEA5777_W_PEN0_SHIFT 20
  70. #define TEA5777_W_CHP0_MASK (1LL << 18)
  71. #define TEA5777_W_CHP0_SHIFT 18
  72. #define TEA5777_W_DEEM_MASK (1LL << 17)
  73. #define TEA5777_W_DEEM_SHIFT 17
  74. #define TEA5777_W_SEARCH_MASK (1LL << 7)
  75. #define TEA5777_W_SEARCH_SHIFT 7
  76. #define TEA5777_W_PROGBLIM_MASK (1LL << 6)
  77. #define TEA5777_W_PROGBLIM_SHIFT 6
  78. #define TEA5777_W_UPDWN_MASK (1LL << 5)
  79. #define TEA5777_W_UPDWN_SHIFT 5
  80. #define TEA5777_W_SLEV_MASK (3LL << 3)
  81. #define TEA5777_W_SLEV_SHIFT 3
  82. /* Write reg, FM specific bits */
  83. #define TEA5777_W_FM_PLL_MASK (0x1fffLL << 32)
  84. #define TEA5777_W_FM_PLL_SHIFT 32
  85. #define TEA5777_W_FM_FREF_MASK (0x03LL << 30)
  86. #define TEA5777_W_FM_FREF_SHIFT 30
  87. #define TEA5777_W_FM_FREF_VALUE 0 /* 50 kHz tune steps, 150 kHz IF */
  88. #define TEA5777_W_FM_FORCEMONO_MASK (1LL << 15)
  89. #define TEA5777_W_FM_FORCEMONO_SHIFT 15
  90. #define TEA5777_W_FM_SDSOFF_MASK (1LL << 14)
  91. #define TEA5777_W_FM_SDSOFF_SHIFT 14
  92. #define TEA5777_W_FM_DOFF_MASK (1LL << 13)
  93. #define TEA5777_W_FM_DOFF_SHIFT 13
  94. #define TEA5777_W_FM_STEP_MASK (3LL << 1)
  95. #define TEA5777_W_FM_STEP_SHIFT 1
  96. /* Write reg, AM specific bits */
  97. #define TEA5777_W_AM_PLL_MASK (0x7ffLL << 34)
  98. #define TEA5777_W_AM_PLL_SHIFT 34
  99. #define TEA5777_W_AM_AGCRF_MASK (1LL << 33)
  100. #define TEA5777_W_AM_AGCRF_SHIFT 33
  101. #define TEA5777_W_AM_AGCIF_MASK (1LL << 32)
  102. #define TEA5777_W_AM_AGCIF_SHIFT 32
  103. #define TEA5777_W_AM_MWLW_MASK (1LL << 31)
  104. #define TEA5777_W_AM_MWLW_SHIFT 31
  105. #define TEA5777_W_AM_LNA_MASK (1LL << 30)
  106. #define TEA5777_W_AM_LNA_SHIFT 30
  107. #define TEA5777_W_AM_PEAK_MASK (1LL << 25)
  108. #define TEA5777_W_AM_PEAK_SHIFT 25
  109. #define TEA5777_W_AM_RFB_MASK (1LL << 16)
  110. #define TEA5777_W_AM_RFB_SHIFT 16
  111. #define TEA5777_W_AM_CALLIGN_MASK (1LL << 15)
  112. #define TEA5777_W_AM_CALLIGN_SHIFT 15
  113. #define TEA5777_W_AM_CBANK_MASK (0x7fLL << 8)
  114. #define TEA5777_W_AM_CBANK_SHIFT 8
  115. #define TEA5777_W_AM_DELAY_MASK (1LL << 2)
  116. #define TEA5777_W_AM_DELAY_SHIFT 2
  117. #define TEA5777_W_AM_STEP_MASK (1LL << 1)
  118. #define TEA5777_W_AM_STEP_SHIFT 1
  119. /* Read reg, common bits */
  120. #define TEA5777_R_LEVEL_MASK (0x0f << 17)
  121. #define TEA5777_R_LEVEL_SHIFT 17
  122. #define TEA5777_R_SFOUND_MASK (0x01 << 16)
  123. #define TEA5777_R_SFOUND_SHIFT 16
  124. #define TEA5777_R_BLIM_MASK (0x01 << 15)
  125. #define TEA5777_R_BLIM_SHIFT 15
  126. /* Read reg, FM specific bits */
  127. #define TEA5777_R_FM_STEREO_MASK (0x01 << 21)
  128. #define TEA5777_R_FM_STEREO_SHIFT 21
  129. #define TEA5777_R_FM_PLL_MASK 0x1fff
  130. #define TEA5777_R_FM_PLL_SHIFT 0
  131. static u32 tea5777_freq_to_v4l2_freq(struct radio_tea5777 *tea, u32 freq)
  132. {
  133. return (freq * TEA5777_FM_FREQ_STEP + TEA5777_FM_IF) * 16;
  134. }
  135. static int radio_tea5777_set_freq(struct radio_tea5777 *tea)
  136. {
  137. u32 freq;
  138. int res;
  139. freq = clamp_t(u32, tea->freq,
  140. TEA5777_FM_RANGELOW, TEA5777_FM_RANGEHIGH);
  141. freq = (freq + 8) / 16; /* to kHz */
  142. freq = (freq - TEA5777_FM_IF) / TEA5777_FM_FREQ_STEP;
  143. tea->write_reg &= ~(TEA5777_W_FM_PLL_MASK | TEA5777_W_FM_FREF_MASK);
  144. tea->write_reg |= (u64)freq << TEA5777_W_FM_PLL_SHIFT;
  145. tea->write_reg |= TEA5777_W_FM_FREF_VALUE << TEA5777_W_FM_FREF_SHIFT;
  146. res = tea->ops->write_reg(tea, tea->write_reg);
  147. if (res)
  148. return res;
  149. tea->needs_write = false;
  150. tea->read_reg = -1;
  151. tea->freq = tea5777_freq_to_v4l2_freq(tea, freq);
  152. return 0;
  153. }
  154. static int radio_tea5777_update_read_reg(struct radio_tea5777 *tea, int wait)
  155. {
  156. int res;
  157. if (tea->read_reg != -1)
  158. return 0;
  159. if (tea->write_before_read && tea->needs_write) {
  160. res = radio_tea5777_set_freq(tea);
  161. if (res)
  162. return res;
  163. }
  164. if (wait) {
  165. if (schedule_timeout_interruptible(msecs_to_jiffies(wait)))
  166. return -ERESTARTSYS;
  167. }
  168. res = tea->ops->read_reg(tea, &tea->read_reg);
  169. if (res)
  170. return res;
  171. tea->needs_write = true;
  172. return 0;
  173. }
  174. /*
  175. * Linux Video interface
  176. */
  177. static int vidioc_querycap(struct file *file, void *priv,
  178. struct v4l2_capability *v)
  179. {
  180. struct radio_tea5777 *tea = video_drvdata(file);
  181. strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
  182. strlcpy(v->card, tea->card, sizeof(v->card));
  183. strlcat(v->card, " TEA5777", sizeof(v->card));
  184. strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
  185. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  186. v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
  187. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  188. return 0;
  189. }
  190. static int vidioc_g_tuner(struct file *file, void *priv,
  191. struct v4l2_tuner *v)
  192. {
  193. struct radio_tea5777 *tea = video_drvdata(file);
  194. int res;
  195. if (v->index > 0)
  196. return -EINVAL;
  197. res = radio_tea5777_update_read_reg(tea, 0);
  198. if (res)
  199. return res;
  200. memset(v, 0, sizeof(*v));
  201. if (tea->has_am)
  202. strlcpy(v->name, "AM/FM", sizeof(v->name));
  203. else
  204. strlcpy(v->name, "FM", sizeof(v->name));
  205. v->type = V4L2_TUNER_RADIO;
  206. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  207. V4L2_TUNER_CAP_HWSEEK_BOUNDED;
  208. v->rangelow = TEA5777_FM_RANGELOW;
  209. v->rangehigh = TEA5777_FM_RANGEHIGH;
  210. v->rxsubchans = (tea->read_reg & TEA5777_R_FM_STEREO_MASK) ?
  211. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  212. v->audmode = (tea->write_reg & TEA5777_W_FM_FORCEMONO_MASK) ?
  213. V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
  214. /* shift - 12 to convert 4-bits (0-15) scale to 16-bits (0-65535) */
  215. v->signal = (tea->read_reg & TEA5777_R_LEVEL_MASK) >>
  216. (TEA5777_R_LEVEL_SHIFT - 12);
  217. /* Invalidate read_reg, so that next call we return up2date signal */
  218. tea->read_reg = -1;
  219. return 0;
  220. }
  221. static int vidioc_s_tuner(struct file *file, void *priv,
  222. struct v4l2_tuner *v)
  223. {
  224. struct radio_tea5777 *tea = video_drvdata(file);
  225. if (v->index)
  226. return -EINVAL;
  227. if (v->audmode == V4L2_TUNER_MODE_MONO)
  228. tea->write_reg |= TEA5777_W_FM_FORCEMONO_MASK;
  229. else
  230. tea->write_reg &= ~TEA5777_W_FM_FORCEMONO_MASK;
  231. return radio_tea5777_set_freq(tea);
  232. }
  233. static int vidioc_g_frequency(struct file *file, void *priv,
  234. struct v4l2_frequency *f)
  235. {
  236. struct radio_tea5777 *tea = video_drvdata(file);
  237. if (f->tuner != 0)
  238. return -EINVAL;
  239. f->type = V4L2_TUNER_RADIO;
  240. f->frequency = tea->freq;
  241. return 0;
  242. }
  243. static int vidioc_s_frequency(struct file *file, void *priv,
  244. struct v4l2_frequency *f)
  245. {
  246. struct radio_tea5777 *tea = video_drvdata(file);
  247. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  248. return -EINVAL;
  249. tea->freq = f->frequency;
  250. return radio_tea5777_set_freq(tea);
  251. }
  252. static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
  253. struct v4l2_hw_freq_seek *a)
  254. {
  255. struct radio_tea5777 *tea = video_drvdata(file);
  256. u32 orig_freq = tea->freq;
  257. unsigned long timeout;
  258. int res, spacing = 200 * 16; /* 200 kHz */
  259. /* These are fixed *for now* */
  260. const u32 seek_rangelow = TEA5777_FM_RANGELOW;
  261. const u32 seek_rangehigh = TEA5777_FM_RANGEHIGH;
  262. if (a->tuner || a->wrap_around)
  263. return -EINVAL;
  264. tea->write_reg |= TEA5777_W_PROGBLIM_MASK;
  265. if (seek_rangelow != tea->seek_rangelow) {
  266. tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
  267. tea->freq = seek_rangelow;
  268. res = radio_tea5777_set_freq(tea);
  269. if (res)
  270. goto leave;
  271. tea->seek_rangelow = tea->freq;
  272. }
  273. if (seek_rangehigh != tea->seek_rangehigh) {
  274. tea->write_reg |= TEA5777_W_UPDWN_MASK;
  275. tea->freq = seek_rangehigh;
  276. res = radio_tea5777_set_freq(tea);
  277. if (res)
  278. goto leave;
  279. tea->seek_rangehigh = tea->freq;
  280. }
  281. tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
  282. tea->write_reg |= TEA5777_W_SEARCH_MASK;
  283. if (a->seek_upward) {
  284. tea->write_reg |= TEA5777_W_UPDWN_MASK;
  285. tea->freq = orig_freq + spacing;
  286. } else {
  287. tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
  288. tea->freq = orig_freq - spacing;
  289. }
  290. res = radio_tea5777_set_freq(tea);
  291. if (res)
  292. goto leave;
  293. timeout = jiffies + msecs_to_jiffies(5000);
  294. for (;;) {
  295. if (time_after(jiffies, timeout)) {
  296. res = -ENODATA;
  297. break;
  298. }
  299. res = radio_tea5777_update_read_reg(tea, 100);
  300. if (res)
  301. break;
  302. /*
  303. * Note we use tea->freq to track how far we've searched sofar
  304. * this is necessary to ensure we continue seeking at the right
  305. * point, in the write_before_read case.
  306. */
  307. tea->freq = (tea->read_reg & TEA5777_R_FM_PLL_MASK);
  308. tea->freq = tea5777_freq_to_v4l2_freq(tea, tea->freq);
  309. if ((tea->read_reg & TEA5777_R_SFOUND_MASK)) {
  310. tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
  311. return 0;
  312. }
  313. if (tea->read_reg & TEA5777_R_BLIM_MASK) {
  314. res = -ENODATA;
  315. break;
  316. }
  317. /* Force read_reg update */
  318. tea->read_reg = -1;
  319. }
  320. leave:
  321. tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
  322. tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
  323. tea->freq = orig_freq;
  324. radio_tea5777_set_freq(tea);
  325. return res;
  326. }
  327. static int tea575x_s_ctrl(struct v4l2_ctrl *c)
  328. {
  329. struct radio_tea5777 *tea =
  330. container_of(c->handler, struct radio_tea5777, ctrl_handler);
  331. switch (c->id) {
  332. case V4L2_CID_AUDIO_MUTE:
  333. if (c->val)
  334. tea->write_reg |= TEA5777_W_MUTE_MASK;
  335. else
  336. tea->write_reg &= ~TEA5777_W_MUTE_MASK;
  337. return radio_tea5777_set_freq(tea);
  338. }
  339. return -EINVAL;
  340. }
  341. static const struct v4l2_file_operations tea575x_fops = {
  342. .unlocked_ioctl = video_ioctl2,
  343. .open = v4l2_fh_open,
  344. .release = v4l2_fh_release,
  345. .poll = v4l2_ctrl_poll,
  346. };
  347. static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
  348. .vidioc_querycap = vidioc_querycap,
  349. .vidioc_g_tuner = vidioc_g_tuner,
  350. .vidioc_s_tuner = vidioc_s_tuner,
  351. .vidioc_g_frequency = vidioc_g_frequency,
  352. .vidioc_s_frequency = vidioc_s_frequency,
  353. .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
  354. .vidioc_log_status = v4l2_ctrl_log_status,
  355. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  356. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  357. };
  358. static const struct video_device tea575x_radio = {
  359. .ioctl_ops = &tea575x_ioctl_ops,
  360. .release = video_device_release_empty,
  361. };
  362. static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
  363. .s_ctrl = tea575x_s_ctrl,
  364. };
  365. int radio_tea5777_init(struct radio_tea5777 *tea, struct module *owner)
  366. {
  367. int res;
  368. tea->write_reg = (1LL << TEA5777_W_IFCE_SHIFT) |
  369. (1LL << TEA5777_W_IFW_SHIFT) |
  370. (1LL << TEA5777_W_INTEXT_SHIFT) |
  371. (1LL << TEA5777_W_CHP0_SHIFT) |
  372. (2LL << TEA5777_W_SLEV_SHIFT);
  373. tea->freq = 90500 * 16; /* 90.5Mhz default */
  374. res = radio_tea5777_set_freq(tea);
  375. if (res) {
  376. v4l2_err(tea->v4l2_dev, "can't set initial freq (%d)\n", res);
  377. return res;
  378. }
  379. tea->vd = tea575x_radio;
  380. video_set_drvdata(&tea->vd, tea);
  381. mutex_init(&tea->mutex);
  382. strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
  383. tea->vd.lock = &tea->mutex;
  384. tea->vd.v4l2_dev = tea->v4l2_dev;
  385. tea->fops = tea575x_fops;
  386. tea->fops.owner = owner;
  387. tea->vd.fops = &tea->fops;
  388. set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
  389. tea->vd.ctrl_handler = &tea->ctrl_handler;
  390. v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
  391. v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
  392. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  393. res = tea->ctrl_handler.error;
  394. if (res) {
  395. v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
  396. v4l2_ctrl_handler_free(&tea->ctrl_handler);
  397. return res;
  398. }
  399. v4l2_ctrl_handler_setup(&tea->ctrl_handler);
  400. res = video_register_device(&tea->vd, VFL_TYPE_RADIO, -1);
  401. if (res) {
  402. v4l2_err(tea->v4l2_dev, "can't register video device!\n");
  403. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  404. return res;
  405. }
  406. return 0;
  407. }
  408. EXPORT_SYMBOL_GPL(radio_tea5777_init);
  409. void radio_tea5777_exit(struct radio_tea5777 *tea)
  410. {
  411. video_unregister_device(&tea->vd);
  412. v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
  413. }
  414. EXPORT_SYMBOL_GPL(radio_tea5777_exit);