radio-tea5777.c 16 KB

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