radio-tea5777.c 14 KB

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