drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / media / radio / wl128x / fmdrv_rx.c
blob419cf2e03bcfc793373f386d6c89109881159ec9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * FM Driver for Connectivity chip of Texas Instruments.
4 * This sub-module of FM driver implements FM RX functionality.
6 * Copyright (C) 2011 Texas Instruments
7 * Author: Raja Mani <raja_mani@ti.com>
8 * Author: Manjunatha Halli <manjunatha_halli@ti.com>
9 */
11 #include "fmdrv.h"
12 #include "fmdrv_common.h"
13 #include "fmdrv_rx.h"
15 void fm_rx_reset_rds_cache(struct fmdev *fmdev)
17 fmdev->rx.rds.flag = FM_RDS_DISABLE;
18 fmdev->rx.rds.last_blk_idx = 0;
19 fmdev->rx.rds.wr_idx = 0;
20 fmdev->rx.rds.rd_idx = 0;
22 if (fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
23 fmdev->irq_info.mask |= FM_LEV_EVENT;
26 void fm_rx_reset_station_info(struct fmdev *fmdev)
28 fmdev->rx.stat_info.picode = FM_NO_PI_CODE;
29 fmdev->rx.stat_info.afcache_size = 0;
30 fmdev->rx.stat_info.af_list_max = 0;
33 int fm_rx_set_freq(struct fmdev *fmdev, u32 freq)
35 unsigned long timeleft;
36 u16 payload, curr_frq, intr_flag;
37 u32 curr_frq_in_khz;
38 u32 resp_len;
39 int ret;
41 if (freq < fmdev->rx.region.bot_freq || freq > fmdev->rx.region.top_freq) {
42 fmerr("Invalid frequency %d\n", freq);
43 return -EINVAL;
46 /* Set audio enable */
47 payload = FM_RX_AUDIO_ENABLE_I2S_AND_ANALOG;
49 ret = fmc_send_cmd(fmdev, AUDIO_ENABLE_SET, REG_WR, &payload,
50 sizeof(payload), NULL, NULL);
51 if (ret < 0)
52 return ret;
54 /* Set hilo to automatic selection */
55 payload = FM_RX_IFFREQ_HILO_AUTOMATIC;
56 ret = fmc_send_cmd(fmdev, HILO_SET, REG_WR, &payload,
57 sizeof(payload), NULL, NULL);
58 if (ret < 0)
59 return ret;
61 /* Calculate frequency index and set*/
62 payload = (freq - fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
64 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_WR, &payload,
65 sizeof(payload), NULL, NULL);
66 if (ret < 0)
67 return ret;
69 /* Read flags - just to clear any pending interrupts if we had */
70 ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2, NULL, NULL);
71 if (ret < 0)
72 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;
83 /* Start tune */
84 payload = FM_TUNER_PRESET_MODE;
85 ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
86 sizeof(payload), NULL, NULL);
87 if (ret < 0)
88 goto exit;
90 /* Wait for tune ended interrupt */
91 init_completion(&fmdev->maintask_comp);
92 timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
93 FM_DRV_TX_TIMEOUT);
94 if (!timeleft) {
95 fmerr("Timeout(%d sec),didn't get tune ended int\n",
96 jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
97 ret = -ETIMEDOUT;
98 goto exit;
101 /* Read freq back to confirm */
102 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, 2, &curr_frq, &resp_len);
103 if (ret < 0)
104 goto exit;
106 curr_frq = be16_to_cpu((__force __be16)curr_frq);
107 curr_frq_in_khz = (fmdev->rx.region.bot_freq + ((u32)curr_frq * FM_FREQ_MUL));
109 if (curr_frq_in_khz != freq) {
110 pr_info("Frequency is set to (%d) but requested freq is (%d)\n",
111 curr_frq_in_khz, freq);
114 /* Update local cache */
115 fmdev->rx.freq = curr_frq_in_khz;
116 exit:
117 /* Re-enable default FM interrupts */
118 fmdev->irq_info.mask = intr_flag;
119 payload = fmdev->irq_info.mask;
120 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
121 sizeof(payload), NULL, NULL);
122 if (ret < 0)
123 return ret;
125 /* Reset RDS cache and current station pointers */
126 fm_rx_reset_rds_cache(fmdev);
127 fm_rx_reset_station_info(fmdev);
129 return ret;
132 static int fm_rx_set_channel_spacing(struct fmdev *fmdev, u32 spacing)
134 u16 payload;
135 int ret;
137 if (spacing > 0 && spacing <= 50000)
138 spacing = FM_CHANNEL_SPACING_50KHZ;
139 else if (spacing > 50000 && spacing <= 100000)
140 spacing = FM_CHANNEL_SPACING_100KHZ;
141 else
142 spacing = FM_CHANNEL_SPACING_200KHZ;
144 /* set channel spacing */
145 payload = spacing;
146 ret = fmc_send_cmd(fmdev, CHANL_BW_SET, REG_WR, &payload,
147 sizeof(payload), NULL, NULL);
148 if (ret < 0)
149 return ret;
151 fmdev->rx.region.chanl_space = spacing * FM_FREQ_MUL;
153 return ret;
156 int fm_rx_seek(struct fmdev *fmdev, u32 seek_upward,
157 u32 wrap_around, u32 spacing)
159 u32 resp_len;
160 u16 curr_frq, next_frq, last_frq;
161 u16 payload, int_reason, intr_flag;
162 u16 offset, space_idx;
163 unsigned long timeleft;
164 int ret;
166 /* Set channel spacing */
167 ret = fm_rx_set_channel_spacing(fmdev, spacing);
168 if (ret < 0) {
169 fmerr("Failed to set channel spacing\n");
170 return ret;
173 /* Read the current frequency from chip */
174 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL,
175 sizeof(curr_frq), &curr_frq, &resp_len);
176 if (ret < 0)
177 return ret;
179 curr_frq = be16_to_cpu((__force __be16)curr_frq);
180 last_frq = (fmdev->rx.region.top_freq - fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
182 /* Check the offset in order to be aligned to the channel spacing*/
183 space_idx = fmdev->rx.region.chanl_space / FM_FREQ_MUL;
184 offset = curr_frq % space_idx;
186 next_frq = seek_upward ? curr_frq + space_idx /* Seek Up */ :
187 curr_frq - space_idx /* Seek Down */ ;
190 * Add or subtract offset in order to stay aligned to the channel
191 * spacing.
193 if ((short)next_frq < 0)
194 next_frq = last_frq - offset;
195 else if (next_frq > last_frq)
196 next_frq = 0 + offset;
198 again:
199 /* Set calculated next frequency to perform seek */
200 payload = next_frq;
201 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_WR, &payload,
202 sizeof(payload), NULL, NULL);
203 if (ret < 0)
204 return ret;
206 /* Set search direction (0:Seek Down, 1:Seek Up) */
207 payload = (seek_upward ? FM_SEARCH_DIRECTION_UP : FM_SEARCH_DIRECTION_DOWN);
208 ret = fmc_send_cmd(fmdev, SEARCH_DIR_SET, REG_WR, &payload,
209 sizeof(payload), NULL, NULL);
210 if (ret < 0)
211 return ret;
213 /* Read flags - just to clear any pending interrupts if we had */
214 ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2, NULL, NULL);
215 if (ret < 0)
216 return ret;
218 /* Enable FR, BL interrupts */
219 intr_flag = fmdev->irq_info.mask;
220 fmdev->irq_info.mask = (FM_FR_EVENT | FM_BL_EVENT);
221 payload = fmdev->irq_info.mask;
222 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
223 sizeof(payload), NULL, NULL);
224 if (ret < 0)
225 return ret;
227 /* Start seek */
228 payload = FM_TUNER_AUTONOMOUS_SEARCH_MODE;
229 ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
230 sizeof(payload), NULL, NULL);
231 if (ret < 0)
232 return ret;
234 /* Wait for tune ended/band limit reached interrupt */
235 init_completion(&fmdev->maintask_comp);
236 timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
237 FM_DRV_RX_SEEK_TIMEOUT);
238 if (!timeleft) {
239 fmerr("Timeout(%d sec),didn't get tune ended int\n",
240 jiffies_to_msecs(FM_DRV_RX_SEEK_TIMEOUT) / 1000);
241 return -ENODATA;
244 int_reason = fmdev->irq_info.flag & (FM_TUNE_COMPLETE | FM_BAND_LIMIT);
246 /* Re-enable default FM interrupts */
247 fmdev->irq_info.mask = intr_flag;
248 payload = fmdev->irq_info.mask;
249 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
250 sizeof(payload), NULL, NULL);
251 if (ret < 0)
252 return ret;
254 if (int_reason & FM_BL_EVENT) {
255 if (wrap_around == 0) {
256 fmdev->rx.freq = seek_upward ?
257 fmdev->rx.region.top_freq :
258 fmdev->rx.region.bot_freq;
259 } else {
260 fmdev->rx.freq = seek_upward ?
261 fmdev->rx.region.bot_freq :
262 fmdev->rx.region.top_freq;
263 /* Calculate frequency index to write */
264 next_frq = (fmdev->rx.freq -
265 fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
266 goto again;
268 } else {
269 /* Read freq to know where operation tune operation stopped */
270 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, 2,
271 &curr_frq, &resp_len);
272 if (ret < 0)
273 return ret;
275 curr_frq = be16_to_cpu((__force __be16)curr_frq);
276 fmdev->rx.freq = (fmdev->rx.region.bot_freq +
277 ((u32)curr_frq * FM_FREQ_MUL));
280 /* Reset RDS cache and current station pointers */
281 fm_rx_reset_rds_cache(fmdev);
282 fm_rx_reset_station_info(fmdev);
284 return ret;
287 int fm_rx_set_volume(struct fmdev *fmdev, u16 vol_to_set)
289 u16 payload;
290 int ret;
292 if (fmdev->curr_fmmode != FM_MODE_RX)
293 return -EPERM;
295 if (vol_to_set > FM_RX_VOLUME_MAX) {
296 fmerr("Volume is not within(%d-%d) range\n",
297 FM_RX_VOLUME_MIN, FM_RX_VOLUME_MAX);
298 return -EINVAL;
300 vol_to_set *= FM_RX_VOLUME_GAIN_STEP;
302 payload = vol_to_set;
303 ret = fmc_send_cmd(fmdev, VOLUME_SET, REG_WR, &payload,
304 sizeof(payload), NULL, NULL);
305 if (ret < 0)
306 return ret;
308 fmdev->rx.volume = vol_to_set;
309 return ret;
312 /* Get volume */
313 int fm_rx_get_volume(struct fmdev *fmdev, u16 *curr_vol)
315 if (fmdev->curr_fmmode != FM_MODE_RX)
316 return -EPERM;
318 if (curr_vol == NULL) {
319 fmerr("Invalid memory\n");
320 return -ENOMEM;
323 *curr_vol = fmdev->rx.volume / FM_RX_VOLUME_GAIN_STEP;
325 return 0;
328 /* To get current band's bottom and top frequency */
329 int fm_rx_get_band_freq_range(struct fmdev *fmdev, u32 *bot_freq, u32 *top_freq)
331 if (bot_freq != NULL)
332 *bot_freq = fmdev->rx.region.bot_freq;
334 if (top_freq != NULL)
335 *top_freq = fmdev->rx.region.top_freq;
337 return 0;
340 /* Returns current band index (0-Europe/US; 1-Japan) */
341 void fm_rx_get_region(struct fmdev *fmdev, u8 *region)
343 *region = fmdev->rx.region.fm_band;
346 /* Sets band (0-Europe/US; 1-Japan) */
347 int fm_rx_set_region(struct fmdev *fmdev, u8 region_to_set)
349 u16 payload;
350 u32 new_frq = 0;
351 int ret;
353 if (region_to_set != FM_BAND_EUROPE_US &&
354 region_to_set != FM_BAND_JAPAN) {
355 fmerr("Invalid band\n");
356 return -EINVAL;
359 if (fmdev->rx.region.fm_band == region_to_set) {
360 fmerr("Requested band is already configured\n");
361 return 0;
364 /* Send cmd to set the band */
365 payload = (u16)region_to_set;
366 ret = fmc_send_cmd(fmdev, BAND_SET, REG_WR, &payload,
367 sizeof(payload), NULL, NULL);
368 if (ret < 0)
369 return ret;
371 fmc_update_region_info(fmdev, region_to_set);
373 /* Check whether current RX frequency is within band boundary */
374 if (fmdev->rx.freq < fmdev->rx.region.bot_freq)
375 new_frq = fmdev->rx.region.bot_freq;
376 else if (fmdev->rx.freq > fmdev->rx.region.top_freq)
377 new_frq = fmdev->rx.region.top_freq;
379 if (new_frq) {
380 fmdbg("Current freq is not within band limit boundary,switching to %d KHz\n",
381 new_frq);
382 /* Current RX frequency is not in range. So, update it */
383 ret = fm_rx_set_freq(fmdev, new_frq);
386 return ret;
389 /* Reads current mute mode (Mute Off/On/Attenuate)*/
390 int fm_rx_get_mute_mode(struct fmdev *fmdev, u8 *curr_mute_mode)
392 if (fmdev->curr_fmmode != FM_MODE_RX)
393 return -EPERM;
395 if (curr_mute_mode == NULL) {
396 fmerr("Invalid memory\n");
397 return -ENOMEM;
400 *curr_mute_mode = fmdev->rx.mute_mode;
402 return 0;
405 static int fm_config_rx_mute_reg(struct fmdev *fmdev)
407 u16 payload, muteval;
408 int ret;
410 muteval = 0;
411 switch (fmdev->rx.mute_mode) {
412 case FM_MUTE_ON:
413 muteval = FM_RX_AC_MUTE_MODE;
414 break;
416 case FM_MUTE_OFF:
417 muteval = FM_RX_UNMUTE_MODE;
418 break;
420 case FM_MUTE_ATTENUATE:
421 muteval = FM_RX_SOFT_MUTE_FORCE_MODE;
422 break;
424 if (fmdev->rx.rf_depend_mute == FM_RX_RF_DEPENDENT_MUTE_ON)
425 muteval |= FM_RX_RF_DEP_MODE;
426 else
427 muteval &= ~FM_RX_RF_DEP_MODE;
429 payload = muteval;
430 ret = fmc_send_cmd(fmdev, MUTE_STATUS_SET, REG_WR, &payload,
431 sizeof(payload), NULL, NULL);
432 if (ret < 0)
433 return ret;
435 return 0;
438 /* Configures mute mode (Mute Off/On/Attenuate) */
439 int fm_rx_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
441 u8 org_state;
442 int ret;
444 if (fmdev->rx.mute_mode == mute_mode_toset)
445 return 0;
447 org_state = fmdev->rx.mute_mode;
448 fmdev->rx.mute_mode = mute_mode_toset;
450 ret = fm_config_rx_mute_reg(fmdev);
451 if (ret < 0) {
452 fmdev->rx.mute_mode = org_state;
453 return ret;
456 return 0;
459 /* Gets RF dependent soft mute mode enable/disable status */
460 int fm_rx_get_rfdepend_softmute(struct fmdev *fmdev, u8 *curr_mute_mode)
462 if (fmdev->curr_fmmode != FM_MODE_RX)
463 return -EPERM;
465 if (curr_mute_mode == NULL) {
466 fmerr("Invalid memory\n");
467 return -ENOMEM;
470 *curr_mute_mode = fmdev->rx.rf_depend_mute;
472 return 0;
475 /* Sets RF dependent soft mute mode */
476 int fm_rx_set_rfdepend_softmute(struct fmdev *fmdev, u8 rfdepend_mute)
478 u8 org_state;
479 int ret;
481 if (fmdev->curr_fmmode != FM_MODE_RX)
482 return -EPERM;
484 if (rfdepend_mute != FM_RX_RF_DEPENDENT_MUTE_ON &&
485 rfdepend_mute != FM_RX_RF_DEPENDENT_MUTE_OFF) {
486 fmerr("Invalid RF dependent soft mute\n");
487 return -EINVAL;
489 if (fmdev->rx.rf_depend_mute == rfdepend_mute)
490 return 0;
492 org_state = fmdev->rx.rf_depend_mute;
493 fmdev->rx.rf_depend_mute = rfdepend_mute;
495 ret = fm_config_rx_mute_reg(fmdev);
496 if (ret < 0) {
497 fmdev->rx.rf_depend_mute = org_state;
498 return ret;
501 return 0;
504 /* Returns the signal strength level of current channel */
505 int fm_rx_get_rssi_level(struct fmdev *fmdev, u16 *rssilvl)
507 __be16 curr_rssi_lel;
508 u32 resp_len;
509 int ret;
511 if (rssilvl == NULL) {
512 fmerr("Invalid memory\n");
513 return -ENOMEM;
515 /* Read current RSSI level */
516 ret = fmc_send_cmd(fmdev, RSSI_LVL_GET, REG_RD, NULL, 2,
517 &curr_rssi_lel, &resp_len);
518 if (ret < 0)
519 return ret;
521 *rssilvl = be16_to_cpu(curr_rssi_lel);
523 return 0;
527 * Sets the signal strength level that once reached
528 * will stop the auto search process
530 int fm_rx_set_rssi_threshold(struct fmdev *fmdev, short rssi_lvl_toset)
532 u16 payload;
533 int ret;
535 if (rssi_lvl_toset < FM_RX_RSSI_THRESHOLD_MIN ||
536 rssi_lvl_toset > FM_RX_RSSI_THRESHOLD_MAX) {
537 fmerr("Invalid RSSI threshold level\n");
538 return -EINVAL;
540 payload = (u16)rssi_lvl_toset;
541 ret = fmc_send_cmd(fmdev, SEARCH_LVL_SET, REG_WR, &payload,
542 sizeof(payload), NULL, NULL);
543 if (ret < 0)
544 return ret;
546 fmdev->rx.rssi_threshold = rssi_lvl_toset;
548 return 0;
551 /* Returns current RX RSSI threshold value */
552 int fm_rx_get_rssi_threshold(struct fmdev *fmdev, short *curr_rssi_lvl)
554 if (fmdev->curr_fmmode != FM_MODE_RX)
555 return -EPERM;
557 if (curr_rssi_lvl == NULL) {
558 fmerr("Invalid memory\n");
559 return -ENOMEM;
562 *curr_rssi_lvl = fmdev->rx.rssi_threshold;
564 return 0;
567 /* Sets RX stereo/mono modes */
568 int fm_rx_set_stereo_mono(struct fmdev *fmdev, u16 mode)
570 u16 payload;
571 int ret;
573 if (mode != FM_STEREO_MODE && mode != FM_MONO_MODE) {
574 fmerr("Invalid mode\n");
575 return -EINVAL;
578 /* Set stereo/mono mode */
579 payload = (u16)mode;
580 ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_WR, &payload,
581 sizeof(payload), NULL, NULL);
582 if (ret < 0)
583 return ret;
585 /* Set stereo blending mode */
586 payload = FM_STEREO_SOFT_BLEND;
587 ret = fmc_send_cmd(fmdev, MOST_BLEND_SET, REG_WR, &payload,
588 sizeof(payload), NULL, NULL);
589 if (ret < 0)
590 return ret;
592 return 0;
595 /* Gets current RX stereo/mono mode */
596 int fm_rx_get_stereo_mono(struct fmdev *fmdev, u16 *mode)
598 __be16 curr_mode;
599 u32 resp_len;
600 int ret;
602 if (mode == NULL) {
603 fmerr("Invalid memory\n");
604 return -ENOMEM;
607 ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_RD, NULL, 2,
608 &curr_mode, &resp_len);
609 if (ret < 0)
610 return ret;
612 *mode = be16_to_cpu(curr_mode);
614 return 0;
617 /* Choose RX de-emphasis filter mode (50us/75us) */
618 int fm_rx_set_deemphasis_mode(struct fmdev *fmdev, u16 mode)
620 u16 payload;
621 int ret;
623 if (fmdev->curr_fmmode != FM_MODE_RX)
624 return -EPERM;
626 if (mode != FM_RX_EMPHASIS_FILTER_50_USEC &&
627 mode != FM_RX_EMPHASIS_FILTER_75_USEC) {
628 fmerr("Invalid rx de-emphasis mode (%d)\n", mode);
629 return -EINVAL;
632 payload = mode;
633 ret = fmc_send_cmd(fmdev, DEMPH_MODE_SET, REG_WR, &payload,
634 sizeof(payload), NULL, NULL);
635 if (ret < 0)
636 return ret;
638 fmdev->rx.deemphasis_mode = mode;
640 return 0;
643 /* Gets current RX de-emphasis filter mode */
644 int fm_rx_get_deemph_mode(struct fmdev *fmdev, u16 *curr_deemphasis_mode)
646 if (fmdev->curr_fmmode != FM_MODE_RX)
647 return -EPERM;
649 if (curr_deemphasis_mode == NULL) {
650 fmerr("Invalid memory\n");
651 return -ENOMEM;
654 *curr_deemphasis_mode = fmdev->rx.deemphasis_mode;
656 return 0;
659 /* Enable/Disable RX RDS */
660 int fm_rx_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
662 u16 payload;
663 int ret;
665 if (rds_en_dis != FM_RDS_ENABLE && rds_en_dis != FM_RDS_DISABLE) {
666 fmerr("Invalid rds option\n");
667 return -EINVAL;
670 if (rds_en_dis == FM_RDS_ENABLE
671 && fmdev->rx.rds.flag == FM_RDS_DISABLE) {
672 /* Turn on RX RDS and RDS circuit */
673 payload = FM_RX_PWR_SET_FM_AND_RDS_BLK_ON;
674 ret = fmc_send_cmd(fmdev, POWER_SET, REG_WR, &payload,
675 sizeof(payload), NULL, NULL);
676 if (ret < 0)
677 return ret;
679 /* Clear and reset RDS FIFO */
680 payload = FM_RX_RDS_FLUSH_FIFO;
681 ret = fmc_send_cmd(fmdev, RDS_CNTRL_SET, REG_WR, &payload,
682 sizeof(payload), NULL, NULL);
683 if (ret < 0)
684 return ret;
686 /* Read flags - just to clear any pending interrupts. */
687 ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2,
688 NULL, NULL);
689 if (ret < 0)
690 return ret;
692 /* Set RDS FIFO threshold value */
693 payload = FM_RX_RDS_FIFO_THRESHOLD;
694 ret = fmc_send_cmd(fmdev, RDS_MEM_SET, REG_WR, &payload,
695 sizeof(payload), NULL, NULL);
696 if (ret < 0)
697 return ret;
699 /* Enable RDS interrupt */
700 fmdev->irq_info.mask |= FM_RDS_EVENT;
701 payload = fmdev->irq_info.mask;
702 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
703 sizeof(payload), NULL, NULL);
704 if (ret < 0) {
705 fmdev->irq_info.mask &= ~FM_RDS_EVENT;
706 return ret;
709 /* Update our local flag */
710 fmdev->rx.rds.flag = FM_RDS_ENABLE;
711 } else if (rds_en_dis == FM_RDS_DISABLE
712 && fmdev->rx.rds.flag == FM_RDS_ENABLE) {
713 /* Turn off RX RDS */
714 payload = FM_RX_PWR_SET_FM_ON_RDS_OFF;
715 ret = fmc_send_cmd(fmdev, POWER_SET, REG_WR, &payload,
716 sizeof(payload), NULL, NULL);
717 if (ret < 0)
718 return ret;
720 /* Reset RDS pointers */
721 fmdev->rx.rds.last_blk_idx = 0;
722 fmdev->rx.rds.wr_idx = 0;
723 fmdev->rx.rds.rd_idx = 0;
724 fm_rx_reset_station_info(fmdev);
726 /* Update RDS local cache */
727 fmdev->irq_info.mask &= ~(FM_RDS_EVENT);
728 fmdev->rx.rds.flag = FM_RDS_DISABLE;
731 return 0;
734 /* Returns current RX RDS enable/disable status */
735 int fm_rx_get_rds_mode(struct fmdev *fmdev, u8 *curr_rds_en_dis)
737 if (fmdev->curr_fmmode != FM_MODE_RX)
738 return -EPERM;
740 if (curr_rds_en_dis == NULL) {
741 fmerr("Invalid memory\n");
742 return -ENOMEM;
745 *curr_rds_en_dis = fmdev->rx.rds.flag;
747 return 0;
750 /* Sets RDS operation mode (RDS/RDBS) */
751 int fm_rx_set_rds_system(struct fmdev *fmdev, u8 rds_mode)
753 u16 payload;
754 int ret;
756 if (fmdev->curr_fmmode != FM_MODE_RX)
757 return -EPERM;
759 if (rds_mode != FM_RDS_SYSTEM_RDS && rds_mode != FM_RDS_SYSTEM_RBDS) {
760 fmerr("Invalid rds mode\n");
761 return -EINVAL;
763 /* Set RDS operation mode */
764 payload = (u16)rds_mode;
765 ret = fmc_send_cmd(fmdev, RDS_SYSTEM_SET, REG_WR, &payload,
766 sizeof(payload), NULL, NULL);
767 if (ret < 0)
768 return ret;
770 fmdev->rx.rds_mode = rds_mode;
772 return 0;
775 /* Configures Alternate Frequency switch mode */
776 int fm_rx_set_af_switch(struct fmdev *fmdev, u8 af_mode)
778 u16 payload;
779 int ret;
781 if (fmdev->curr_fmmode != FM_MODE_RX)
782 return -EPERM;
784 if (af_mode != FM_RX_RDS_AF_SWITCH_MODE_ON &&
785 af_mode != FM_RX_RDS_AF_SWITCH_MODE_OFF) {
786 fmerr("Invalid af mode\n");
787 return -EINVAL;
789 /* Enable/disable low RSSI interrupt based on af_mode */
790 if (af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
791 fmdev->irq_info.mask |= FM_LEV_EVENT;
792 else
793 fmdev->irq_info.mask &= ~FM_LEV_EVENT;
795 payload = fmdev->irq_info.mask;
796 ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
797 sizeof(payload), NULL, NULL);
798 if (ret < 0)
799 return ret;
801 fmdev->rx.af_mode = af_mode;
803 return 0;
806 /* Returns Alternate Frequency switch status */
807 int fm_rx_get_af_switch(struct fmdev *fmdev, u8 *af_mode)
809 if (fmdev->curr_fmmode != FM_MODE_RX)
810 return -EPERM;
812 if (af_mode == NULL) {
813 fmerr("Invalid memory\n");
814 return -ENOMEM;
817 *af_mode = fmdev->rx.af_mode;
819 return 0;