radio-si470x-common.c 21 KB

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