1 // SPDX-License-Identifier: GPL-2.0-only
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>
12 #include "fmdrv_common.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;
41 if (freq < fmdev->rx.region.bot_freq || freq > fmdev->rx.region.top_freq) {
42 fmerr("Invalid frequency %d\n", freq);
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);
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);
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);
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);
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);
84 payload = FM_TUNER_PRESET_MODE;
85 ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
86 sizeof(payload), NULL, NULL);
90 /* Wait for tune ended interrupt */
91 init_completion(&fmdev->maintask_comp);
92 timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
95 fmerr("Timeout(%d sec),didn't get tune ended int\n",
96 jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
101 /* Read freq back to confirm */
102 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, 2, &curr_frq, &resp_len);
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;
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);
125 /* Reset RDS cache and current station pointers */
126 fm_rx_reset_rds_cache(fmdev);
127 fm_rx_reset_station_info(fmdev);
132 static int fm_rx_set_channel_spacing(struct fmdev *fmdev, u32 spacing)
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;
142 spacing = FM_CHANNEL_SPACING_200KHZ;
144 /* set channel spacing */
146 ret = fmc_send_cmd(fmdev, CHANL_BW_SET, REG_WR, &payload,
147 sizeof(payload), NULL, NULL);
151 fmdev->rx.region.chanl_space = spacing * FM_FREQ_MUL;
156 int fm_rx_seek(struct fmdev *fmdev, u32 seek_upward,
157 u32 wrap_around, u32 spacing)
160 u16 curr_frq, next_frq, last_frq;
161 u16 payload, int_reason, intr_flag;
162 u16 offset, space_idx;
163 unsigned long timeleft;
166 /* Set channel spacing */
167 ret = fm_rx_set_channel_spacing(fmdev, spacing);
169 fmerr("Failed to set channel spacing\n");
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);
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
193 if ((short)next_frq < 0)
194 next_frq = last_frq - offset;
195 else if (next_frq > last_frq)
196 next_frq = 0 + offset;
199 /* Set calculated next frequency to perform seek */
201 ret = fmc_send_cmd(fmdev, FREQ_SET, REG_WR, &payload,
202 sizeof(payload), NULL, NULL);
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);
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);
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);
228 payload = FM_TUNER_AUTONOMOUS_SEARCH_MODE;
229 ret = fmc_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
230 sizeof(payload), NULL, NULL);
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);
239 fmerr("Timeout(%d sec),didn't get tune ended int\n",
240 jiffies_to_msecs(FM_DRV_RX_SEEK_TIMEOUT) / 1000);
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);
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;
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;
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);
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);
287 int fm_rx_set_volume(struct fmdev *fmdev, u16 vol_to_set)
292 if (fmdev->curr_fmmode != FM_MODE_RX)
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);
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);
308 fmdev->rx.volume = vol_to_set;
313 int fm_rx_get_volume(struct fmdev *fmdev, u16 *curr_vol)
315 if (fmdev->curr_fmmode != FM_MODE_RX)
318 if (curr_vol == NULL) {
319 fmerr("Invalid memory\n");
323 *curr_vol = fmdev->rx.volume / FM_RX_VOLUME_GAIN_STEP;
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;
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)
353 if (region_to_set != FM_BAND_EUROPE_US &&
354 region_to_set != FM_BAND_JAPAN) {
355 fmerr("Invalid band\n");
359 if (fmdev->rx.region.fm_band == region_to_set) {
360 fmerr("Requested band is already configured\n");
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);
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;
380 fmdbg("Current freq is not within band limit boundary,switching to %d KHz\n",
382 /* Current RX frequency is not in range. So, update it */
383 ret = fm_rx_set_freq(fmdev, new_frq);
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)
395 if (curr_mute_mode == NULL) {
396 fmerr("Invalid memory\n");
400 *curr_mute_mode = fmdev->rx.mute_mode;
405 static int fm_config_rx_mute_reg(struct fmdev *fmdev)
407 u16 payload, muteval;
411 switch (fmdev->rx.mute_mode) {
413 muteval = FM_RX_AC_MUTE_MODE;
417 muteval = FM_RX_UNMUTE_MODE;
420 case FM_MUTE_ATTENUATE:
421 muteval = FM_RX_SOFT_MUTE_FORCE_MODE;
424 if (fmdev->rx.rf_depend_mute == FM_RX_RF_DEPENDENT_MUTE_ON)
425 muteval |= FM_RX_RF_DEP_MODE;
427 muteval &= ~FM_RX_RF_DEP_MODE;
430 ret = fmc_send_cmd(fmdev, MUTE_STATUS_SET, REG_WR, &payload,
431 sizeof(payload), NULL, NULL);
438 /* Configures mute mode (Mute Off/On/Attenuate) */
439 int fm_rx_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
444 if (fmdev->rx.mute_mode == mute_mode_toset)
447 org_state = fmdev->rx.mute_mode;
448 fmdev->rx.mute_mode = mute_mode_toset;
450 ret = fm_config_rx_mute_reg(fmdev);
452 fmdev->rx.mute_mode = org_state;
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)
465 if (curr_mute_mode == NULL) {
466 fmerr("Invalid memory\n");
470 *curr_mute_mode = fmdev->rx.rf_depend_mute;
475 /* Sets RF dependent soft mute mode */
476 int fm_rx_set_rfdepend_softmute(struct fmdev *fmdev, u8 rfdepend_mute)
481 if (fmdev->curr_fmmode != FM_MODE_RX)
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");
489 if (fmdev->rx.rf_depend_mute == rfdepend_mute)
492 org_state = fmdev->rx.rf_depend_mute;
493 fmdev->rx.rf_depend_mute = rfdepend_mute;
495 ret = fm_config_rx_mute_reg(fmdev);
497 fmdev->rx.rf_depend_mute = org_state;
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;
511 if (rssilvl == NULL) {
512 fmerr("Invalid memory\n");
515 /* Read current RSSI level */
516 ret = fmc_send_cmd(fmdev, RSSI_LVL_GET, REG_RD, NULL, 2,
517 &curr_rssi_lel, &resp_len);
521 *rssilvl = be16_to_cpu(curr_rssi_lel);
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)
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");
540 payload = (u16)rssi_lvl_toset;
541 ret = fmc_send_cmd(fmdev, SEARCH_LVL_SET, REG_WR, &payload,
542 sizeof(payload), NULL, NULL);
546 fmdev->rx.rssi_threshold = rssi_lvl_toset;
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)
557 if (curr_rssi_lvl == NULL) {
558 fmerr("Invalid memory\n");
562 *curr_rssi_lvl = fmdev->rx.rssi_threshold;
567 /* Sets RX stereo/mono modes */
568 int fm_rx_set_stereo_mono(struct fmdev *fmdev, u16 mode)
573 if (mode != FM_STEREO_MODE && mode != FM_MONO_MODE) {
574 fmerr("Invalid mode\n");
578 /* Set stereo/mono mode */
580 ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_WR, &payload,
581 sizeof(payload), NULL, NULL);
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);
595 /* Gets current RX stereo/mono mode */
596 int fm_rx_get_stereo_mono(struct fmdev *fmdev, u16 *mode)
603 fmerr("Invalid memory\n");
607 ret = fmc_send_cmd(fmdev, MOST_MODE_SET, REG_RD, NULL, 2,
608 &curr_mode, &resp_len);
612 *mode = be16_to_cpu(curr_mode);
617 /* Choose RX de-emphasis filter mode (50us/75us) */
618 int fm_rx_set_deemphasis_mode(struct fmdev *fmdev, u16 mode)
623 if (fmdev->curr_fmmode != FM_MODE_RX)
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);
633 ret = fmc_send_cmd(fmdev, DEMPH_MODE_SET, REG_WR, &payload,
634 sizeof(payload), NULL, NULL);
638 fmdev->rx.deemphasis_mode = mode;
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)
649 if (curr_deemphasis_mode == NULL) {
650 fmerr("Invalid memory\n");
654 *curr_deemphasis_mode = fmdev->rx.deemphasis_mode;
659 /* Enable/Disable RX RDS */
660 int fm_rx_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
665 if (rds_en_dis != FM_RDS_ENABLE && rds_en_dis != FM_RDS_DISABLE) {
666 fmerr("Invalid rds option\n");
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);
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);
686 /* Read flags - just to clear any pending interrupts. */
687 ret = fmc_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, 2,
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);
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);
705 fmdev->irq_info.mask &= ~FM_RDS_EVENT;
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);
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;
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)
740 if (curr_rds_en_dis == NULL) {
741 fmerr("Invalid memory\n");
745 *curr_rds_en_dis = fmdev->rx.rds.flag;
750 /* Sets RDS operation mode (RDS/RDBS) */
751 int fm_rx_set_rds_system(struct fmdev *fmdev, u8 rds_mode)
756 if (fmdev->curr_fmmode != FM_MODE_RX)
759 if (rds_mode != FM_RDS_SYSTEM_RDS && rds_mode != FM_RDS_SYSTEM_RBDS) {
760 fmerr("Invalid rds mode\n");
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);
770 fmdev->rx.rds_mode = rds_mode;
775 /* Configures Alternate Frequency switch mode */
776 int fm_rx_set_af_switch(struct fmdev *fmdev, u8 af_mode)
781 if (fmdev->curr_fmmode != FM_MODE_RX)
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");
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;
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);
801 fmdev->rx.af_mode = af_mode;
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)
812 if (af_mode == NULL) {
813 fmerr("Invalid memory\n");
817 *af_mode = fmdev->rx.af_mode;