radio-si470x-common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. /* wait till tune operation has completed */
  160. timeout = jiffies + msecs_to_jiffies(tune_timeout);
  161. do {
  162. retval = si470x_get_register(radio, STATUSRSSI);
  163. if (retval < 0)
  164. goto stop;
  165. timed_out = time_after(jiffies, timeout);
  166. } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
  167. (!timed_out));
  168. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  169. dev_warn(&radio->videodev->dev, "tune does not complete\n");
  170. if (timed_out)
  171. dev_warn(&radio->videodev->dev,
  172. "tune timed out after %u ms\n", tune_timeout);
  173. stop:
  174. /* stop tuning */
  175. radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
  176. retval = si470x_set_register(radio, CHANNEL);
  177. done:
  178. return retval;
  179. }
  180. /*
  181. * si470x_get_freq - get the frequency
  182. */
  183. static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
  184. {
  185. unsigned int spacing, band_bottom;
  186. unsigned short chan;
  187. int retval;
  188. /* Spacing (kHz) */
  189. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
  190. /* 0: 200 kHz (USA, Australia) */
  191. case 0:
  192. spacing = 0.200 * FREQ_MUL; break;
  193. /* 1: 100 kHz (Europe, Japan) */
  194. case 1:
  195. spacing = 0.100 * FREQ_MUL; break;
  196. /* 2: 50 kHz */
  197. default:
  198. spacing = 0.050 * FREQ_MUL; break;
  199. };
  200. /* Bottom of Band (MHz) */
  201. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
  202. /* 0: 87.5 - 108 MHz (USA, Europe) */
  203. case 0:
  204. band_bottom = 87.5 * FREQ_MUL; break;
  205. /* 1: 76 - 108 MHz (Japan wide band) */
  206. default:
  207. band_bottom = 76 * FREQ_MUL; break;
  208. /* 2: 76 - 90 MHz (Japan) */
  209. case 2:
  210. band_bottom = 76 * FREQ_MUL; break;
  211. };
  212. /* read channel */
  213. retval = si470x_get_register(radio, READCHAN);
  214. chan = radio->registers[READCHAN] & READCHAN_READCHAN;
  215. /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
  216. *freq = chan * spacing + band_bottom;
  217. return retval;
  218. }
  219. /*
  220. * si470x_set_freq - set the frequency
  221. */
  222. int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
  223. {
  224. unsigned int spacing, band_bottom;
  225. unsigned short chan;
  226. /* Spacing (kHz) */
  227. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
  228. /* 0: 200 kHz (USA, Australia) */
  229. case 0:
  230. spacing = 0.200 * FREQ_MUL; break;
  231. /* 1: 100 kHz (Europe, Japan) */
  232. case 1:
  233. spacing = 0.100 * FREQ_MUL; break;
  234. /* 2: 50 kHz */
  235. default:
  236. spacing = 0.050 * FREQ_MUL; break;
  237. };
  238. /* Bottom of Band (MHz) */
  239. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
  240. /* 0: 87.5 - 108 MHz (USA, Europe) */
  241. case 0:
  242. band_bottom = 87.5 * FREQ_MUL; break;
  243. /* 1: 76 - 108 MHz (Japan wide band) */
  244. default:
  245. band_bottom = 76 * FREQ_MUL; break;
  246. /* 2: 76 - 90 MHz (Japan) */
  247. case 2:
  248. band_bottom = 76 * FREQ_MUL; break;
  249. };
  250. /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
  251. chan = (freq - band_bottom) / spacing;
  252. return si470x_set_chan(radio, chan);
  253. }
  254. /*
  255. * si470x_set_seek - set seek
  256. */
  257. static int si470x_set_seek(struct si470x_device *radio,
  258. unsigned int wrap_around, unsigned int seek_upward)
  259. {
  260. int retval = 0;
  261. unsigned long timeout;
  262. bool timed_out = 0;
  263. /* start seeking */
  264. radio->registers[POWERCFG] |= POWERCFG_SEEK;
  265. if (wrap_around == 1)
  266. radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
  267. else
  268. radio->registers[POWERCFG] |= POWERCFG_SKMODE;
  269. if (seek_upward == 1)
  270. radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
  271. else
  272. radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
  273. retval = si470x_set_register(radio, POWERCFG);
  274. if (retval < 0)
  275. goto done;
  276. /* wait till seek operation has completed */
  277. timeout = jiffies + msecs_to_jiffies(seek_timeout);
  278. do {
  279. retval = si470x_get_register(radio, STATUSRSSI);
  280. if (retval < 0)
  281. goto stop;
  282. timed_out = time_after(jiffies, timeout);
  283. } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
  284. (!timed_out));
  285. if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
  286. dev_warn(&radio->videodev->dev, "seek does not complete\n");
  287. if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
  288. dev_warn(&radio->videodev->dev,
  289. "seek failed / band limit reached\n");
  290. if (timed_out)
  291. dev_warn(&radio->videodev->dev,
  292. "seek timed out after %u ms\n", seek_timeout);
  293. stop:
  294. /* stop seeking */
  295. radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
  296. retval = si470x_set_register(radio, POWERCFG);
  297. done:
  298. /* try again, if timed out */
  299. if ((retval == 0) && timed_out)
  300. retval = -EAGAIN;
  301. return retval;
  302. }
  303. /*
  304. * si470x_start - switch on radio
  305. */
  306. int si470x_start(struct si470x_device *radio)
  307. {
  308. int retval;
  309. /* powercfg */
  310. radio->registers[POWERCFG] =
  311. POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
  312. retval = si470x_set_register(radio, POWERCFG);
  313. if (retval < 0)
  314. goto done;
  315. /* sysconfig 1 */
  316. radio->registers[SYSCONFIG1] = SYSCONFIG1_DE;
  317. retval = si470x_set_register(radio, SYSCONFIG1);
  318. if (retval < 0)
  319. goto done;
  320. /* sysconfig 2 */
  321. radio->registers[SYSCONFIG2] =
  322. (0x3f << 8) | /* SEEKTH */
  323. ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
  324. ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
  325. 15; /* VOLUME (max) */
  326. retval = si470x_set_register(radio, SYSCONFIG2);
  327. if (retval < 0)
  328. goto done;
  329. /* reset last channel */
  330. retval = si470x_set_chan(radio,
  331. radio->registers[CHANNEL] & CHANNEL_CHAN);
  332. done:
  333. return retval;
  334. }
  335. /*
  336. * si470x_stop - switch off radio
  337. */
  338. int si470x_stop(struct si470x_device *radio)
  339. {
  340. int retval;
  341. /* sysconfig 1 */
  342. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  343. retval = si470x_set_register(radio, SYSCONFIG1);
  344. if (retval < 0)
  345. goto done;
  346. /* powercfg */
  347. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  348. /* POWERCFG_ENABLE has to automatically go low */
  349. radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
  350. retval = si470x_set_register(radio, POWERCFG);
  351. done:
  352. return retval;
  353. }
  354. /*
  355. * si470x_rds_on - switch on rds reception
  356. */
  357. int si470x_rds_on(struct si470x_device *radio)
  358. {
  359. int retval;
  360. /* sysconfig 1 */
  361. mutex_lock(&radio->lock);
  362. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
  363. retval = si470x_set_register(radio, SYSCONFIG1);
  364. if (retval < 0)
  365. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
  366. mutex_unlock(&radio->lock);
  367. return retval;
  368. }
  369. /**************************************************************************
  370. * Video4Linux Interface
  371. **************************************************************************/
  372. /*
  373. * si470x_vidioc_queryctrl - enumerate control items
  374. */
  375. static int si470x_vidioc_queryctrl(struct file *file, void *priv,
  376. struct v4l2_queryctrl *qc)
  377. {
  378. struct si470x_device *radio = video_drvdata(file);
  379. int retval = -EINVAL;
  380. /* abort if qc->id is below V4L2_CID_BASE */
  381. if (qc->id < V4L2_CID_BASE)
  382. goto done;
  383. /* search video control */
  384. switch (qc->id) {
  385. case V4L2_CID_AUDIO_VOLUME:
  386. return v4l2_ctrl_query_fill(qc, 0, 15, 1, 15);
  387. case V4L2_CID_AUDIO_MUTE:
  388. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  389. }
  390. /* disable unsupported base controls */
  391. /* to satisfy kradio and such apps */
  392. if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
  393. qc->flags = V4L2_CTRL_FLAG_DISABLED;
  394. retval = 0;
  395. }
  396. done:
  397. if (retval < 0)
  398. dev_warn(&radio->videodev->dev,
  399. "query controls failed with %d\n", retval);
  400. return retval;
  401. }
  402. /*
  403. * si470x_vidioc_g_ctrl - get the value of a control
  404. */
  405. static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
  406. struct v4l2_control *ctrl)
  407. {
  408. struct si470x_device *radio = video_drvdata(file);
  409. int retval = 0;
  410. /* safety checks */
  411. retval = si470x_disconnect_check(radio);
  412. if (retval)
  413. goto done;
  414. switch (ctrl->id) {
  415. case V4L2_CID_AUDIO_VOLUME:
  416. ctrl->value = radio->registers[SYSCONFIG2] &
  417. SYSCONFIG2_VOLUME;
  418. break;
  419. case V4L2_CID_AUDIO_MUTE:
  420. ctrl->value = ((radio->registers[POWERCFG] &
  421. POWERCFG_DMUTE) == 0) ? 1 : 0;
  422. break;
  423. default:
  424. retval = -EINVAL;
  425. }
  426. done:
  427. if (retval < 0)
  428. dev_warn(&radio->videodev->dev,
  429. "get control failed with %d\n", retval);
  430. return retval;
  431. }
  432. /*
  433. * si470x_vidioc_s_ctrl - set the value of a control
  434. */
  435. static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
  436. struct v4l2_control *ctrl)
  437. {
  438. struct si470x_device *radio = video_drvdata(file);
  439. int retval = 0;
  440. /* safety checks */
  441. retval = si470x_disconnect_check(radio);
  442. if (retval)
  443. goto done;
  444. switch (ctrl->id) {
  445. case V4L2_CID_AUDIO_VOLUME:
  446. radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
  447. radio->registers[SYSCONFIG2] |= ctrl->value;
  448. retval = si470x_set_register(radio, SYSCONFIG2);
  449. break;
  450. case V4L2_CID_AUDIO_MUTE:
  451. if (ctrl->value == 1)
  452. radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
  453. else
  454. radio->registers[POWERCFG] |= POWERCFG_DMUTE;
  455. retval = si470x_set_register(radio, POWERCFG);
  456. break;
  457. default:
  458. retval = -EINVAL;
  459. }
  460. done:
  461. if (retval < 0)
  462. dev_warn(&radio->videodev->dev,
  463. "set control failed with %d\n", retval);
  464. return retval;
  465. }
  466. /*
  467. * si470x_vidioc_g_audio - get audio attributes
  468. */
  469. static int si470x_vidioc_g_audio(struct file *file, void *priv,
  470. struct v4l2_audio *audio)
  471. {
  472. /* driver constants */
  473. audio->index = 0;
  474. strcpy(audio->name, "Radio");
  475. audio->capability = V4L2_AUDCAP_STEREO;
  476. audio->mode = 0;
  477. return 0;
  478. }
  479. /*
  480. * si470x_vidioc_g_tuner - get tuner attributes
  481. */
  482. static int si470x_vidioc_g_tuner(struct file *file, void *priv,
  483. struct v4l2_tuner *tuner)
  484. {
  485. struct si470x_device *radio = video_drvdata(file);
  486. int retval = 0;
  487. /* safety checks */
  488. retval = si470x_disconnect_check(radio);
  489. if (retval)
  490. goto done;
  491. if (tuner->index != 0) {
  492. retval = -EINVAL;
  493. goto done;
  494. }
  495. retval = si470x_get_register(radio, STATUSRSSI);
  496. if (retval < 0)
  497. goto done;
  498. /* driver constants */
  499. strcpy(tuner->name, "FM");
  500. tuner->type = V4L2_TUNER_RADIO;
  501. #if defined(CONFIG_USB_SI470X) || defined(CONFIG_USB_SI470X_MODULE)
  502. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  503. V4L2_TUNER_CAP_RDS;
  504. #else
  505. tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  506. #endif
  507. /* range limits */
  508. switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
  509. /* 0: 87.5 - 108 MHz (USA, Europe, default) */
  510. default:
  511. tuner->rangelow = 87.5 * FREQ_MUL;
  512. tuner->rangehigh = 108 * FREQ_MUL;
  513. break;
  514. /* 1: 76 - 108 MHz (Japan wide band) */
  515. case 1:
  516. tuner->rangelow = 76 * FREQ_MUL;
  517. tuner->rangehigh = 108 * FREQ_MUL;
  518. break;
  519. /* 2: 76 - 90 MHz (Japan) */
  520. case 2:
  521. tuner->rangelow = 76 * FREQ_MUL;
  522. tuner->rangehigh = 90 * FREQ_MUL;
  523. break;
  524. };
  525. /* stereo indicator == stereo (instead of mono) */
  526. if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
  527. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  528. else
  529. tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  530. #if defined(CONFIG_USB_SI470X) || defined(CONFIG_USB_SI470X_MODULE)
  531. /* If there is a reliable method of detecting an RDS channel,
  532. then this code should check for that before setting this
  533. RDS subchannel. */
  534. tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
  535. #endif
  536. /* mono/stereo selector */
  537. if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
  538. tuner->audmode = V4L2_TUNER_MODE_STEREO;
  539. else
  540. tuner->audmode = V4L2_TUNER_MODE_MONO;
  541. /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
  542. /* measured in units of db쨉V in 1 db increments (max at ~75 db쨉V) */
  543. tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
  544. /* the ideal factor is 0xffff/75 = 873,8 */
  545. tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
  546. /* automatic frequency control: -1: freq to low, 1 freq to high */
  547. /* AFCRL does only indicate that freq. differs, not if too low/high */
  548. tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
  549. done:
  550. if (retval < 0)
  551. dev_warn(&radio->videodev->dev,
  552. "get tuner failed with %d\n", retval);
  553. return retval;
  554. }
  555. /*
  556. * si470x_vidioc_s_tuner - set tuner attributes
  557. */
  558. static int si470x_vidioc_s_tuner(struct file *file, void *priv,
  559. struct v4l2_tuner *tuner)
  560. {
  561. struct si470x_device *radio = video_drvdata(file);
  562. int retval = -EINVAL;
  563. /* safety checks */
  564. retval = si470x_disconnect_check(radio);
  565. if (retval)
  566. goto done;
  567. if (tuner->index != 0)
  568. goto done;
  569. /* mono/stereo selector */
  570. switch (tuner->audmode) {
  571. case V4L2_TUNER_MODE_MONO:
  572. radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
  573. break;
  574. case V4L2_TUNER_MODE_STEREO:
  575. radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
  576. break;
  577. default:
  578. goto done;
  579. }
  580. retval = si470x_set_register(radio, POWERCFG);
  581. done:
  582. if (retval < 0)
  583. dev_warn(&radio->videodev->dev,
  584. "set tuner failed with %d\n", retval);
  585. return retval;
  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. int retval = 0;
  595. /* safety checks */
  596. retval = si470x_disconnect_check(radio);
  597. if (retval)
  598. goto done;
  599. if (freq->tuner != 0) {
  600. retval = -EINVAL;
  601. goto done;
  602. }
  603. freq->type = V4L2_TUNER_RADIO;
  604. retval = si470x_get_freq(radio, &freq->frequency);
  605. done:
  606. if (retval < 0)
  607. dev_warn(&radio->videodev->dev,
  608. "get frequency failed with %d\n", retval);
  609. return retval;
  610. }
  611. /*
  612. * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
  613. */
  614. static int si470x_vidioc_s_frequency(struct file *file, void *priv,
  615. struct v4l2_frequency *freq)
  616. {
  617. struct si470x_device *radio = video_drvdata(file);
  618. int retval = 0;
  619. /* safety checks */
  620. retval = si470x_disconnect_check(radio);
  621. if (retval)
  622. goto done;
  623. if (freq->tuner != 0) {
  624. retval = -EINVAL;
  625. goto done;
  626. }
  627. retval = si470x_set_freq(radio, freq->frequency);
  628. done:
  629. if (retval < 0)
  630. dev_warn(&radio->videodev->dev,
  631. "set frequency failed with %d\n", retval);
  632. return retval;
  633. }
  634. /*
  635. * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
  636. */
  637. static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
  638. struct v4l2_hw_freq_seek *seek)
  639. {
  640. struct si470x_device *radio = video_drvdata(file);
  641. int retval = 0;
  642. /* safety checks */
  643. retval = si470x_disconnect_check(radio);
  644. if (retval)
  645. goto done;
  646. if (seek->tuner != 0) {
  647. retval = -EINVAL;
  648. goto done;
  649. }
  650. retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
  651. done:
  652. if (retval < 0)
  653. dev_warn(&radio->videodev->dev,
  654. "set hardware frequency seek failed with %d\n", retval);
  655. return retval;
  656. }
  657. /*
  658. * si470x_ioctl_ops - video device ioctl operations
  659. */
  660. static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
  661. .vidioc_querycap = si470x_vidioc_querycap,
  662. .vidioc_queryctrl = si470x_vidioc_queryctrl,
  663. .vidioc_g_ctrl = si470x_vidioc_g_ctrl,
  664. .vidioc_s_ctrl = si470x_vidioc_s_ctrl,
  665. .vidioc_g_audio = si470x_vidioc_g_audio,
  666. .vidioc_g_tuner = si470x_vidioc_g_tuner,
  667. .vidioc_s_tuner = si470x_vidioc_s_tuner,
  668. .vidioc_g_frequency = si470x_vidioc_g_frequency,
  669. .vidioc_s_frequency = si470x_vidioc_s_frequency,
  670. .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
  671. };
  672. /*
  673. * si470x_viddev_template - video device interface
  674. */
  675. struct video_device si470x_viddev_template = {
  676. .fops = &si470x_fops,
  677. .name = DRIVER_NAME,
  678. .release = video_device_release,
  679. .ioctl_ops = &si470x_ioctl_ops,
  680. };