blob: 3e4b9eaeb7a45e40bca1758ced9b452c92285913 [file] [log] [blame]
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001/*
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +05302 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_fm"
21/*#define LOG_NDEBUG 0*/
22#define LOG_NDDEBUG 0
23
24#include <errno.h>
25#include <math.h>
26#include <cutils/log.h>
27
28#include "audio_hw.h"
29#include "platform.h"
30#include "platform_api.h"
31#include <stdlib.h>
32#include <cutils/str_parms.h>
33
34#ifdef FM_ENABLED
35#define AUDIO_PARAMETER_KEY_HANDLE_FM "handle_fm"
36#define AUDIO_PARAMETER_KEY_FM_VOLUME "fm_volume"
Naresh Tanniru9e95c242014-12-08 16:49:22 +053037#define AUDIO_PARAMETER_KEY_REC_PLAY_CONC "rec_play_conc_on"
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053038#define FM_LOOPBACK_DRAIN_TIME_MS 2
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070039
40static struct pcm_config pcm_config_fm = {
41 .channels = 2,
42 .rate = 48000,
43 .period_size = 256,
44 .period_count = 4,
45 .format = PCM_FORMAT_S16_LE,
46 .start_threshold = 0,
47 .stop_threshold = INT_MAX,
48 .avail_min = 0,
49};
50
51struct fm_module {
52 struct pcm *fm_pcm_rx;
53 struct pcm *fm_pcm_tx;
54 bool is_fm_running;
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080055 float fm_volume;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053056 bool restart_fm;
57 int scard_state;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070058};
59
60static struct fm_module fmmod = {
61 .fm_pcm_rx = NULL,
62 .fm_pcm_tx = NULL,
63 .fm_volume = 0,
64 .is_fm_running = 0,
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053065 .restart_fm = 0,
66 .scard_state = SND_CARD_STATE_ONLINE,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070067};
68
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053069static int32_t fm_set_volume(struct audio_device *adev, float value, bool persist)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070070{
71 int32_t vol, ret = 0;
72 struct mixer_ctl *ctl;
Banajit Goswami88d6cc52014-04-10 17:59:02 -070073 const char *mixer_ctl_name = FM_RX_VOLUME;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070074
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080075 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070076 ALOGD("%s: (%f)\n", __func__, value);
77
78 if (value < 0.0) {
79 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
80 value = 0.0;
81 } else if (value > 1.0) {
82 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
83 value = 1.0;
84 }
85 vol = lrint((value * 0x2000) + 0.5);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053086 if (persist)
87 fmmod.fm_volume = value;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070088
89 if (!fmmod.is_fm_running) {
90 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
91 return -EIO;
92 }
93
94 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
95 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
96 if (!ctl) {
97 ALOGE("%s: Could not get ctl for mixer cmd - %s",
98 __func__, mixer_ctl_name);
99 return -EINVAL;
100 }
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800101 mixer_ctl_set_value(ctl, 0, vol);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800102 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700103 return ret;
104}
105
106static int32_t fm_stop(struct audio_device *adev)
107{
108 int32_t i, ret = 0;
109 struct audio_usecase *uc_info;
110
111 ALOGD("%s: enter", __func__);
112 fmmod.is_fm_running = false;
113
114 /* 1. Close the PCM devices */
115 if (fmmod.fm_pcm_rx) {
116 pcm_close(fmmod.fm_pcm_rx);
117 fmmod.fm_pcm_rx = NULL;
118 }
119 if (fmmod.fm_pcm_tx) {
120 pcm_close(fmmod.fm_pcm_tx);
121 fmmod.fm_pcm_tx = NULL;
122 }
123
124 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
125 if (uc_info == NULL) {
126 ALOGE("%s: Could not find the usecase (%d) in the list",
127 __func__, USECASE_VOICE_CALL);
128 return -EINVAL;
129 }
130
131 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700132 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700133
134 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700135 disable_snd_device(adev, uc_info->out_snd_device);
136 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700137
138 list_remove(&uc_info->list);
139 free(uc_info);
140
141 ALOGD("%s: exit: status(%d)", __func__, ret);
142 return ret;
143}
144
145static int32_t fm_start(struct audio_device *adev)
146{
147 int32_t i, ret = 0;
148 struct audio_usecase *uc_info;
149 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
150
151 ALOGD("%s: enter", __func__);
152
153 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700154
155 if (!uc_info)
156 return -ENOMEM;
157
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700158 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
159 uc_info->type = PCM_PLAYBACK;
160 uc_info->stream.out = adev->primary_output;
161 uc_info->devices = adev->primary_output->devices;
162 uc_info->in_snd_device = SND_DEVICE_NONE;
163 uc_info->out_snd_device = SND_DEVICE_NONE;
164
165 list_add_tail(&adev->usecase_list, &uc_info->list);
166
167 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
168
169 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
170 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
171
172 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
173 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
174 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
175 ret = -EIO;
176 goto exit;
177 }
178
179 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
180 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
181
182 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800183 __func__, adev->snd_card, pcm_dev_rx_id);
184 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
185 pcm_dev_rx_id,
186 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700187 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
188 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
189 ret = -EIO;
190 goto exit;
191 }
192
193 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800194 __func__, adev->snd_card, pcm_dev_tx_id);
195 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
196 pcm_dev_tx_id,
197 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700198 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
199 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
200 ret = -EIO;
201 goto exit;
202 }
203 pcm_start(fmmod.fm_pcm_rx);
204 pcm_start(fmmod.fm_pcm_tx);
205
206 fmmod.is_fm_running = true;
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530207 fm_set_volume(adev, fmmod.fm_volume, false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700208
209 ALOGD("%s: exit: status(%d)", __func__, ret);
210 return 0;
211
212exit:
213 fm_stop(adev);
214 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
215 return ret;
216}
217
218void audio_extn_fm_set_parameters(struct audio_device *adev,
219 struct str_parms *parms)
220{
221 int ret, val;
222 char value[32]={0};
223 float vol =0.0;
224
225 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530226 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
227 if (ret >= 0) {
228 char *snd_card_status = value+2;
229 if (strstr(snd_card_status, "OFFLINE")) {
230 fmmod.scard_state = SND_CARD_STATE_OFFLINE;
231 }
232 else if (strstr(snd_card_status, "ONLINE")) {
233 fmmod.scard_state = SND_CARD_STATE_ONLINE;
234 }
235 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700236 if(fmmod.is_fm_running) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530237 if (fmmod.scard_state == SND_CARD_STATE_OFFLINE) {
238 ALOGD("sound card is OFFLINE, stop FM");
239 fm_stop(adev);
240 fmmod.restart_fm = 1;
241 }
242
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700243 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
244 value, sizeof(value));
245 if (ret >= 0) {
246 val = atoi(value);
247 if(val > 0)
248 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
249 }
250 }
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530251 if (fmmod.restart_fm && (fmmod.scard_state == SND_CARD_STATE_ONLINE)) {
252 ALOGD("sound card is ONLINE, restart FM");
253 fmmod.restart_fm = 0;
254 fm_start(adev);
255 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700256
257 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
258 value, sizeof(value));
259 if (ret >= 0) {
260 val = atoi(value);
261 ALOGD("%s: FM usecase", __func__);
262 if (val != 0) {
263 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700264 && fmmod.is_fm_running == false) {
265 adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700266 fm_start(adev);
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700267 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530268 && fmmod.is_fm_running == true) {
269 fm_set_volume(adev, 0, false);
270 usleep(FM_LOOPBACK_DRAIN_TIME_MS*1000);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700271 fm_stop(adev);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530272 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700273 }
274 }
275
276 memset(value, 0, sizeof(value));
277 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
278 value, sizeof(value));
279 if (ret >= 0) {
280 if (sscanf(value, "%f", &vol) != 1){
281 ALOGE("%s: error in retrieving fm volume", __func__);
282 ret = -EIO;
283 goto exit;
284 }
285 ALOGD("%s: set_fm_volume usecase", __func__);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530286 fm_set_volume(adev, vol, true);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700287 }
288
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530289#ifdef RECORD_PLAY_CONCURRENCY
290 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_REC_PLAY_CONC,
291 value, sizeof(value));
292 if ((ret >= 0)
293 && (fmmod.is_fm_running == true)) {
294
295 if (!strncmp("true", value, sizeof("true")))
296 ALOGD("Record play concurrency ON Forcing FM device reroute");
297 else
298 ALOGD("Record play concurrency OFF Forcing FM device reroute");
299
300 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530301 fm_set_volume(adev, fmmod.fm_volume, false);
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530302 }
303#endif
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700304exit:
305 ALOGV("%s: exit", __func__);
306}
307#endif /* FM_ENABLED end */