blob: 24facea368576e0b311c3191c5150b03cb4ba246 [file] [log] [blame]
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001/*
Yidong Huang7939a3b2018-01-23 17:32:30 +08002 * Copyright (c) 2013-2018, 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>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053027#include <unistd.h>
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070028
29#include "audio_hw.h"
30#include "platform.h"
31#include "platform_api.h"
32#include <stdlib.h>
33#include <cutils/str_parms.h>
34
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053035#ifdef DYNAMIC_LOG_ENABLED
36#include <log_xml_parser.h>
37#define LOG_MASK HAL_MOD_FILE_FM
38#include <log_utils.h>
39#endif
40
Mingming Yin12125e82015-10-26 20:40:36 -070041#ifdef FM_POWER_OPT
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070042#define AUDIO_PARAMETER_KEY_HANDLE_FM "handle_fm"
43#define AUDIO_PARAMETER_KEY_FM_VOLUME "fm_volume"
Naresh Tanniru9e95c242014-12-08 16:49:22 +053044#define AUDIO_PARAMETER_KEY_REC_PLAY_CONC "rec_play_conc_on"
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053045#define AUDIO_PARAMETER_KEY_FM_MUTE "fm_mute"
Yidong Huang7939a3b2018-01-23 17:32:30 +080046#define AUDIO_PARAMETER_KEY_FM_RESTORE_VOLUME "fm_restore_volume"
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053047#define FM_LOOPBACK_DRAIN_TIME_MS 2
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070048
49static struct pcm_config pcm_config_fm = {
50 .channels = 2,
51 .rate = 48000,
52 .period_size = 256,
53 .period_count = 4,
54 .format = PCM_FORMAT_S16_LE,
55 .start_threshold = 0,
56 .stop_threshold = INT_MAX,
57 .avail_min = 0,
58};
59
60struct fm_module {
61 struct pcm *fm_pcm_rx;
62 struct pcm *fm_pcm_tx;
63 bool is_fm_running;
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053064 bool is_fm_muted;
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080065 float fm_volume;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053066 bool restart_fm;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053067 card_status_t card_status;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070068};
69
70static struct fm_module fmmod = {
71 .fm_pcm_rx = NULL,
72 .fm_pcm_tx = NULL,
73 .fm_volume = 0,
74 .is_fm_running = 0,
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053075 .is_fm_muted = 0,
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053076 .restart_fm = 0,
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053077 .card_status = CARD_STATUS_ONLINE,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070078};
79
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053080static int32_t fm_set_volume(struct audio_device *adev, float value, bool persist)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070081{
82 int32_t vol, ret = 0;
83 struct mixer_ctl *ctl;
Banajit Goswami88d6cc52014-04-10 17:59:02 -070084 const char *mixer_ctl_name = FM_RX_VOLUME;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070085
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080086 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070087 ALOGD("%s: (%f)\n", __func__, value);
88
89 if (value < 0.0) {
90 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
91 value = 0.0;
92 } else if (value > 1.0) {
93 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
94 value = 1.0;
95 }
96 vol = lrint((value * 0x2000) + 0.5);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053097 if (persist)
98 fmmod.fm_volume = value;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070099
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530100 if (fmmod.is_fm_muted == true && vol > 0) {
101 ALOGD("%s: fm is muted, applying '0' volume instead of '%d'.",
102 __func__, vol);
103 vol = 0;
104 }
105
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700106 if (!fmmod.is_fm_running) {
107 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
108 return -EIO;
109 }
110
111 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
112 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
113 if (!ctl) {
114 ALOGE("%s: Could not get ctl for mixer cmd - %s",
115 __func__, mixer_ctl_name);
116 return -EINVAL;
117 }
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800118 mixer_ctl_set_value(ctl, 0, vol);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800119 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700120 return ret;
121}
122
123static int32_t fm_stop(struct audio_device *adev)
124{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530125 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700126 struct audio_usecase *uc_info;
127
128 ALOGD("%s: enter", __func__);
129 fmmod.is_fm_running = false;
130
131 /* 1. Close the PCM devices */
132 if (fmmod.fm_pcm_rx) {
133 pcm_close(fmmod.fm_pcm_rx);
134 fmmod.fm_pcm_rx = NULL;
135 }
136 if (fmmod.fm_pcm_tx) {
137 pcm_close(fmmod.fm_pcm_tx);
138 fmmod.fm_pcm_tx = NULL;
139 }
140
141 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
142 if (uc_info == NULL) {
143 ALOGE("%s: Could not find the usecase (%d) in the list",
144 __func__, USECASE_VOICE_CALL);
145 return -EINVAL;
146 }
147
148 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700149 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700150
151 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700152 disable_snd_device(adev, uc_info->out_snd_device);
153 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700154
155 list_remove(&uc_info->list);
156 free(uc_info);
157
158 ALOGD("%s: exit: status(%d)", __func__, ret);
159 return ret;
160}
161
162static int32_t fm_start(struct audio_device *adev)
163{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530164 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700165 struct audio_usecase *uc_info;
166 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
167
168 ALOGD("%s: enter", __func__);
169
170 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700171
172 if (!uc_info)
173 return -ENOMEM;
174
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700175 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
176 uc_info->type = PCM_PLAYBACK;
177 uc_info->stream.out = adev->primary_output;
178 uc_info->devices = adev->primary_output->devices;
179 uc_info->in_snd_device = SND_DEVICE_NONE;
180 uc_info->out_snd_device = SND_DEVICE_NONE;
181
182 list_add_tail(&adev->usecase_list, &uc_info->list);
183
184 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
185
186 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
187 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
188
189 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
190 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
191 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
192 ret = -EIO;
193 goto exit;
194 }
195
196 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
197 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
198
199 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800200 __func__, adev->snd_card, pcm_dev_rx_id);
201 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
202 pcm_dev_rx_id,
203 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700204 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
205 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
206 ret = -EIO;
207 goto exit;
208 }
209
210 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800211 __func__, adev->snd_card, pcm_dev_tx_id);
212 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
213 pcm_dev_tx_id,
214 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700215 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
216 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
217 ret = -EIO;
218 goto exit;
219 }
220 pcm_start(fmmod.fm_pcm_rx);
221 pcm_start(fmmod.fm_pcm_tx);
222
223 fmmod.is_fm_running = true;
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530224 fm_set_volume(adev, fmmod.fm_volume, false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700225
226 ALOGD("%s: exit: status(%d)", __func__, ret);
227 return 0;
228
229exit:
230 fm_stop(adev);
231 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
232 return ret;
233}
234
235void audio_extn_fm_set_parameters(struct audio_device *adev,
236 struct str_parms *parms)
237{
238 int ret, val;
239 char value[32]={0};
240 float vol =0.0;
241
242 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530243 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
244 if (ret >= 0) {
245 char *snd_card_status = value+2;
246 if (strstr(snd_card_status, "OFFLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530247 fmmod.card_status = CARD_STATUS_OFFLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530248 }
249 else if (strstr(snd_card_status, "ONLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530250 fmmod.card_status = CARD_STATUS_ONLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530251 }
252 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700253 if(fmmod.is_fm_running) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530254 if (fmmod.card_status == CARD_STATUS_OFFLINE) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530255 ALOGD("sound card is OFFLINE, stop FM");
256 fm_stop(adev);
257 fmmod.restart_fm = 1;
258 }
259
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700260 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
261 value, sizeof(value));
262 if (ret >= 0) {
263 val = atoi(value);
264 if(val > 0)
265 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
266 }
267 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530268 if (fmmod.restart_fm && (fmmod.card_status == CARD_STATUS_ONLINE)) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530269 ALOGD("sound card is ONLINE, restart FM");
270 fmmod.restart_fm = 0;
271 fm_start(adev);
272 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700273
274 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
275 value, sizeof(value));
276 if (ret >= 0) {
277 val = atoi(value);
278 ALOGD("%s: FM usecase", __func__);
279 if (val != 0) {
280 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700281 && fmmod.is_fm_running == false) {
282 adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700283 fm_start(adev);
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700284 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530285 && fmmod.is_fm_running == true) {
286 fm_set_volume(adev, 0, false);
287 usleep(FM_LOOPBACK_DRAIN_TIME_MS*1000);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700288 fm_stop(adev);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530289 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700290 }
291 }
292
293 memset(value, 0, sizeof(value));
294 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
295 value, sizeof(value));
296 if (ret >= 0) {
297 if (sscanf(value, "%f", &vol) != 1){
298 ALOGE("%s: error in retrieving fm volume", __func__);
299 ret = -EIO;
300 goto exit;
301 }
302 ALOGD("%s: set_fm_volume usecase", __func__);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530303 fm_set_volume(adev, vol, true);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700304 }
305
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530306 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_MUTE,
307 value, sizeof(value));
308 if (ret >= 0) {
309 if (value[0] == '1')
310 fmmod.is_fm_muted = true;
311 else
312 fmmod.is_fm_muted = false;
313 ALOGV("%s: set_fm_volume from param mute", __func__);
314 fm_set_volume(adev, fmmod.fm_volume, false);
315 }
316
Yidong Huang7939a3b2018-01-23 17:32:30 +0800317 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_RESTORE_VOLUME,
318 value, sizeof(value));
319 if (ret >= 0) {
320 if (value[0] == '1')
321 fm_set_volume(adev, fmmod.fm_volume, false);
322 ALOGV("%s: set_fm_volume from param restore volume", __func__);
323 }
324
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530325#ifdef RECORD_PLAY_CONCURRENCY
326 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_REC_PLAY_CONC,
327 value, sizeof(value));
328 if ((ret >= 0)
329 && (fmmod.is_fm_running == true)) {
330
331 if (!strncmp("true", value, sizeof("true")))
332 ALOGD("Record play concurrency ON Forcing FM device reroute");
333 else
334 ALOGD("Record play concurrency OFF Forcing FM device reroute");
335
336 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530337 fm_set_volume(adev, fmmod.fm_volume, false);
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530338 }
339#endif
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700340exit:
341 ALOGV("%s: exit", __func__);
342}
Mingming Yin12125e82015-10-26 20:40:36 -0700343#endif /* FM_POWER_OPT end */