radio-tea5777.c 17 KB

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