radio-si470x-common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * drivers/media/radio/si470x/radio-si470x-common.c
  3. *
  4. * Driver for radios with Silicon Labs Si470x FM Radio Receivers
  5. *
  6. * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
  7. * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. /*
  20. * History:
  21. * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
  22. * Version 1.0.0
  23. * - First working version
  24. * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
  25. * Version 1.0.1
  26. * - Improved error handling, every function now returns errno
  27. * - Improved multi user access (start/mute/stop)
  28. * - Channel doesn't get lost anymore after start/mute/stop
  29. * - RDS support added (polling mode via interrupt EP 1)
  30. * - marked default module parameters with *value*
  31. * - switched from bit structs to bit masks
  32. * - header file cleaned and integrated
  33. * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
  34. * Version 1.0.2
  35. * - hex values are now lower case
  36. * - commented USB ID for ADS/Tech moved on todo list
  37. * - blacklisted si470x in hid-quirks.c
  38. * - rds buffer handling functions integrated into *_work, *_read
  39. * - rds_command in si470x_poll exchanged against simple retval
  40. * - check for firmware version 15
  41. * - code order and prototypes still remain the same
  42. * - spacing and bottom of band codes remain the same
  43. * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
  44. * Version 1.0.3
  45. * - code reordered to avoid function prototypes
  46. * - switch/case defaults are now more user-friendly
  47. * - unified comment style
  48. * - applied all checkpatch.pl v1.12 suggestions
  49. * except the warning about the too long lines with bit comments
  50. * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
  51. * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
  52. * Version 1.0.4
  53. * - avoid poss. locking when doing copy_to_user which may sleep
  54. * - RDS is automatically activated on read now
  55. * - code cleaned of unnecessary rds_commands
  56. * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
  57. * (thanks to Guillaume RAMOUSSE)
  58. * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
  59. * Version 1.0.5
  60. * - number of seek_retries changed to tune_timeout
  61. * - fixed problem with incomplete tune operations by own buffers
  62. * - optimization of variables and printf types
  63. * - improved error logging
  64. * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
  65. * Oliver Neukum <oliver@neukum.org>
  66. * Version 1.0.6
  67. * - fixed coverity checker warnings in *_usb_driver_disconnect
  68. * - probe()/open() race by correct ordering in probe()
  69. * - DMA coherency rules by separate allocation of all buffers
  70. * - use of endianness macros
  71. * - abuse of spinlock, replaced by mutex
  72. * - racy handling of timer in disconnect,
  73. * replaced by delayed_work
  74. * - racy interruptible_sleep_on(),
  75. * replaced with wait_event_interruptible()
  76. * - handle signals in read()
  77. * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
  78. * Oliver Neukum <oliver@neukum.org>
  79. * Version 1.0.7
  80. * - usb autosuspend support
  81. * - unplugging fixed
  82. * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
  83. * Version 1.0.8
  84. * - hardware frequency seek support
  85. * - afc indication
  86. * - more safety checks, let si470x_get_freq return errno
  87. * - vidioc behavior corrected according to v4l2 spec
  88. * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com>
  89. * - add support for KWorld USB FM Radio FM700
  90. * - blacklisted KWorld radio in hid-core.c and hid-ids.h
  91. * 2008-12-03 Mark Lord <mlord@pobox.com>
  92. * - add support for DealExtreme USB Radio
  93. * 2009-01-31 Bob Ross <pigiron@gmx.com>
  94. * - correction of stereo detection/setting
  95. * - correction of signal strength indicator scaling
  96. * 2009-01-31 Rick Bronson <rick@efn.org>
  97. * Tobias Lorenz <tobias.lorenz@gmx.net>
  98. * - add LED status output
  99. * - get HW/SW version from scratchpad
  100. * 2009-06-16 Edouard Lafargue <edouard@lafargue.name>
  101. * Version 1.0.10
  102. * - add support for interrupt mode for RDS endpoint,
  103. * instead of polling.
  104. * Improves RDS reception significantly
  105. */
  106. /* kernel includes */
  107. #include "radio-si470x.h"
  108. /**************************************************************************
  109. * Module Parameters
  110. **************************************************************************/
  111. /* Spacing (kHz) */
  112. /* 0: 200 kHz (USA, Australia) */
  113. /* 1: 100 kHz (Europe, Japan) */
  114. /* 2: 50 kHz */
  115. static unsigned short space = 2;
  116. module_param(space, ushort, 0444);
  117. MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
  118. /* De-emphasis */
  119. /* 0: 75 us (USA) */
  120. /* 1: 50 us (Europe, Australia, Japan) */
  121. static unsigned short de = 1;
  122. module_param(de, ushort, 0444);
  123. MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
  124. /* Tune timeout */
  125. static unsigned int tune_timeout = 3000;
  126. module_param(tune_timeout, uint, 0644);
  127. MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
  128. /* Seek timeout */
  129. static unsigned int seek_timeout = 5000;
  130. module_param(seek_timeout, uint, 0644);
  131. MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
  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_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  138. V4L2_TUNER_CAP_FREQ_BANDS |
  139. V4L2_TUNER_CAP_HWSEEK_BOUNDED |
  140. V4L2_TUNER_CAP_HWSEEK_WRAP,
  141. .rangelow = 87500 * 16,
  142. .rangehigh = 108000 * 16,
  143. .modulation = V4L2_BAND_MODULATION_FM,
  144. },
  145. {
  146. .type = V4L2_TUNER_RADIO,
  147. .index = 1,
  148. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  149. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  150. V4L2_TUNER_CAP_FREQ_BANDS |
  151. V4L2_TUNER_CAP_HWSEEK_BOUNDED |
  152. V4L2_TUNER_CAP_HWSEEK_WRAP,
  153. .rangelow = 76000 * 16,
  154. .rangehigh = 108000 * 16,
  155. .modulation = V4L2_BAND_MODULATION_FM,
  156. },
  157. {
  158. .type = V4L2_TUNER_RADIO,
  159. .index = 2,
  160. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  161. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  162. V4L2_TUNER_CAP_FREQ_BANDS |
  163. V4L2_TUNER_CAP_HWSEEK_BOUNDED |
  164. V4L2_TUNER_CAP_HWSEEK_WRAP,
  165. .rangelow = 76000 * 16,
  166. .rangehigh = 90000 * 16,
  167. .modulation = V4L2_BAND_MODULATION_FM,
  168. },
  169. };
  170. /**************************************************************************
  171. * Generic Functions
  172. **************************************************************************/
  173. /*
  174. * si470x_set_band - set the band
  175. */
  176. static int si470x_set_band(struct si470x_device *radio, int band)
  177. {
  178. if (radio->band == band)
  179. return 0;
  180. radio->band = band;
  181. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
  182. radio->registers[SYSCONFIG2] |= radio->band << 6;
  183. return radio->set_register(radio, SYSCONFIG2);
  184. }
  185. /*
  186. * si470x_set_chan - set the channel
  187. */
  188. static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
  189. {
  190. int retval;
  191. unsigned long time_left;
  192. bool timed_out = false;
  193. retval = radio->get_register(radio, POWERCFG);
  194. if (retval)
  195. return retval;
  196. if ((radio->registers[POWERCFG] & (POWERCFG_ENABLE|POWERCFG_DMUTE))
  197. != (POWERCFG_ENABLE|POWERCFG_DMUTE)) {
  198. return 0;
  199. }
  200. /* start tuning */
  201. radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
  202. radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
  203. retval = radio->set_register(radio, CHANNEL);
  204. if (retval < 0)
  205. goto done;
  206. /* wait till tune operation has completed */
  207. reinit_completion(&radio->completion);
  208. time_left = wait_for_completion_timeout(&radio->completion,
  209. msecs_to_jiffies(tune_timeout));
  210. if (time_left == 0)
  211. timed_out = true;
  212. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  213. dev_warn(&radio->videodev.dev, "tune does not complete\n");
  214. if (timed_out)
  215. dev_warn(&radio->videodev.dev,
  216. "tune timed out after %u ms\n", tune_timeout);
  217. /* stop tuning */
  218. radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
  219. retval = radio->set_register(radio, CHANNEL);
  220. done:
  221. return retval;
  222. }
  223. /*
  224. * si470x_get_step - get channel spacing
  225. */
  226. static unsigned int si470x_get_step(struct si470x_device *radio)
  227. {
  228. /* Spacing (kHz) */
  229. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
  230. /* 0: 200 kHz (USA, Australia) */
  231. case 0:
  232. return 200 * 16;
  233. /* 1: 100 kHz (Europe, Japan) */
  234. case 1:
  235. return 100 * 16;
  236. /* 2: 50 kHz */
  237. default:
  238. return 50 * 16;
  239. }
  240. }
  241. /*
  242. * si470x_get_freq - get the frequency
  243. */
  244. static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
  245. {
  246. int chan, retval;
  247. /* read channel */
  248. retval = radio->get_register(radio, READCHAN);
  249. chan = radio->registers[READCHAN] & READCHAN_READCHAN;
  250. /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
  251. *freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow;
  252. return retval;
  253. }
  254. /*
  255. * si470x_set_freq - set the frequency
  256. */
  257. int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
  258. {
  259. unsigned short chan;
  260. freq = clamp(freq, bands[radio->band].rangelow,
  261. bands[radio->band].rangehigh);
  262. /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
  263. chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio);
  264. return si470x_set_chan(radio, chan);
  265. }
  266. EXPORT_SYMBOL_GPL(si470x_set_freq);
  267. /*
  268. * si470x_set_seek - set seek
  269. */
  270. static int si470x_set_seek(struct si470x_device *radio,
  271. const struct v4l2_hw_freq_seek *seek)
  272. {
  273. int band, retval;
  274. unsigned int freq;
  275. bool timed_out = false;
  276. unsigned long time_left;
  277. /* set band */
  278. if (seek->rangelow || seek->rangehigh) {
  279. for (band = 0; band < ARRAY_SIZE(bands); band++) {
  280. if (bands[band].rangelow == seek->rangelow &&
  281. bands[band].rangehigh == seek->rangehigh)
  282. break;
  283. }
  284. if (band == ARRAY_SIZE(bands))
  285. return -EINVAL; /* No matching band found */
  286. } else
  287. band = 1; /* If nothing is specified seek 76 - 108 Mhz */
  288. if (radio->band != band) {
  289. retval = si470x_get_freq(radio, &freq);
  290. if (retval)
  291. return retval;
  292. retval = si470x_set_band(radio, band);
  293. if (retval)
  294. return retval;
  295. retval = si470x_set_freq(radio, freq);
  296. if (retval)
  297. return retval;
  298. }
  299. /* start seeking */
  300. radio->registers[POWERCFG] |= POWERCFG_SEEK;
  301. if (seek->wrap_around)
  302. radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
  303. else
  304. radio->registers[POWERCFG] |= POWERCFG_SKMODE;
  305. if (seek->seek_upward)
  306. radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
  307. else
  308. radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
  309. retval = radio->set_register(radio, POWERCFG);
  310. if (retval < 0)
  311. return retval;
  312. /* wait till tune operation has completed */
  313. reinit_completion(&radio->completion);
  314. time_left = wait_for_completion_timeout(&radio->completion,
  315. msecs_to_jiffies(seek_timeout));
  316. if (time_left == 0)
  317. timed_out = true;
  318. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  319. dev_warn(&radio->videodev.dev, "seek does not complete\n");
  320. if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
  321. dev_warn(&radio->videodev.dev,
  322. "seek failed / band limit reached\n");
  323. /* stop seeking */
  324. radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
  325. retval = radio->set_register(radio, POWERCFG);
  326. /* try again, if timed out */
  327. if (retval == 0 && timed_out)
  328. return -ENODATA;
  329. return retval;
  330. }
  331. /*
  332. * si470x_start - switch on radio
  333. */
  334. int si470x_start(struct si470x_device *radio)
  335. {
  336. int retval;
  337. /* powercfg */
  338. radio->registers[POWERCFG] =
  339. POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
  340. retval = radio->set_register(radio, POWERCFG);
  341. if (retval < 0)
  342. goto done;
  343. /* sysconfig 1 */
  344. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN | SYSCONFIG1_STCIEN |
  345. SYSCONFIG1_RDS;
  346. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2;
  347. radio->registers[SYSCONFIG1] |= SYSCONFIG1_GPIO2_INT;
  348. if (de)
  349. radio->registers[SYSCONFIG1] |= SYSCONFIG1_DE;
  350. retval = radio->set_register(radio, SYSCONFIG1);
  351. if (retval < 0)
  352. goto done;
  353. /* sysconfig 2 */
  354. radio->registers[SYSCONFIG2] =
  355. (0x1f << 8) | /* SEEKTH */
  356. ((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */
  357. ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
  358. 15; /* VOLUME (max) */
  359. retval = radio->set_register(radio, SYSCONFIG2);
  360. if (retval < 0)
  361. goto done;
  362. /* reset last channel */
  363. retval = si470x_set_chan(radio,
  364. radio->registers[CHANNEL] & CHANNEL_CHAN);
  365. done:
  366. return retval;
  367. }
  368. EXPORT_SYMBOL_GPL(si470x_start);
  369. /*
  370. * si470x_stop - switch off radio
  371. */
  372. int si470x_stop(struct si470x_device *radio)
  373. {
  374. int retval;
  375. /* sysconfig 1 */
  376. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  377. retval = radio->set_register(radio, SYSCONFIG1);
  378. if (retval < 0)
  379. goto done;
  380. /* powercfg */
  381. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  382. /* POWERCFG_ENABLE has to automatically go low */
  383. radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
  384. retval = radio->set_register(radio, POWERCFG);
  385. done:
  386. return retval;
  387. }
  388. EXPORT_SYMBOL_GPL(si470x_stop);
  389. /*
  390. * si470x_rds_on - switch on rds reception
  391. */
  392. static int si470x_rds_on(struct si470x_device *radio)
  393. {
  394. int retval;
  395. /* sysconfig 1 */
  396. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
  397. retval = radio->set_register(radio, SYSCONFIG1);
  398. if (retval < 0)
  399. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  400. return retval;
  401. }
  402. /**************************************************************************
  403. * File Operations Interface
  404. **************************************************************************/
  405. /*
  406. * si470x_fops_read - read RDS data
  407. */
  408. static ssize_t si470x_fops_read(struct file *file, char __user *buf,
  409. size_t count, loff_t *ppos)
  410. {
  411. struct si470x_device *radio = video_drvdata(file);
  412. int retval = 0;
  413. unsigned int block_count = 0;
  414. /* switch on rds reception */
  415. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  416. si470x_rds_on(radio);
  417. /* block if no new data available */
  418. while (radio->wr_index == radio->rd_index) {
  419. if (file->f_flags & O_NONBLOCK) {
  420. retval = -EWOULDBLOCK;
  421. goto done;
  422. }
  423. if (wait_event_interruptible(radio->read_queue,
  424. radio->wr_index != radio->rd_index) < 0) {
  425. retval = -EINTR;
  426. goto done;
  427. }
  428. }
  429. /* calculate block count from byte count */
  430. count /= 3;
  431. /* copy RDS block out of internal buffer and to user buffer */
  432. while (block_count < count) {
  433. if (radio->rd_index == radio->wr_index)
  434. break;
  435. /* always transfer rds complete blocks */
  436. if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
  437. /* retval = -EFAULT; */
  438. break;
  439. /* increment and wrap read pointer */
  440. radio->rd_index += 3;
  441. if (radio->rd_index >= radio->buf_size)
  442. radio->rd_index = 0;
  443. /* increment counters */
  444. block_count++;
  445. buf += 3;
  446. retval += 3;
  447. }
  448. done:
  449. return retval;
  450. }
  451. /*
  452. * si470x_fops_poll - poll RDS data
  453. */
  454. static __poll_t si470x_fops_poll(struct file *file,
  455. struct poll_table_struct *pts)
  456. {
  457. struct si470x_device *radio = video_drvdata(file);
  458. __poll_t req_events = poll_requested_events(pts);
  459. __poll_t retval = v4l2_ctrl_poll(file, pts);
  460. if (req_events & (EPOLLIN | EPOLLRDNORM)) {
  461. /* switch on rds reception */
  462. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  463. si470x_rds_on(radio);
  464. poll_wait(file, &radio->read_queue, pts);
  465. if (radio->rd_index != radio->wr_index)
  466. retval |= EPOLLIN | EPOLLRDNORM;
  467. }
  468. return retval;
  469. }
  470. static int si470x_fops_open(struct file *file)
  471. {
  472. struct si470x_device *radio = video_drvdata(file);
  473. return radio->fops_open(file);
  474. }
  475. /*
  476. * si470x_fops_release - file release
  477. */
  478. static int si470x_fops_release(struct file *file)
  479. {
  480. struct si470x_device *radio = video_drvdata(file);
  481. return radio->fops_release(file);
  482. }
  483. /*
  484. * si470x_fops - file operations interface
  485. */
  486. static const struct v4l2_file_operations si470x_fops = {
  487. .owner = THIS_MODULE,
  488. .read = si470x_fops_read,
  489. .poll = si470x_fops_poll,
  490. .unlocked_ioctl = video_ioctl2,
  491. .open = si470x_fops_open,
  492. .release = si470x_fops_release,
  493. };
  494. /**************************************************************************
  495. * Video4Linux Interface
  496. **************************************************************************/
  497. static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
  498. {
  499. struct si470x_device *radio =
  500. container_of(ctrl->handler, struct si470x_device, hdl);
  501. switch (ctrl->id) {
  502. case V4L2_CID_AUDIO_VOLUME:
  503. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
  504. radio->registers[SYSCONFIG2] |= ctrl->val;
  505. return radio->set_register(radio, SYSCONFIG2);
  506. case V4L2_CID_AUDIO_MUTE:
  507. if (ctrl->val)
  508. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  509. else
  510. radio->registers[POWERCFG] |= POWERCFG_DMUTE;
  511. return radio->set_register(radio, POWERCFG);
  512. default:
  513. return -EINVAL;
  514. }
  515. }
  516. /*
  517. * si470x_vidioc_g_tuner - get tuner attributes
  518. */
  519. static int si470x_vidioc_g_tuner(struct file *file, void *priv,
  520. struct v4l2_tuner *tuner)
  521. {
  522. struct si470x_device *radio = video_drvdata(file);
  523. int retval = 0;
  524. if (tuner->index != 0)
  525. return -EINVAL;
  526. if (!radio->status_rssi_auto_update) {
  527. retval = radio->get_register(radio, STATUSRSSI);
  528. if (retval < 0)
  529. return retval;
  530. }
  531. /* driver constants */
  532. strcpy(tuner->name, "FM");
  533. tuner->type = V4L2_TUNER_RADIO;
  534. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  535. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  536. V4L2_TUNER_CAP_HWSEEK_BOUNDED |
  537. V4L2_TUNER_CAP_HWSEEK_WRAP;
  538. tuner->rangelow = 76 * FREQ_MUL;
  539. tuner->rangehigh = 108 * FREQ_MUL;
  540. /* stereo indicator == stereo (instead of mono) */
  541. if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
  542. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  543. else
  544. tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
  545. /* If there is a reliable method of detecting an RDS channel,
  546. then this code should check for that before setting this
  547. RDS subchannel. */
  548. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  549. /* mono/stereo selector */
  550. if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
  551. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  552. else
  553. tuner->audmode = V4L2_TUNER_MODE_MONO;
  554. /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
  555. /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
  556. tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
  557. /* the ideal factor is 0xffff/75 = 873,8 */
  558. tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
  559. if (tuner->signal > 0xffff)
  560. tuner->signal = 0xffff;
  561. /* automatic frequency control: -1: freq to low, 1 freq to high */
  562. /* AFCRL does only indicate that freq. differs, not if too low/high */
  563. tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
  564. return retval;
  565. }
  566. /*
  567. * si470x_vidioc_s_tuner - set tuner attributes
  568. */
  569. static int si470x_vidioc_s_tuner(struct file *file, void *priv,
  570. const struct v4l2_tuner *tuner)
  571. {
  572. struct si470x_device *radio = video_drvdata(file);
  573. if (tuner->index != 0)
  574. return -EINVAL;
  575. /* mono/stereo selector */
  576. switch (tuner->audmode) {
  577. case V4L2_TUNER_MODE_MONO:
  578. radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
  579. break;
  580. case V4L2_TUNER_MODE_STEREO:
  581. default:
  582. radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
  583. break;
  584. }
  585. return radio->set_register(radio, POWERCFG);
  586. }
  587. /*
  588. * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
  589. */
  590. static int si470x_vidioc_g_frequency(struct file *file, void *priv,
  591. struct v4l2_frequency *freq)
  592. {
  593. struct si470x_device *radio = video_drvdata(file);
  594. if (freq->tuner != 0)
  595. return -EINVAL;
  596. freq->type = V4L2_TUNER_RADIO;
  597. return si470x_get_freq(radio, &freq->frequency);
  598. }
  599. /*
  600. * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
  601. */
  602. static int si470x_vidioc_s_frequency(struct file *file, void *priv,
  603. const struct v4l2_frequency *freq)
  604. {
  605. struct si470x_device *radio = video_drvdata(file);
  606. int retval;
  607. if (freq->tuner != 0)
  608. return -EINVAL;
  609. if (freq->frequency < bands[radio->band].rangelow ||
  610. freq->frequency > bands[radio->band].rangehigh) {
  611. /* Switch to band 1 which covers everything we support */
  612. retval = si470x_set_band(radio, 1);
  613. if (retval)
  614. return retval;
  615. }
  616. return si470x_set_freq(radio, freq->frequency);
  617. }
  618. /*
  619. * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
  620. */
  621. static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  622. const struct v4l2_hw_freq_seek *seek)
  623. {
  624. struct si470x_device *radio = video_drvdata(file);
  625. if (seek->tuner != 0)
  626. return -EINVAL;
  627. if (file->f_flags & O_NONBLOCK)
  628. return -EWOULDBLOCK;
  629. return si470x_set_seek(radio, seek);
  630. }
  631. /*
  632. * si470x_vidioc_enum_freq_bands - enumerate supported bands
  633. */
  634. static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv,
  635. struct v4l2_frequency_band *band)
  636. {
  637. if (band->tuner != 0)
  638. return -EINVAL;
  639. if (band->index >= ARRAY_SIZE(bands))
  640. return -EINVAL;
  641. *band = bands[band->index];
  642. return 0;
  643. }
  644. const struct v4l2_ctrl_ops si470x_ctrl_ops = {
  645. .s_ctrl = si470x_s_ctrl,
  646. };
  647. EXPORT_SYMBOL_GPL(si470x_ctrl_ops);
  648. static int si470x_vidioc_querycap(struct file *file, void *priv,
  649. struct v4l2_capability *capability)
  650. {
  651. struct si470x_device *radio = video_drvdata(file);
  652. return radio->vidioc_querycap(file, priv, capability);
  653. };
  654. /*
  655. * si470x_ioctl_ops - video device ioctl operations
  656. */
  657. static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
  658. .vidioc_querycap = si470x_vidioc_querycap,
  659. .vidioc_g_tuner = si470x_vidioc_g_tuner,
  660. .vidioc_s_tuner = si470x_vidioc_s_tuner,
  661. .vidioc_g_frequency = si470x_vidioc_g_frequency,
  662. .vidioc_s_frequency = si470x_vidioc_s_frequency,
  663. .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
  664. .vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands,
  665. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  666. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  667. };
  668. /*
  669. * si470x_viddev_template - video device interface
  670. */
  671. const struct video_device si470x_viddev_template = {
  672. .fops = &si470x_fops,
  673. .name = DRIVER_NAME,
  674. .release = video_device_release_empty,
  675. .ioctl_ops = &si470x_ioctl_ops,
  676. };
  677. EXPORT_SYMBOL_GPL(si470x_viddev_template);
  678. MODULE_LICENSE("GPL");