radio-si470x-common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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_HWSEEK_BOUNDED |
  143. V4L2_TUNER_CAP_HWSEEK_WRAP,
  144. .rangelow = 87500 * 16,
  145. .rangehigh = 108000 * 16,
  146. .modulation = V4L2_BAND_MODULATION_FM,
  147. },
  148. {
  149. .type = V4L2_TUNER_RADIO,
  150. .index = 1,
  151. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  152. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  153. V4L2_TUNER_CAP_HWSEEK_BOUNDED |
  154. V4L2_TUNER_CAP_HWSEEK_WRAP,
  155. .rangelow = 76000 * 16,
  156. .rangehigh = 108000 * 16,
  157. .modulation = V4L2_BAND_MODULATION_FM,
  158. },
  159. {
  160. .type = V4L2_TUNER_RADIO,
  161. .index = 2,
  162. .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  163. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  164. V4L2_TUNER_CAP_HWSEEK_BOUNDED |
  165. V4L2_TUNER_CAP_HWSEEK_WRAP,
  166. .rangelow = 76000 * 16,
  167. .rangehigh = 90000 * 16,
  168. .modulation = V4L2_BAND_MODULATION_FM,
  169. },
  170. };
  171. /**************************************************************************
  172. * Generic Functions
  173. **************************************************************************/
  174. /*
  175. * si470x_set_band - set the band
  176. */
  177. static int si470x_set_band(struct si470x_device *radio, int band)
  178. {
  179. if (radio->band == band)
  180. return 0;
  181. radio->band = band;
  182. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
  183. radio->registers[SYSCONFIG2] |= radio->band << 6;
  184. return si470x_set_register(radio, SYSCONFIG2);
  185. }
  186. /*
  187. * si470x_set_chan - set the channel
  188. */
  189. static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
  190. {
  191. int retval;
  192. bool timed_out = 0;
  193. /* start tuning */
  194. radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
  195. radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
  196. retval = si470x_set_register(radio, CHANNEL);
  197. if (retval < 0)
  198. goto done;
  199. /* wait till tune operation has completed */
  200. INIT_COMPLETION(radio->completion);
  201. retval = wait_for_completion_timeout(&radio->completion,
  202. msecs_to_jiffies(tune_timeout));
  203. if (!retval)
  204. timed_out = true;
  205. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  206. dev_warn(&radio->videodev.dev, "tune does not complete\n");
  207. if (timed_out)
  208. dev_warn(&radio->videodev.dev,
  209. "tune timed out after %u ms\n", tune_timeout);
  210. /* stop tuning */
  211. radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
  212. retval = si470x_set_register(radio, CHANNEL);
  213. done:
  214. return retval;
  215. }
  216. /*
  217. * si470x_get_step - get channel spacing
  218. */
  219. static unsigned int si470x_get_step(struct si470x_device *radio)
  220. {
  221. /* Spacing (kHz) */
  222. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
  223. /* 0: 200 kHz (USA, Australia) */
  224. case 0:
  225. return 200 * 16;
  226. /* 1: 100 kHz (Europe, Japan) */
  227. case 1:
  228. return 100 * 16;
  229. /* 2: 50 kHz */
  230. default:
  231. return 50 * 16;
  232. };
  233. }
  234. /*
  235. * si470x_get_freq - get the frequency
  236. */
  237. static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
  238. {
  239. int chan, retval;
  240. /* read channel */
  241. retval = si470x_get_register(radio, READCHAN);
  242. chan = radio->registers[READCHAN] & READCHAN_READCHAN;
  243. /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
  244. *freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow;
  245. return retval;
  246. }
  247. /*
  248. * si470x_set_freq - set the frequency
  249. */
  250. int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
  251. {
  252. unsigned short chan;
  253. freq = clamp(freq, bands[radio->band].rangelow,
  254. bands[radio->band].rangehigh);
  255. /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
  256. chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio);
  257. return si470x_set_chan(radio, chan);
  258. }
  259. /*
  260. * si470x_set_seek - set seek
  261. */
  262. static int si470x_set_seek(struct si470x_device *radio,
  263. struct v4l2_hw_freq_seek *seek)
  264. {
  265. int band, retval;
  266. unsigned int freq;
  267. bool timed_out = 0;
  268. /* set band */
  269. if (seek->rangelow || seek->rangehigh) {
  270. for (band = 0; band < ARRAY_SIZE(bands); band++) {
  271. if (bands[band].rangelow == seek->rangelow &&
  272. bands[band].rangehigh == seek->rangehigh)
  273. break;
  274. }
  275. if (band == ARRAY_SIZE(bands))
  276. return -EINVAL; /* No matching band found */
  277. } else
  278. band = 1; /* If nothing is specified seek 76 - 108 Mhz */
  279. if (radio->band != band) {
  280. retval = si470x_get_freq(radio, &freq);
  281. if (retval)
  282. return retval;
  283. retval = si470x_set_band(radio, band);
  284. if (retval)
  285. return retval;
  286. retval = si470x_set_freq(radio, freq);
  287. if (retval)
  288. return retval;
  289. }
  290. /* start seeking */
  291. radio->registers[POWERCFG] |= POWERCFG_SEEK;
  292. if (seek->wrap_around)
  293. radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
  294. else
  295. radio->registers[POWERCFG] |= POWERCFG_SKMODE;
  296. if (seek->seek_upward)
  297. radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
  298. else
  299. radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
  300. retval = si470x_set_register(radio, POWERCFG);
  301. if (retval < 0)
  302. return retval;
  303. /* wait till tune operation has completed */
  304. INIT_COMPLETION(radio->completion);
  305. retval = wait_for_completion_timeout(&radio->completion,
  306. msecs_to_jiffies(seek_timeout));
  307. if (!retval)
  308. timed_out = true;
  309. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  310. dev_warn(&radio->videodev.dev, "seek does not complete\n");
  311. if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
  312. dev_warn(&radio->videodev.dev,
  313. "seek failed / band limit reached\n");
  314. /* stop seeking */
  315. radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
  316. retval = si470x_set_register(radio, POWERCFG);
  317. /* try again, if timed out */
  318. if (retval == 0 && timed_out)
  319. return -ENODATA;
  320. return retval;
  321. }
  322. /*
  323. * si470x_start - switch on radio
  324. */
  325. int si470x_start(struct si470x_device *radio)
  326. {
  327. int retval;
  328. /* powercfg */
  329. radio->registers[POWERCFG] =
  330. POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
  331. retval = si470x_set_register(radio, POWERCFG);
  332. if (retval < 0)
  333. goto done;
  334. /* sysconfig 1 */
  335. radio->registers[SYSCONFIG1] =
  336. (de << 11) & SYSCONFIG1_DE; /* DE*/
  337. retval = si470x_set_register(radio, SYSCONFIG1);
  338. if (retval < 0)
  339. goto done;
  340. /* sysconfig 2 */
  341. radio->registers[SYSCONFIG2] =
  342. (0x1f << 8) | /* SEEKTH */
  343. ((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */
  344. ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
  345. 15; /* VOLUME (max) */
  346. retval = si470x_set_register(radio, SYSCONFIG2);
  347. if (retval < 0)
  348. goto done;
  349. /* reset last channel */
  350. retval = si470x_set_chan(radio,
  351. radio->registers[CHANNEL] & CHANNEL_CHAN);
  352. done:
  353. return retval;
  354. }
  355. /*
  356. * si470x_stop - switch off radio
  357. */
  358. int si470x_stop(struct si470x_device *radio)
  359. {
  360. int retval;
  361. /* sysconfig 1 */
  362. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  363. retval = si470x_set_register(radio, SYSCONFIG1);
  364. if (retval < 0)
  365. goto done;
  366. /* powercfg */
  367. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  368. /* POWERCFG_ENABLE has to automatically go low */
  369. radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
  370. retval = si470x_set_register(radio, POWERCFG);
  371. done:
  372. return retval;
  373. }
  374. /*
  375. * si470x_rds_on - switch on rds reception
  376. */
  377. static int si470x_rds_on(struct si470x_device *radio)
  378. {
  379. int retval;
  380. /* sysconfig 1 */
  381. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
  382. retval = si470x_set_register(radio, SYSCONFIG1);
  383. if (retval < 0)
  384. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  385. return retval;
  386. }
  387. /**************************************************************************
  388. * File Operations Interface
  389. **************************************************************************/
  390. /*
  391. * si470x_fops_read - read RDS data
  392. */
  393. static ssize_t si470x_fops_read(struct file *file, char __user *buf,
  394. size_t count, loff_t *ppos)
  395. {
  396. struct si470x_device *radio = video_drvdata(file);
  397. int retval = 0;
  398. unsigned int block_count = 0;
  399. /* switch on rds reception */
  400. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  401. si470x_rds_on(radio);
  402. /* block if no new data available */
  403. while (radio->wr_index == radio->rd_index) {
  404. if (file->f_flags & O_NONBLOCK) {
  405. retval = -EWOULDBLOCK;
  406. goto done;
  407. }
  408. if (wait_event_interruptible(radio->read_queue,
  409. radio->wr_index != radio->rd_index) < 0) {
  410. retval = -EINTR;
  411. goto done;
  412. }
  413. }
  414. /* calculate block count from byte count */
  415. count /= 3;
  416. /* copy RDS block out of internal buffer and to user buffer */
  417. while (block_count < count) {
  418. if (radio->rd_index == radio->wr_index)
  419. break;
  420. /* always transfer rds complete blocks */
  421. if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
  422. /* retval = -EFAULT; */
  423. break;
  424. /* increment and wrap read pointer */
  425. radio->rd_index += 3;
  426. if (radio->rd_index >= radio->buf_size)
  427. radio->rd_index = 0;
  428. /* increment counters */
  429. block_count++;
  430. buf += 3;
  431. retval += 3;
  432. }
  433. done:
  434. return retval;
  435. }
  436. /*
  437. * si470x_fops_poll - poll RDS data
  438. */
  439. static unsigned int si470x_fops_poll(struct file *file,
  440. struct poll_table_struct *pts)
  441. {
  442. struct si470x_device *radio = video_drvdata(file);
  443. unsigned long req_events = poll_requested_events(pts);
  444. int retval = v4l2_ctrl_poll(file, pts);
  445. if (req_events & (POLLIN | POLLRDNORM)) {
  446. /* switch on rds reception */
  447. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  448. si470x_rds_on(radio);
  449. poll_wait(file, &radio->read_queue, pts);
  450. if (radio->rd_index != radio->wr_index)
  451. retval |= POLLIN | POLLRDNORM;
  452. }
  453. return retval;
  454. }
  455. /*
  456. * si470x_fops - file operations interface
  457. */
  458. static const struct v4l2_file_operations si470x_fops = {
  459. .owner = THIS_MODULE,
  460. .read = si470x_fops_read,
  461. .poll = si470x_fops_poll,
  462. .unlocked_ioctl = video_ioctl2,
  463. .open = si470x_fops_open,
  464. .release = si470x_fops_release,
  465. };
  466. /**************************************************************************
  467. * Video4Linux Interface
  468. **************************************************************************/
  469. static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
  470. {
  471. struct si470x_device *radio =
  472. container_of(ctrl->handler, struct si470x_device, hdl);
  473. switch (ctrl->id) {
  474. case V4L2_CID_AUDIO_VOLUME:
  475. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
  476. radio->registers[SYSCONFIG2] |= ctrl->val;
  477. return si470x_set_register(radio, SYSCONFIG2);
  478. case V4L2_CID_AUDIO_MUTE:
  479. if (ctrl->val)
  480. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  481. else
  482. radio->registers[POWERCFG] |= POWERCFG_DMUTE;
  483. return si470x_set_register(radio, POWERCFG);
  484. default:
  485. return -EINVAL;
  486. }
  487. }
  488. /*
  489. * si470x_vidioc_g_tuner - get tuner attributes
  490. */
  491. static int si470x_vidioc_g_tuner(struct file *file, void *priv,
  492. struct v4l2_tuner *tuner)
  493. {
  494. struct si470x_device *radio = video_drvdata(file);
  495. int retval = 0;
  496. if (tuner->index != 0)
  497. return -EINVAL;
  498. if (!radio->status_rssi_auto_update) {
  499. retval = si470x_get_register(radio, STATUSRSSI);
  500. if (retval < 0)
  501. return retval;
  502. }
  503. /* driver constants */
  504. strcpy(tuner->name, "FM");
  505. tuner->type = V4L2_TUNER_RADIO;
  506. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  507. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
  508. V4L2_TUNER_CAP_HWSEEK_BOUNDED |
  509. V4L2_TUNER_CAP_HWSEEK_WRAP;
  510. tuner->rangelow = 76 * FREQ_MUL;
  511. tuner->rangehigh = 108 * FREQ_MUL;
  512. /* stereo indicator == stereo (instead of mono) */
  513. if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
  514. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  515. else
  516. tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
  517. /* If there is a reliable method of detecting an RDS channel,
  518. then this code should check for that before setting this
  519. RDS subchannel. */
  520. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  521. /* mono/stereo selector */
  522. if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
  523. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  524. else
  525. tuner->audmode = V4L2_TUNER_MODE_MONO;
  526. /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
  527. /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
  528. tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
  529. /* the ideal factor is 0xffff/75 = 873,8 */
  530. tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
  531. if (tuner->signal > 0xffff)
  532. tuner->signal = 0xffff;
  533. /* automatic frequency control: -1: freq to low, 1 freq to high */
  534. /* AFCRL does only indicate that freq. differs, not if too low/high */
  535. tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
  536. return retval;
  537. }
  538. /*
  539. * si470x_vidioc_s_tuner - set tuner attributes
  540. */
  541. static int si470x_vidioc_s_tuner(struct file *file, void *priv,
  542. struct v4l2_tuner *tuner)
  543. {
  544. struct si470x_device *radio = video_drvdata(file);
  545. if (tuner->index != 0)
  546. return -EINVAL;
  547. /* mono/stereo selector */
  548. switch (tuner->audmode) {
  549. case V4L2_TUNER_MODE_MONO:
  550. radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
  551. break;
  552. case V4L2_TUNER_MODE_STEREO:
  553. default:
  554. radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
  555. break;
  556. }
  557. return si470x_set_register(radio, POWERCFG);
  558. }
  559. /*
  560. * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
  561. */
  562. static int si470x_vidioc_g_frequency(struct file *file, void *priv,
  563. struct v4l2_frequency *freq)
  564. {
  565. struct si470x_device *radio = video_drvdata(file);
  566. if (freq->tuner != 0)
  567. return -EINVAL;
  568. freq->type = V4L2_TUNER_RADIO;
  569. return si470x_get_freq(radio, &freq->frequency);
  570. }
  571. /*
  572. * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
  573. */
  574. static int si470x_vidioc_s_frequency(struct file *file, void *priv,
  575. struct v4l2_frequency *freq)
  576. {
  577. struct si470x_device *radio = video_drvdata(file);
  578. int retval;
  579. if (freq->tuner != 0)
  580. return -EINVAL;
  581. if (freq->frequency < bands[radio->band].rangelow ||
  582. freq->frequency > bands[radio->band].rangehigh) {
  583. /* Switch to band 1 which covers everything we support */
  584. retval = si470x_set_band(radio, 1);
  585. if (retval)
  586. return retval;
  587. }
  588. return si470x_set_freq(radio, freq->frequency);
  589. }
  590. /*
  591. * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
  592. */
  593. static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  594. struct v4l2_hw_freq_seek *seek)
  595. {
  596. struct si470x_device *radio = video_drvdata(file);
  597. if (seek->tuner != 0)
  598. return -EINVAL;
  599. return si470x_set_seek(radio, seek);
  600. }
  601. /*
  602. * si470x_vidioc_enum_freq_bands - enumerate supported bands
  603. */
  604. static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv,
  605. struct v4l2_frequency_band *band)
  606. {
  607. if (band->tuner != 0)
  608. return -EINVAL;
  609. if (band->index >= ARRAY_SIZE(bands))
  610. return -EINVAL;
  611. *band = bands[band->index];
  612. return 0;
  613. }
  614. const struct v4l2_ctrl_ops si470x_ctrl_ops = {
  615. .s_ctrl = si470x_s_ctrl,
  616. };
  617. /*
  618. * si470x_ioctl_ops - video device ioctl operations
  619. */
  620. static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
  621. .vidioc_querycap = si470x_vidioc_querycap,
  622. .vidioc_g_tuner = si470x_vidioc_g_tuner,
  623. .vidioc_s_tuner = si470x_vidioc_s_tuner,
  624. .vidioc_g_frequency = si470x_vidioc_g_frequency,
  625. .vidioc_s_frequency = si470x_vidioc_s_frequency,
  626. .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
  627. .vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands,
  628. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  629. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  630. };
  631. /*
  632. * si470x_viddev_template - video device interface
  633. */
  634. struct video_device si470x_viddev_template = {
  635. .fops = &si470x_fops,
  636. .name = DRIVER_NAME,
  637. .release = video_device_release_empty,
  638. .ioctl_ops = &si470x_ioctl_ops,
  639. };