radio-si470x-common.c 21 KB

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