Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / radio / wl128x / fmdrv_rx.c
blobcfaeb2417fbbd182da662f5299dffeed9d66e77a
1 /*
2 * FM Driver for Connectivity chip of Texas Instruments.
3 * This sub-module of FM driver implements FM RX functionality.
5 * Copyright (C) 2011 Texas Instruments
6 * Author: Raja Mani <raja_mani@ti.com>
7 * Author: Manjunatha Halli <manjunatha_halli@ti.com>
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.
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.
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
24 #include "fmdrv.h"
25 #include "fmdrv_common.h"
26 #include "fmdrv_rx.h"
28 void fm_rx_reset_rds_cache(struct fmdev *fmdev)
30 fmdev->rx.rds.flag = FM_RDS_DISABLE;
31 fmdev->rx.rds.last_blk_idx = 0;
32 fmdev->rx.rds.wr_idx = 0;
33 fmdev->rx.rds.rd_idx = 0;
35 if (fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
36 fmdev->irq_info.mask |= FM_LEV_EVENT;
39 void fm_rx_reset_station_info(struct fmdev *fmdev)
41 fmdev->rx.stat_info.picode = FM_NO_PI_CODE;
42 fmdev->rx.stat_info.afcache_size = 0;
43 fmdev->rx.stat_info.af_list_max = 0;
46 int fm_rx_set_freq(struct fmdev *fmdev, u32 freq)
48 unsigned long timeleft;
49 u16 payload, curr_frq, intr_flag;
50 u32 curr_frq_in_khz;
51 u32 resp_len;
52 int ret;
54 if (freq < fmdev->rx.region.bot_freq || freq > fmdev->rx.region.top_freq) {
55 fmerr("Invalid frequency %d\n", freq);
56 return -EINVAL;
59 /* Set audio enable */
60 payload = FM_RX_AUDIO_ENABLE_I2S_AND_ANALOG;
62 ret = fmc_send_cmd(fmdev, AUDIO_ENABLE_SET, REG_WR, &payload,
63 sizeof(payload), NULL, NULL);
64 if (ret < 0)
65 return ret;
67 /* Set hilo to automatic selection */
68 payload = FM_RX_IFFREQ_HILO_AUTOMATIC;
69 ret = fmc_send_cmd(fmdev, HILO_SET, REG_WR, &payload,
70 sizeof(payload), NULL, NULL);
71 if (ret < 0)
72 return ret;
74 /* Calculate frequency index and set*/
75 payload = (freq - fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
77 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_WR, &payload,
78 sizeof(payload), NULL, NULL);
79 if (ret < 0)
80 return ret;
82 /* Read flags - just to clear any pending interrupts if we had */
83 ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2, NULL, NULL);
84 if (ret < 0)
85 return ret;
87 /* Enable FR, BL interrupts */
88 intr_flag = fmdev->irq_info.mask;
89 fmdev->irq_info.mask = (FM_FR_EVENT | FM_BL_EVENT);
90 payload = fmdev->irq_info.mask;
91 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
92 sizeof(payload), NULL, NULL);
93 if (ret < 0)
94 return ret;
96 /* Start tune */
97 payload = FM_TUNER_PRESET_MODE;
98 ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
99 sizeof(payload), NULL, NULL);
100 if (ret < 0)
101 goto exit;
103 /* Wait for tune ended interrupt */
104 init_completion(&fmdev->maintask_comp);
105 timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
106 FM_DRV_TX_TIMEOUT);
107 if (!timeleft) {
108 fmerr("Timeout(%d sec),didn't get tune ended int\n",
109 jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
110 ret = -ETIMEDOUT;
111 goto exit;
114 /* Read freq back to confirm */
115 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, 2, &curr_frq, &resp_len);
116 if (ret < 0)
117 goto exit;
119 curr_frq = be16_to_cpu((__force __be16)curr_frq);
120 curr_frq_in_khz = (fmdev->rx.region.bot_freq + ((u32)curr_frq * FM_FREQ_MUL));
122 if (curr_frq_in_khz != freq) {
123 pr_info("Frequency is set to (%d) but "
124 "requested freq is (%d)\n", curr_frq_in_khz, freq);
127 /* Update local cache */
128 fmdev->rx.freq = curr_frq_in_khz;
129 exit:
130 /* Re-enable default FM interrupts */
131 fmdev->irq_info.mask = intr_flag;
132 payload = fmdev->irq_info.mask;
133 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
134 sizeof(payload), NULL, NULL);
135 if (ret < 0)
136 return ret;
138 /* Reset RDS cache and current station pointers */
139 fm_rx_reset_rds_cache(fmdev);
140 fm_rx_reset_station_info(fmdev);
142 return ret;
145 static int fm_rx_set_channel_spacing(struct fmdev *fmdev, u32 spacing)
147 u16 payload;
148 int ret;
150 if (spacing > 0 && spacing <= 50000)
151 spacing = FM_CHANNEL_SPACING_50KHZ;
152 else if (spacing > 50000 && spacing <= 100000)
153 spacing = FM_CHANNEL_SPACING_100KHZ;
154 else
155 spacing = FM_CHANNEL_SPACING_200KHZ;
157 /* set channel spacing */
158 payload = spacing;
159 ret = fmc_send_cmd(fmdev, CHANL_BW_SET, REG_WR, &payload,
160 sizeof(payload), NULL, NULL);
161 if (ret < 0)
162 return ret;
164 fmdev->rx.region.chanl_space = spacing * FM_FREQ_MUL;
166 return ret;
169 int fm_rx_seek(struct fmdev *fmdev, u32 seek_upward,
170 u32 wrap_around, u32 spacing)
172 u32 resp_len;
173 u16 curr_frq, next_frq, last_frq;
174 u16 payload, int_reason, intr_flag;
175 u16 offset, space_idx;
176 unsigned long timeleft;
177 int ret;
179 /* Set channel spacing */
180 ret = fm_rx_set_channel_spacing(fmdev, spacing);
181 if (ret < 0) {
182 fmerr("Failed to set channel spacing\n");
183 return ret;
186 /* Read the current frequency from chip */
187 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL,
188 sizeof(curr_frq), &curr_frq, &resp_len);
189 if (ret < 0)
190 return ret;
192 curr_frq = be16_to_cpu((__force __be16)curr_frq);
193 last_frq = (fmdev->rx.region.top_freq - fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
195 /* Check the offset in order to be aligned to the channel spacing*/
196 space_idx = fmdev->rx.region.chanl_space / FM_FREQ_MUL;
197 offset = curr_frq % space_idx;
199 next_frq = seek_upward ? curr_frq + space_idx /* Seek Up */ :
200 curr_frq - space_idx /* Seek Down */ ;
203 * Add or subtract offset in order to stay aligned to the channel
204 * spacing.
206 if ((short)next_frq < 0)
207 next_frq = last_frq - offset;
208 else if (next_frq > last_frq)
209 next_frq = 0 + offset;
211 again:
212 /* Set calculated next frequency to perform seek */
213 payload = next_frq;
214 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_WR, &payload,
215 sizeof(payload), NULL, NULL);
216 if (ret < 0)
217 return ret;
219 /* Set search direction (0:Seek Down, 1:Seek Up) */
220 payload = (seek_upward ? FM_SEARCH_DIRECTION_UP : FM_SEARCH_DIRECTION_DOWN);
221 ret = fmc_send_cmd(fmdev, SEARCH_DIR_SET, REG_WR, &payload,
222 sizeof(payload), NULL, NULL);
223 if (ret < 0)
224 return ret;
226 /* Read flags - just to clear any pending interrupts if we had */
227 ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2, NULL, NULL);
228 if (ret < 0)
229 return ret;
231 /* Enable FR, BL interrupts */
232 intr_flag = fmdev->irq_info.mask;
233 fmdev->irq_info.mask = (FM_FR_EVENT | FM_BL_EVENT);
234 payload = fmdev->irq_info.mask;
235 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
236 sizeof(payload), NULL, NULL);
237 if (ret < 0)
238 return ret;
240 /* Start seek */
241 payload = FM_TUNER_AUTONOMOUS_SEARCH_MODE;
242 ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
243 sizeof(payload), NULL, NULL);
244 if (ret < 0)
245 return ret;
247 /* Wait for tune ended/band limit reached interrupt */
248 init_completion(&fmdev->maintask_comp);
249 timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
250 FM_DRV_RX_SEEK_TIMEOUT);
251 if (!timeleft) {
252 fmerr("Timeout(%d sec),didn't get tune ended int\n",
253 jiffies_to_msecs(FM_DRV_RX_SEEK_TIMEOUT) / 1000);
254 return -ENODATA;
257 int_reason = fmdev->irq_info.flag & (FM_TUNE_COMPLETE | FM_BAND_LIMIT);
259 /* Re-enable default FM interrupts */
260 fmdev->irq_info.mask = intr_flag;
261 payload = fmdev->irq_info.mask;
262 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
263 sizeof(payload), NULL, NULL);
264 if (ret < 0)
265 return ret;
267 if (int_reason & FM_BL_EVENT) {
268 if (wrap_around == 0) {
269 fmdev->rx.freq = seek_upward ?
270 fmdev->rx.region.top_freq :
271 fmdev->rx.region.bot_freq;
272 } else {
273 fmdev->rx.freq = seek_upward ?
274 fmdev->rx.region.bot_freq :
275 fmdev->rx.region.top_freq;
276 /* Calculate frequency index to write */
277 next_frq = (fmdev->rx.freq -
278 fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
279 goto again;
281 } else {
282 /* Read freq to know where operation tune operation stopped */
283 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, 2,
284 &curr_frq, &resp_len);
285 if (ret < 0)
286 return ret;
288 curr_frq = be16_to_cpu((__force __be16)curr_frq);
289 fmdev->rx.freq = (fmdev->rx.region.bot_freq +
290 ((u32)curr_frq * FM_FREQ_MUL));
293 /* Reset RDS cache and current station pointers */
294 fm_rx_reset_rds_cache(fmdev);
295 fm_rx_reset_station_info(fmdev);
297 return ret;
300 int fm_rx_set_volume(struct fmdev *fmdev, u16 vol_to_set)
302 u16 payload;
303 int ret;
305 if (fmdev->curr_fmmode != FM_MODE_RX)
306 return -EPERM;
308 if (vol_to_set > FM_RX_VOLUME_MAX) {
309 fmerr("Volume is not within(%d-%d) range\n",
310 FM_RX_VOLUME_MIN, FM_RX_VOLUME_MAX);
311 return -EINVAL;
313 vol_to_set *= FM_RX_VOLUME_GAIN_STEP;
315 payload = vol_to_set;
316 ret = fmc_send_cmd(fmdev, VOLUME_SET, REG_WR, &payload,
317 sizeof(payload), NULL, NULL);
318 if (ret < 0)
319 return ret;
321 fmdev->rx.volume = vol_to_set;
322 return ret;
325 /* Get volume */
326 int fm_rx_get_volume(struct fmdev *fmdev, u16 *curr_vol)
328 if (fmdev->curr_fmmode != FM_MODE_RX)
329 return -EPERM;
331 if (curr_vol == NULL) {
332 fmerr("Invalid memory\n");
333 return -ENOMEM;
336 *curr_vol = fmdev->rx.volume / FM_RX_VOLUME_GAIN_STEP;
338 return 0;
341 /* To get current band's bottom and top frequency */
342 int fm_rx_get_band_freq_range(struct fmdev *fmdev, u32 *bot_freq, u32 *top_freq)
344 if (bot_freq != NULL)
345 *bot_freq = fmdev->rx.region.bot_freq;
347 if (top_freq != NULL)
348 *top_freq = fmdev->rx.region.top_freq;
350 return 0;
353 /* Returns current band index (0-Europe/US; 1-Japan) */
354 void fm_rx_get_region(struct fmdev *fmdev, u8 *region)
356 *region = fmdev->rx.region.fm_band;
359 /* Sets band (0-Europe/US; 1-Japan) */
360 int fm_rx_set_region(struct fmdev *fmdev, u8 region_to_set)
362 u16 payload;
363 u32 new_frq = 0;
364 int ret;
366 if (region_to_set != FM_BAND_EUROPE_US &&
367 region_to_set != FM_BAND_JAPAN) {
368 fmerr("Invalid band\n");
369 return -EINVAL;
372 if (fmdev->rx.region.fm_band == region_to_set) {
373 fmerr("Requested band is already configured\n");
374 return 0;
377 /* Send cmd to set the band */
378 payload = (u16)region_to_set;
379 ret = fmc_send_cmd(fmdev, BAND_SET, REG_WR, &payload,
380 sizeof(payload), NULL, NULL);
381 if (ret < 0)
382 return ret;
384 fmc_update_region_info(fmdev, region_to_set);
386 /* Check whether current RX frequency is within band boundary */
387 if (fmdev->rx.freq < fmdev->rx.region.bot_freq)
388 new_frq = fmdev->rx.region.bot_freq;
389 else if (fmdev->rx.freq > fmdev->rx.region.top_freq)
390 new_frq = fmdev->rx.region.top_freq;
392 if (new_frq) {
393 fmdbg("Current freq is not within band limit boundary,"
394 "switching to %d KHz\n", new_frq);
395 /* Current RX frequency is not in range. So, update it */
396 ret = fm_rx_set_freq(fmdev, new_frq);
399 return ret;
402 /* Reads current mute mode (Mute Off/On/Attenuate)*/
403 int fm_rx_get_mute_mode(struct fmdev *fmdev, u8 *curr_mute_mode)
405 if (fmdev->curr_fmmode != FM_MODE_RX)
406 return -EPERM;
408 if (curr_mute_mode == NULL) {
409 fmerr("Invalid memory\n");
410 return -ENOMEM;
413 *curr_mute_mode = fmdev->rx.mute_mode;
415 return 0;
418 static int fm_config_rx_mute_reg(struct fmdev *fmdev)
420 u16 payload, muteval;
421 int ret;
423 muteval = 0;
424 switch (fmdev->rx.mute_mode) {
425 case FM_MUTE_ON:
426 muteval = FM_RX_AC_MUTE_MODE;
427 break;
429 case FM_MUTE_OFF:
430 muteval = FM_RX_UNMUTE_MODE;
431 break;
433 case FM_MUTE_ATTENUATE:
434 muteval = FM_RX_SOFT_MUTE_FORCE_MODE;
435 break;
437 if (fmdev->rx.rf_depend_mute == FM_RX_RF_DEPENDENT_MUTE_ON)
438 muteval |= FM_RX_RF_DEP_MODE;
439 else
440 muteval &= ~FM_RX_RF_DEP_MODE;
442 payload = muteval;
443 ret = fmc_send_cmd(fmdev, MUTE_STATUS_SET, REG_WR, &payload,
444 sizeof(payload), NULL, NULL);
445 if (ret < 0)
446 return ret;
448 return 0;
451 /* Configures mute mode (Mute Off/On/Attenuate) */
452 int fm_rx_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
454 u8 org_state;
455 int ret;
457 if (fmdev->rx.mute_mode == mute_mode_toset)
458 return 0;
460 org_state = fmdev->rx.mute_mode;
461 fmdev->rx.mute_mode = mute_mode_toset;
463 ret = fm_config_rx_mute_reg(fmdev);
464 if (ret < 0) {
465 fmdev->rx.mute_mode = org_state;
466 return ret;
469 return 0;
472 /* Gets RF dependent soft mute mode enable/disable status */
473 int fm_rx_get_rfdepend_softmute(struct fmdev *fmdev, u8 *curr_mute_mode)
475 if (fmdev->curr_fmmode != FM_MODE_RX)
476 return -EPERM;
478 if (curr_mute_mode == NULL) {
479 fmerr("Invalid memory\n");
480 return -ENOMEM;
483 *curr_mute_mode = fmdev->rx.rf_depend_mute;
485 return 0;
488 /* Sets RF dependent soft mute mode */
489 int fm_rx_set_rfdepend_softmute(struct fmdev *fmdev, u8 rfdepend_mute)
491 u8 org_state;
492 int ret;
494 if (fmdev->curr_fmmode != FM_MODE_RX)
495 return -EPERM;
497 if (rfdepend_mute != FM_RX_RF_DEPENDENT_MUTE_ON &&
498 rfdepend_mute != FM_RX_RF_DEPENDENT_MUTE_OFF) {
499 fmerr("Invalid RF dependent soft mute\n");
500 return -EINVAL;
502 if (fmdev->rx.rf_depend_mute == rfdepend_mute)
503 return 0;
505 org_state = fmdev->rx.rf_depend_mute;
506 fmdev->rx.rf_depend_mute = rfdepend_mute;
508 ret = fm_config_rx_mute_reg(fmdev);
509 if (ret < 0) {
510 fmdev->rx.rf_depend_mute = org_state;
511 return ret;
514 return 0;
517 /* Returns the signal strength level of current channel */
518 int fm_rx_get_rssi_level(struct fmdev *fmdev, u16 *rssilvl)
520 __be16 curr_rssi_lel;
521 u32 resp_len;
522 int ret;
524 if (rssilvl == NULL) {
525 fmerr("Invalid memory\n");
526 return -ENOMEM;
528 /* Read current RSSI level */
529 ret = fmc_send_cmd(fmdev, RSSI_LVL_GET, REG_RD, NULL, 2,
530 &curr_rssi_lel, &resp_len);
531 if (ret < 0)
532 return ret;
534 *rssilvl = be16_to_cpu(curr_rssi_lel);
536 return 0;
540 * Sets the signal strength level that once reached
541 * will stop the auto search process
543 int fm_rx_set_rssi_threshold(struct fmdev *fmdev, short rssi_lvl_toset)
545 u16 payload;
546 int ret;
548 if (rssi_lvl_toset < FM_RX_RSSI_THRESHOLD_MIN ||
549 rssi_lvl_toset > FM_RX_RSSI_THRESHOLD_MAX) {
550 fmerr("Invalid RSSI threshold level\n");
551 return -EINVAL;
553 payload = (u16)rssi_lvl_toset;
554 ret = fmc_send_cmd(fmdev, SEARCH_LVL_SET, REG_WR, &payload,
555 sizeof(payload), NULL, NULL);
556 if (ret < 0)
557 return ret;
559 fmdev->rx.rssi_threshold = rssi_lvl_toset;
561 return 0;
564 /* Returns current RX RSSI threshold value */
565 int fm_rx_get_rssi_threshold(struct fmdev *fmdev, short *curr_rssi_lvl)
567 if (fmdev->curr_fmmode != FM_MODE_RX)
568 return -EPERM;
570 if (curr_rssi_lvl == NULL) {
571 fmerr("Invalid memory\n");
572 return -ENOMEM;
575 *curr_rssi_lvl = fmdev->rx.rssi_threshold;
577 return 0;
580 /* Sets RX stereo/mono modes */
581 int fm_rx_set_stereo_mono(struct fmdev *fmdev, u16 mode)
583 u16 payload;
584 int ret;
586 if (mode != FM_STEREO_MODE && mode != FM_MONO_MODE) {
587 fmerr("Invalid mode\n");
588 return -EINVAL;
591 /* Set stereo/mono mode */
592 payload = (u16)mode;
593 ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_WR, &payload,
594 sizeof(payload), NULL, NULL);
595 if (ret < 0)
596 return ret;
598 /* Set stereo blending mode */
599 payload = FM_STEREO_SOFT_BLEND;
600 ret = fmc_send_cmd(fmdev, MOST_BLEND_SET, REG_WR, &payload,
601 sizeof(payload), NULL, NULL);
602 if (ret < 0)
603 return ret;
605 return 0;
608 /* Gets current RX stereo/mono mode */
609 int fm_rx_get_stereo_mono(struct fmdev *fmdev, u16 *mode)
611 __be16 curr_mode;
612 u32 resp_len;
613 int ret;
615 if (mode == NULL) {
616 fmerr("Invalid memory\n");
617 return -ENOMEM;
620 ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_RD, NULL, 2,
621 &curr_mode, &resp_len);
622 if (ret < 0)
623 return ret;
625 *mode = be16_to_cpu(curr_mode);
627 return 0;
630 /* Choose RX de-emphasis filter mode (50us/75us) */
631 int fm_rx_set_deemphasis_mode(struct fmdev *fmdev, u16 mode)
633 u16 payload;
634 int ret;
636 if (fmdev->curr_fmmode != FM_MODE_RX)
637 return -EPERM;
639 if (mode != FM_RX_EMPHASIS_FILTER_50_USEC &&
640 mode != FM_RX_EMPHASIS_FILTER_75_USEC) {
641 fmerr("Invalid rx de-emphasis mode (%d)\n", mode);
642 return -EINVAL;
645 payload = mode;
646 ret = fmc_send_cmd(fmdev, DEMPH_MODE_SET, REG_WR, &payload,
647 sizeof(payload), NULL, NULL);
648 if (ret < 0)
649 return ret;
651 fmdev->rx.deemphasis_mode = mode;
653 return 0;
656 /* Gets current RX de-emphasis filter mode */
657 int fm_rx_get_deemph_mode(struct fmdev *fmdev, u16 *curr_deemphasis_mode)
659 if (fmdev->curr_fmmode != FM_MODE_RX)
660 return -EPERM;
662 if (curr_deemphasis_mode == NULL) {
663 fmerr("Invalid memory\n");
664 return -ENOMEM;
667 *curr_deemphasis_mode = fmdev->rx.deemphasis_mode;
669 return 0;
672 /* Enable/Disable RX RDS */
673 int fm_rx_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
675 u16 payload;
676 int ret;
678 if (rds_en_dis != FM_RDS_ENABLE && rds_en_dis != FM_RDS_DISABLE) {
679 fmerr("Invalid rds option\n");
680 return -EINVAL;
683 if (rds_en_dis == FM_RDS_ENABLE
684 && fmdev->rx.rds.flag == FM_RDS_DISABLE) {
685 /* Turn on RX RDS and RDS circuit */
686 payload = FM_RX_PWR_SET_FM_AND_RDS_BLK_ON;
687 ret = fmc_send_cmd(fmdev, POWER_SET, REG_WR, &payload,
688 sizeof(payload), NULL, NULL);
689 if (ret < 0)
690 return ret;
692 /* Clear and reset RDS FIFO */
693 payload = FM_RX_RDS_FLUSH_FIFO;
694 ret = fmc_send_cmd(fmdev, RDS_CNTRL_SET, REG_WR, &payload,
695 sizeof(payload), NULL, NULL);
696 if (ret < 0)
697 return ret;
699 /* Read flags - just to clear any pending interrupts. */
700 ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2,
701 NULL, NULL);
702 if (ret < 0)
703 return ret;
705 /* Set RDS FIFO threshold value */
706 payload = FM_RX_RDS_FIFO_THRESHOLD;
707 ret = fmc_send_cmd(fmdev, RDS_MEM_SET, REG_WR, &payload,
708 sizeof(payload), NULL, NULL);
709 if (ret < 0)
710 return ret;
712 /* Enable RDS interrupt */
713 fmdev->irq_info.mask |= FM_RDS_EVENT;
714 payload = fmdev->irq_info.mask;
715 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
716 sizeof(payload), NULL, NULL);
717 if (ret < 0) {
718 fmdev->irq_info.mask &= ~FM_RDS_EVENT;
719 return ret;
722 /* Update our local flag */
723 fmdev->rx.rds.flag = FM_RDS_ENABLE;
724 } else if (rds_en_dis == FM_RDS_DISABLE
725 && fmdev->rx.rds.flag == FM_RDS_ENABLE) {
726 /* Turn off RX RDS */
727 payload = FM_RX_PWR_SET_FM_ON_RDS_OFF;
728 ret = fmc_send_cmd(fmdev, POWER_SET, REG_WR, &payload,
729 sizeof(payload), NULL, NULL);
730 if (ret < 0)
731 return ret;
733 /* Reset RDS pointers */
734 fmdev->rx.rds.last_blk_idx = 0;
735 fmdev->rx.rds.wr_idx = 0;
736 fmdev->rx.rds.rd_idx = 0;
737 fm_rx_reset_station_info(fmdev);
739 /* Update RDS local cache */
740 fmdev->irq_info.mask &= ~(FM_RDS_EVENT);
741 fmdev->rx.rds.flag = FM_RDS_DISABLE;
744 return 0;
747 /* Returns current RX RDS enable/disable status */
748 int fm_rx_get_rds_mode(struct fmdev *fmdev, u8 *curr_rds_en_dis)
750 if (fmdev->curr_fmmode != FM_MODE_RX)
751 return -EPERM;
753 if (curr_rds_en_dis == NULL) {
754 fmerr("Invalid memory\n");
755 return -ENOMEM;
758 *curr_rds_en_dis = fmdev->rx.rds.flag;
760 return 0;
763 /* Sets RDS operation mode (RDS/RDBS) */
764 int fm_rx_set_rds_system(struct fmdev *fmdev, u8 rds_mode)
766 u16 payload;
767 int ret;
769 if (fmdev->curr_fmmode != FM_MODE_RX)
770 return -EPERM;
772 if (rds_mode != FM_RDS_SYSTEM_RDS && rds_mode != FM_RDS_SYSTEM_RBDS) {
773 fmerr("Invalid rds mode\n");
774 return -EINVAL;
776 /* Set RDS operation mode */
777 payload = (u16)rds_mode;
778 ret = fmc_send_cmd(fmdev, RDS_SYSTEM_SET, REG_WR, &payload,
779 sizeof(payload), NULL, NULL);
780 if (ret < 0)
781 return ret;
783 fmdev->rx.rds_mode = rds_mode;
785 return 0;
788 /* Configures Alternate Frequency switch mode */
789 int fm_rx_set_af_switch(struct fmdev *fmdev, u8 af_mode)
791 u16 payload;
792 int ret;
794 if (fmdev->curr_fmmode != FM_MODE_RX)
795 return -EPERM;
797 if (af_mode != FM_RX_RDS_AF_SWITCH_MODE_ON &&
798 af_mode != FM_RX_RDS_AF_SWITCH_MODE_OFF) {
799 fmerr("Invalid af mode\n");
800 return -EINVAL;
802 /* Enable/disable low RSSI interrupt based on af_mode */
803 if (af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
804 fmdev->irq_info.mask |= FM_LEV_EVENT;
805 else
806 fmdev->irq_info.mask &= ~FM_LEV_EVENT;
808 payload = fmdev->irq_info.mask;
809 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
810 sizeof(payload), NULL, NULL);
811 if (ret < 0)
812 return ret;
814 fmdev->rx.af_mode = af_mode;
816 return 0;
819 /* Returns Alternate Frequency switch status */
820 int fm_rx_get_af_switch(struct fmdev *fmdev, u8 *af_mode)
822 if (fmdev->curr_fmmode != FM_MODE_RX)
823 return -EPERM;
825 if (af_mode == NULL) {
826 fmerr("Invalid memory\n");
827 return -ENOMEM;
830 *af_mode = fmdev->rx.af_mode;
832 return 0;