fmdrv_rx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. * FM Driver for Connectivity chip of Texas Instruments.
  3. * This sub-module of FM driver implements FM RX functionality.
  4. *
  5. * Copyright (C) 2011 Texas Instruments
  6. * Author: Raja Mani <raja_mani@ti.com>
  7. * Author: Manjunatha Halli <manjunatha_halli@ti.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 version 2 as
  11. * published by the Free Software Foundation.
  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. #include "fmdrv.h"
  24. #include "fmdrv_common.h"
  25. #include "fmdrv_rx.h"
  26. void fm_rx_reset_rds_cache(struct fmdev *fmdev)
  27. {
  28. fmdev->rx.rds.flag = FM_RDS_DISABLE;
  29. fmdev->rx.rds.last_blk_idx = 0;
  30. fmdev->rx.rds.wr_idx = 0;
  31. fmdev->rx.rds.rd_idx = 0;
  32. if (fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
  33. fmdev->irq_info.mask |= FM_LEV_EVENT;
  34. }
  35. void fm_rx_reset_station_info(struct fmdev *fmdev)
  36. {
  37. fmdev->rx.stat_info.picode = FM_NO_PI_CODE;
  38. fmdev->rx.stat_info.afcache_size = 0;
  39. fmdev->rx.stat_info.af_list_max = 0;
  40. }
  41. int fm_rx_set_freq(struct fmdev *fmdev, u32 freq)
  42. {
  43. unsigned long timeleft;
  44. u16 payload, curr_frq, intr_flag;
  45. u32 curr_frq_in_khz;
  46. u32 resp_len;
  47. int ret;
  48. if (freq < fmdev->rx.region.bot_freq || freq > fmdev->rx.region.top_freq) {
  49. fmerr("Invalid frequency %d\n", freq);
  50. return -EINVAL;
  51. }
  52. /* Set audio enable */
  53. payload = FM_RX_AUDIO_ENABLE_I2S_AND_ANALOG;
  54. ret = fmc_send_cmd(fmdev, AUDIO_ENABLE_SET, REG_WR, &payload,
  55. sizeof(payload), NULL, NULL);
  56. if (ret < 0)
  57. return ret;
  58. /* Set hilo to automatic selection */
  59. payload = FM_RX_IFFREQ_HILO_AUTOMATIC;
  60. ret = fmc_send_cmd(fmdev, HILO_SET, REG_WR, &payload,
  61. sizeof(payload), NULL, NULL);
  62. if (ret < 0)
  63. return ret;
  64. /* Calculate frequency index and set*/
  65. payload = (freq - fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
  66. ret = fmc_send_cmd(fmdev, FREQ_SET, REG_WR, &payload,
  67. sizeof(payload), NULL, NULL);
  68. if (ret < 0)
  69. return ret;
  70. /* Read flags - just to clear any pending interrupts if we had */
  71. ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2, NULL, NULL);
  72. if (ret < 0)
  73. return ret;
  74. /* Enable FR, BL interrupts */
  75. intr_flag = fmdev->irq_info.mask;
  76. fmdev->irq_info.mask = (FM_FR_EVENT | FM_BL_EVENT);
  77. payload = fmdev->irq_info.mask;
  78. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  79. sizeof(payload), NULL, NULL);
  80. if (ret < 0)
  81. return ret;
  82. /* Start tune */
  83. payload = FM_TUNER_PRESET_MODE;
  84. ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
  85. sizeof(payload), NULL, NULL);
  86. if (ret < 0)
  87. goto exit;
  88. /* Wait for tune ended interrupt */
  89. init_completion(&fmdev->maintask_comp);
  90. timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
  91. FM_DRV_TX_TIMEOUT);
  92. if (!timeleft) {
  93. fmerr("Timeout(%d sec),didn't get tune ended int\n",
  94. jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
  95. ret = -ETIMEDOUT;
  96. goto exit;
  97. }
  98. /* Read freq back to confirm */
  99. ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, 2, &curr_frq, &resp_len);
  100. if (ret < 0)
  101. goto exit;
  102. curr_frq = be16_to_cpu((__force __be16)curr_frq);
  103. curr_frq_in_khz = (fmdev->rx.region.bot_freq + ((u32)curr_frq * FM_FREQ_MUL));
  104. if (curr_frq_in_khz != freq) {
  105. pr_info("Frequency is set to (%d) but "
  106. "requested freq is (%d)\n", curr_frq_in_khz, freq);
  107. }
  108. /* Update local cache */
  109. fmdev->rx.freq = curr_frq_in_khz;
  110. exit:
  111. /* Re-enable default FM interrupts */
  112. fmdev->irq_info.mask = intr_flag;
  113. payload = fmdev->irq_info.mask;
  114. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  115. sizeof(payload), NULL, NULL);
  116. if (ret < 0)
  117. return ret;
  118. /* Reset RDS cache and current station pointers */
  119. fm_rx_reset_rds_cache(fmdev);
  120. fm_rx_reset_station_info(fmdev);
  121. return ret;
  122. }
  123. static int fm_rx_set_channel_spacing(struct fmdev *fmdev, u32 spacing)
  124. {
  125. u16 payload;
  126. int ret;
  127. if (spacing > 0 && spacing <= 50000)
  128. spacing = FM_CHANNEL_SPACING_50KHZ;
  129. else if (spacing > 50000 && spacing <= 100000)
  130. spacing = FM_CHANNEL_SPACING_100KHZ;
  131. else
  132. spacing = FM_CHANNEL_SPACING_200KHZ;
  133. /* set channel spacing */
  134. payload = spacing;
  135. ret = fmc_send_cmd(fmdev, CHANL_BW_SET, REG_WR, &payload,
  136. sizeof(payload), NULL, NULL);
  137. if (ret < 0)
  138. return ret;
  139. fmdev->rx.region.chanl_space = spacing * FM_FREQ_MUL;
  140. return ret;
  141. }
  142. int fm_rx_seek(struct fmdev *fmdev, u32 seek_upward,
  143. u32 wrap_around, u32 spacing)
  144. {
  145. u32 resp_len;
  146. u16 curr_frq, next_frq, last_frq;
  147. u16 payload, int_reason, intr_flag;
  148. u16 offset, space_idx;
  149. unsigned long timeleft;
  150. int ret;
  151. /* Set channel spacing */
  152. ret = fm_rx_set_channel_spacing(fmdev, spacing);
  153. if (ret < 0) {
  154. fmerr("Failed to set channel spacing\n");
  155. return ret;
  156. }
  157. /* Read the current frequency from chip */
  158. ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL,
  159. sizeof(curr_frq), &curr_frq, &resp_len);
  160. if (ret < 0)
  161. return ret;
  162. curr_frq = be16_to_cpu((__force __be16)curr_frq);
  163. last_frq = (fmdev->rx.region.top_freq - fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
  164. /* Check the offset in order to be aligned to the channel spacing*/
  165. space_idx = fmdev->rx.region.chanl_space / FM_FREQ_MUL;
  166. offset = curr_frq % space_idx;
  167. next_frq = seek_upward ? curr_frq + space_idx /* Seek Up */ :
  168. curr_frq - space_idx /* Seek Down */ ;
  169. /*
  170. * Add or subtract offset in order to stay aligned to the channel
  171. * spacing.
  172. */
  173. if ((short)next_frq < 0)
  174. next_frq = last_frq - offset;
  175. else if (next_frq > last_frq)
  176. next_frq = 0 + offset;
  177. again:
  178. /* Set calculated next frequency to perform seek */
  179. payload = next_frq;
  180. ret = fmc_send_cmd(fmdev, FREQ_SET, REG_WR, &payload,
  181. sizeof(payload), NULL, NULL);
  182. if (ret < 0)
  183. return ret;
  184. /* Set search direction (0:Seek Down, 1:Seek Up) */
  185. payload = (seek_upward ? FM_SEARCH_DIRECTION_UP : FM_SEARCH_DIRECTION_DOWN);
  186. ret = fmc_send_cmd(fmdev, SEARCH_DIR_SET, REG_WR, &payload,
  187. sizeof(payload), NULL, NULL);
  188. if (ret < 0)
  189. return ret;
  190. /* Read flags - just to clear any pending interrupts if we had */
  191. ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2, NULL, NULL);
  192. if (ret < 0)
  193. return ret;
  194. /* Enable FR, BL interrupts */
  195. intr_flag = fmdev->irq_info.mask;
  196. fmdev->irq_info.mask = (FM_FR_EVENT | FM_BL_EVENT);
  197. payload = fmdev->irq_info.mask;
  198. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  199. sizeof(payload), NULL, NULL);
  200. if (ret < 0)
  201. return ret;
  202. /* Start seek */
  203. payload = FM_TUNER_AUTONOMOUS_SEARCH_MODE;
  204. ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
  205. sizeof(payload), NULL, NULL);
  206. if (ret < 0)
  207. return ret;
  208. /* Wait for tune ended/band limit reached interrupt */
  209. init_completion(&fmdev->maintask_comp);
  210. timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
  211. FM_DRV_RX_SEEK_TIMEOUT);
  212. if (!timeleft) {
  213. fmerr("Timeout(%d sec),didn't get tune ended int\n",
  214. jiffies_to_msecs(FM_DRV_RX_SEEK_TIMEOUT) / 1000);
  215. return -ENODATA;
  216. }
  217. int_reason = fmdev->irq_info.flag & (FM_TUNE_COMPLETE | FM_BAND_LIMIT);
  218. /* Re-enable default FM interrupts */
  219. fmdev->irq_info.mask = intr_flag;
  220. payload = fmdev->irq_info.mask;
  221. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  222. sizeof(payload), NULL, NULL);
  223. if (ret < 0)
  224. return ret;
  225. if (int_reason & FM_BL_EVENT) {
  226. if (wrap_around == 0) {
  227. fmdev->rx.freq = seek_upward ?
  228. fmdev->rx.region.top_freq :
  229. fmdev->rx.region.bot_freq;
  230. } else {
  231. fmdev->rx.freq = seek_upward ?
  232. fmdev->rx.region.bot_freq :
  233. fmdev->rx.region.top_freq;
  234. /* Calculate frequency index to write */
  235. next_frq = (fmdev->rx.freq -
  236. fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
  237. goto again;
  238. }
  239. } else {
  240. /* Read freq to know where operation tune operation stopped */
  241. ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, 2,
  242. &curr_frq, &resp_len);
  243. if (ret < 0)
  244. return ret;
  245. curr_frq = be16_to_cpu((__force __be16)curr_frq);
  246. fmdev->rx.freq = (fmdev->rx.region.bot_freq +
  247. ((u32)curr_frq * FM_FREQ_MUL));
  248. }
  249. /* Reset RDS cache and current station pointers */
  250. fm_rx_reset_rds_cache(fmdev);
  251. fm_rx_reset_station_info(fmdev);
  252. return ret;
  253. }
  254. int fm_rx_set_volume(struct fmdev *fmdev, u16 vol_to_set)
  255. {
  256. u16 payload;
  257. int ret;
  258. if (fmdev->curr_fmmode != FM_MODE_RX)
  259. return -EPERM;
  260. if (vol_to_set > FM_RX_VOLUME_MAX) {
  261. fmerr("Volume is not within(%d-%d) range\n",
  262. FM_RX_VOLUME_MIN, FM_RX_VOLUME_MAX);
  263. return -EINVAL;
  264. }
  265. vol_to_set *= FM_RX_VOLUME_GAIN_STEP;
  266. payload = vol_to_set;
  267. ret = fmc_send_cmd(fmdev, VOLUME_SET, REG_WR, &payload,
  268. sizeof(payload), NULL, NULL);
  269. if (ret < 0)
  270. return ret;
  271. fmdev->rx.volume = vol_to_set;
  272. return ret;
  273. }
  274. /* Get volume */
  275. int fm_rx_get_volume(struct fmdev *fmdev, u16 *curr_vol)
  276. {
  277. if (fmdev->curr_fmmode != FM_MODE_RX)
  278. return -EPERM;
  279. if (curr_vol == NULL) {
  280. fmerr("Invalid memory\n");
  281. return -ENOMEM;
  282. }
  283. *curr_vol = fmdev->rx.volume / FM_RX_VOLUME_GAIN_STEP;
  284. return 0;
  285. }
  286. /* To get current band's bottom and top frequency */
  287. int fm_rx_get_band_freq_range(struct fmdev *fmdev, u32 *bot_freq, u32 *top_freq)
  288. {
  289. if (bot_freq != NULL)
  290. *bot_freq = fmdev->rx.region.bot_freq;
  291. if (top_freq != NULL)
  292. *top_freq = fmdev->rx.region.top_freq;
  293. return 0;
  294. }
  295. /* Returns current band index (0-Europe/US; 1-Japan) */
  296. void fm_rx_get_region(struct fmdev *fmdev, u8 *region)
  297. {
  298. *region = fmdev->rx.region.fm_band;
  299. }
  300. /* Sets band (0-Europe/US; 1-Japan) */
  301. int fm_rx_set_region(struct fmdev *fmdev, u8 region_to_set)
  302. {
  303. u16 payload;
  304. u32 new_frq = 0;
  305. int ret;
  306. if (region_to_set != FM_BAND_EUROPE_US &&
  307. region_to_set != FM_BAND_JAPAN) {
  308. fmerr("Invalid band\n");
  309. return -EINVAL;
  310. }
  311. if (fmdev->rx.region.fm_band == region_to_set) {
  312. fmerr("Requested band is already configured\n");
  313. return 0;
  314. }
  315. /* Send cmd to set the band */
  316. payload = (u16)region_to_set;
  317. ret = fmc_send_cmd(fmdev, BAND_SET, REG_WR, &payload,
  318. sizeof(payload), NULL, NULL);
  319. if (ret < 0)
  320. return ret;
  321. fmc_update_region_info(fmdev, region_to_set);
  322. /* Check whether current RX frequency is within band boundary */
  323. if (fmdev->rx.freq < fmdev->rx.region.bot_freq)
  324. new_frq = fmdev->rx.region.bot_freq;
  325. else if (fmdev->rx.freq > fmdev->rx.region.top_freq)
  326. new_frq = fmdev->rx.region.top_freq;
  327. if (new_frq) {
  328. fmdbg("Current freq is not within band limit boundary,"
  329. "switching to %d KHz\n", new_frq);
  330. /* Current RX frequency is not in range. So, update it */
  331. ret = fm_rx_set_freq(fmdev, new_frq);
  332. }
  333. return ret;
  334. }
  335. /* Reads current mute mode (Mute Off/On/Attenuate)*/
  336. int fm_rx_get_mute_mode(struct fmdev *fmdev, u8 *curr_mute_mode)
  337. {
  338. if (fmdev->curr_fmmode != FM_MODE_RX)
  339. return -EPERM;
  340. if (curr_mute_mode == NULL) {
  341. fmerr("Invalid memory\n");
  342. return -ENOMEM;
  343. }
  344. *curr_mute_mode = fmdev->rx.mute_mode;
  345. return 0;
  346. }
  347. static int fm_config_rx_mute_reg(struct fmdev *fmdev)
  348. {
  349. u16 payload, muteval;
  350. int ret;
  351. muteval = 0;
  352. switch (fmdev->rx.mute_mode) {
  353. case FM_MUTE_ON:
  354. muteval = FM_RX_AC_MUTE_MODE;
  355. break;
  356. case FM_MUTE_OFF:
  357. muteval = FM_RX_UNMUTE_MODE;
  358. break;
  359. case FM_MUTE_ATTENUATE:
  360. muteval = FM_RX_SOFT_MUTE_FORCE_MODE;
  361. break;
  362. }
  363. if (fmdev->rx.rf_depend_mute == FM_RX_RF_DEPENDENT_MUTE_ON)
  364. muteval |= FM_RX_RF_DEP_MODE;
  365. else
  366. muteval &= ~FM_RX_RF_DEP_MODE;
  367. payload = muteval;
  368. ret = fmc_send_cmd(fmdev, MUTE_STATUS_SET, REG_WR, &payload,
  369. sizeof(payload), NULL, NULL);
  370. if (ret < 0)
  371. return ret;
  372. return 0;
  373. }
  374. /* Configures mute mode (Mute Off/On/Attenuate) */
  375. int fm_rx_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
  376. {
  377. u8 org_state;
  378. int ret;
  379. if (fmdev->rx.mute_mode == mute_mode_toset)
  380. return 0;
  381. org_state = fmdev->rx.mute_mode;
  382. fmdev->rx.mute_mode = mute_mode_toset;
  383. ret = fm_config_rx_mute_reg(fmdev);
  384. if (ret < 0) {
  385. fmdev->rx.mute_mode = org_state;
  386. return ret;
  387. }
  388. return 0;
  389. }
  390. /* Gets RF dependent soft mute mode enable/disable status */
  391. int fm_rx_get_rfdepend_softmute(struct fmdev *fmdev, u8 *curr_mute_mode)
  392. {
  393. if (fmdev->curr_fmmode != FM_MODE_RX)
  394. return -EPERM;
  395. if (curr_mute_mode == NULL) {
  396. fmerr("Invalid memory\n");
  397. return -ENOMEM;
  398. }
  399. *curr_mute_mode = fmdev->rx.rf_depend_mute;
  400. return 0;
  401. }
  402. /* Sets RF dependent soft mute mode */
  403. int fm_rx_set_rfdepend_softmute(struct fmdev *fmdev, u8 rfdepend_mute)
  404. {
  405. u8 org_state;
  406. int ret;
  407. if (fmdev->curr_fmmode != FM_MODE_RX)
  408. return -EPERM;
  409. if (rfdepend_mute != FM_RX_RF_DEPENDENT_MUTE_ON &&
  410. rfdepend_mute != FM_RX_RF_DEPENDENT_MUTE_OFF) {
  411. fmerr("Invalid RF dependent soft mute\n");
  412. return -EINVAL;
  413. }
  414. if (fmdev->rx.rf_depend_mute == rfdepend_mute)
  415. return 0;
  416. org_state = fmdev->rx.rf_depend_mute;
  417. fmdev->rx.rf_depend_mute = rfdepend_mute;
  418. ret = fm_config_rx_mute_reg(fmdev);
  419. if (ret < 0) {
  420. fmdev->rx.rf_depend_mute = org_state;
  421. return ret;
  422. }
  423. return 0;
  424. }
  425. /* Returns the signal strength level of current channel */
  426. int fm_rx_get_rssi_level(struct fmdev *fmdev, u16 *rssilvl)
  427. {
  428. __be16 curr_rssi_lel;
  429. u32 resp_len;
  430. int ret;
  431. if (rssilvl == NULL) {
  432. fmerr("Invalid memory\n");
  433. return -ENOMEM;
  434. }
  435. /* Read current RSSI level */
  436. ret = fmc_send_cmd(fmdev, RSSI_LVL_GET, REG_RD, NULL, 2,
  437. &curr_rssi_lel, &resp_len);
  438. if (ret < 0)
  439. return ret;
  440. *rssilvl = be16_to_cpu(curr_rssi_lel);
  441. return 0;
  442. }
  443. /*
  444. * Sets the signal strength level that once reached
  445. * will stop the auto search process
  446. */
  447. int fm_rx_set_rssi_threshold(struct fmdev *fmdev, short rssi_lvl_toset)
  448. {
  449. u16 payload;
  450. int ret;
  451. if (rssi_lvl_toset < FM_RX_RSSI_THRESHOLD_MIN ||
  452. rssi_lvl_toset > FM_RX_RSSI_THRESHOLD_MAX) {
  453. fmerr("Invalid RSSI threshold level\n");
  454. return -EINVAL;
  455. }
  456. payload = (u16)rssi_lvl_toset;
  457. ret = fmc_send_cmd(fmdev, SEARCH_LVL_SET, REG_WR, &payload,
  458. sizeof(payload), NULL, NULL);
  459. if (ret < 0)
  460. return ret;
  461. fmdev->rx.rssi_threshold = rssi_lvl_toset;
  462. return 0;
  463. }
  464. /* Returns current RX RSSI threshold value */
  465. int fm_rx_get_rssi_threshold(struct fmdev *fmdev, short *curr_rssi_lvl)
  466. {
  467. if (fmdev->curr_fmmode != FM_MODE_RX)
  468. return -EPERM;
  469. if (curr_rssi_lvl == NULL) {
  470. fmerr("Invalid memory\n");
  471. return -ENOMEM;
  472. }
  473. *curr_rssi_lvl = fmdev->rx.rssi_threshold;
  474. return 0;
  475. }
  476. /* Sets RX stereo/mono modes */
  477. int fm_rx_set_stereo_mono(struct fmdev *fmdev, u16 mode)
  478. {
  479. u16 payload;
  480. int ret;
  481. if (mode != FM_STEREO_MODE && mode != FM_MONO_MODE) {
  482. fmerr("Invalid mode\n");
  483. return -EINVAL;
  484. }
  485. /* Set stereo/mono mode */
  486. payload = (u16)mode;
  487. ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_WR, &payload,
  488. sizeof(payload), NULL, NULL);
  489. if (ret < 0)
  490. return ret;
  491. /* Set stereo blending mode */
  492. payload = FM_STEREO_SOFT_BLEND;
  493. ret = fmc_send_cmd(fmdev, MOST_BLEND_SET, REG_WR, &payload,
  494. sizeof(payload), NULL, NULL);
  495. if (ret < 0)
  496. return ret;
  497. return 0;
  498. }
  499. /* Gets current RX stereo/mono mode */
  500. int fm_rx_get_stereo_mono(struct fmdev *fmdev, u16 *mode)
  501. {
  502. __be16 curr_mode;
  503. u32 resp_len;
  504. int ret;
  505. if (mode == NULL) {
  506. fmerr("Invalid memory\n");
  507. return -ENOMEM;
  508. }
  509. ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_RD, NULL, 2,
  510. &curr_mode, &resp_len);
  511. if (ret < 0)
  512. return ret;
  513. *mode = be16_to_cpu(curr_mode);
  514. return 0;
  515. }
  516. /* Choose RX de-emphasis filter mode (50us/75us) */
  517. int fm_rx_set_deemphasis_mode(struct fmdev *fmdev, u16 mode)
  518. {
  519. u16 payload;
  520. int ret;
  521. if (fmdev->curr_fmmode != FM_MODE_RX)
  522. return -EPERM;
  523. if (mode != FM_RX_EMPHASIS_FILTER_50_USEC &&
  524. mode != FM_RX_EMPHASIS_FILTER_75_USEC) {
  525. fmerr("Invalid rx de-emphasis mode (%d)\n", mode);
  526. return -EINVAL;
  527. }
  528. payload = mode;
  529. ret = fmc_send_cmd(fmdev, DEMPH_MODE_SET, REG_WR, &payload,
  530. sizeof(payload), NULL, NULL);
  531. if (ret < 0)
  532. return ret;
  533. fmdev->rx.deemphasis_mode = mode;
  534. return 0;
  535. }
  536. /* Gets current RX de-emphasis filter mode */
  537. int fm_rx_get_deemph_mode(struct fmdev *fmdev, u16 *curr_deemphasis_mode)
  538. {
  539. if (fmdev->curr_fmmode != FM_MODE_RX)
  540. return -EPERM;
  541. if (curr_deemphasis_mode == NULL) {
  542. fmerr("Invalid memory\n");
  543. return -ENOMEM;
  544. }
  545. *curr_deemphasis_mode = fmdev->rx.deemphasis_mode;
  546. return 0;
  547. }
  548. /* Enable/Disable RX RDS */
  549. int fm_rx_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
  550. {
  551. u16 payload;
  552. int ret;
  553. if (rds_en_dis != FM_RDS_ENABLE && rds_en_dis != FM_RDS_DISABLE) {
  554. fmerr("Invalid rds option\n");
  555. return -EINVAL;
  556. }
  557. if (rds_en_dis == FM_RDS_ENABLE
  558. && fmdev->rx.rds.flag == FM_RDS_DISABLE) {
  559. /* Turn on RX RDS and RDS circuit */
  560. payload = FM_RX_PWR_SET_FM_AND_RDS_BLK_ON;
  561. ret = fmc_send_cmd(fmdev, POWER_SET, REG_WR, &payload,
  562. sizeof(payload), NULL, NULL);
  563. if (ret < 0)
  564. return ret;
  565. /* Clear and reset RDS FIFO */
  566. payload = FM_RX_RDS_FLUSH_FIFO;
  567. ret = fmc_send_cmd(fmdev, RDS_CNTRL_SET, REG_WR, &payload,
  568. sizeof(payload), NULL, NULL);
  569. if (ret < 0)
  570. return ret;
  571. /* Read flags - just to clear any pending interrupts. */
  572. ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2,
  573. NULL, NULL);
  574. if (ret < 0)
  575. return ret;
  576. /* Set RDS FIFO threshold value */
  577. payload = FM_RX_RDS_FIFO_THRESHOLD;
  578. ret = fmc_send_cmd(fmdev, RDS_MEM_SET, REG_WR, &payload,
  579. sizeof(payload), NULL, NULL);
  580. if (ret < 0)
  581. return ret;
  582. /* Enable RDS interrupt */
  583. fmdev->irq_info.mask |= FM_RDS_EVENT;
  584. payload = fmdev->irq_info.mask;
  585. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  586. sizeof(payload), NULL, NULL);
  587. if (ret < 0) {
  588. fmdev->irq_info.mask &= ~FM_RDS_EVENT;
  589. return ret;
  590. }
  591. /* Update our local flag */
  592. fmdev->rx.rds.flag = FM_RDS_ENABLE;
  593. } else if (rds_en_dis == FM_RDS_DISABLE
  594. && fmdev->rx.rds.flag == FM_RDS_ENABLE) {
  595. /* Turn off RX RDS */
  596. payload = FM_RX_PWR_SET_FM_ON_RDS_OFF;
  597. ret = fmc_send_cmd(fmdev, POWER_SET, REG_WR, &payload,
  598. sizeof(payload), NULL, NULL);
  599. if (ret < 0)
  600. return ret;
  601. /* Reset RDS pointers */
  602. fmdev->rx.rds.last_blk_idx = 0;
  603. fmdev->rx.rds.wr_idx = 0;
  604. fmdev->rx.rds.rd_idx = 0;
  605. fm_rx_reset_station_info(fmdev);
  606. /* Update RDS local cache */
  607. fmdev->irq_info.mask &= ~(FM_RDS_EVENT);
  608. fmdev->rx.rds.flag = FM_RDS_DISABLE;
  609. }
  610. return 0;
  611. }
  612. /* Returns current RX RDS enable/disable status */
  613. int fm_rx_get_rds_mode(struct fmdev *fmdev, u8 *curr_rds_en_dis)
  614. {
  615. if (fmdev->curr_fmmode != FM_MODE_RX)
  616. return -EPERM;
  617. if (curr_rds_en_dis == NULL) {
  618. fmerr("Invalid memory\n");
  619. return -ENOMEM;
  620. }
  621. *curr_rds_en_dis = fmdev->rx.rds.flag;
  622. return 0;
  623. }
  624. /* Sets RDS operation mode (RDS/RDBS) */
  625. int fm_rx_set_rds_system(struct fmdev *fmdev, u8 rds_mode)
  626. {
  627. u16 payload;
  628. int ret;
  629. if (fmdev->curr_fmmode != FM_MODE_RX)
  630. return -EPERM;
  631. if (rds_mode != FM_RDS_SYSTEM_RDS && rds_mode != FM_RDS_SYSTEM_RBDS) {
  632. fmerr("Invalid rds mode\n");
  633. return -EINVAL;
  634. }
  635. /* Set RDS operation mode */
  636. payload = (u16)rds_mode;
  637. ret = fmc_send_cmd(fmdev, RDS_SYSTEM_SET, REG_WR, &payload,
  638. sizeof(payload), NULL, NULL);
  639. if (ret < 0)
  640. return ret;
  641. fmdev->rx.rds_mode = rds_mode;
  642. return 0;
  643. }
  644. /* Configures Alternate Frequency switch mode */
  645. int fm_rx_set_af_switch(struct fmdev *fmdev, u8 af_mode)
  646. {
  647. u16 payload;
  648. int ret;
  649. if (fmdev->curr_fmmode != FM_MODE_RX)
  650. return -EPERM;
  651. if (af_mode != FM_RX_RDS_AF_SWITCH_MODE_ON &&
  652. af_mode != FM_RX_RDS_AF_SWITCH_MODE_OFF) {
  653. fmerr("Invalid af mode\n");
  654. return -EINVAL;
  655. }
  656. /* Enable/disable low RSSI interrupt based on af_mode */
  657. if (af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
  658. fmdev->irq_info.mask |= FM_LEV_EVENT;
  659. else
  660. fmdev->irq_info.mask &= ~FM_LEV_EVENT;
  661. payload = fmdev->irq_info.mask;
  662. ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
  663. sizeof(payload), NULL, NULL);
  664. if (ret < 0)
  665. return ret;
  666. fmdev->rx.af_mode = af_mode;
  667. return 0;
  668. }
  669. /* Returns Alternate Frequency switch status */
  670. int fm_rx_get_af_switch(struct fmdev *fmdev, u8 *af_mode)
  671. {
  672. if (fmdev->curr_fmmode != FM_MODE_RX)
  673. return -EPERM;
  674. if (af_mode == NULL) {
  675. fmerr("Invalid memory\n");
  676. return -ENOMEM;
  677. }
  678. *af_mode = fmdev->rx.af_mode;
  679. return 0;
  680. }