blob: 0a048cd9e1e73728ff90a93c1410f947073b39b1 [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"
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +053047#define AUDIO_PARAMETER_KEY_FM_ROUTING "fm_routing"
Preetam Singh Ranawat0fae9b52018-12-17 17:08:30 +053048#define AUDIO_PARAMETER_KEY_FM_STATUS "fm_status"
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053049#define FM_LOOPBACK_DRAIN_TIME_MS 2
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070050
51static struct pcm_config pcm_config_fm = {
52 .channels = 2,
53 .rate = 48000,
54 .period_size = 256,
55 .period_count = 4,
56 .format = PCM_FORMAT_S16_LE,
57 .start_threshold = 0,
58 .stop_threshold = INT_MAX,
59 .avail_min = 0,
60};
61
62struct fm_module {
63 struct pcm *fm_pcm_rx;
64 struct pcm *fm_pcm_tx;
65 bool is_fm_running;
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053066 bool is_fm_muted;
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080067 float fm_volume;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053068 bool restart_fm;
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +053069 audio_devices_t fm_device;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053070 card_status_t card_status;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070071};
72
73static struct fm_module fmmod = {
74 .fm_pcm_rx = NULL,
75 .fm_pcm_tx = NULL,
76 .fm_volume = 0,
77 .is_fm_running = 0,
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053078 .is_fm_muted = 0,
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053079 .restart_fm = 0,
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +053080 .fm_device = 0,
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053081 .card_status = CARD_STATUS_ONLINE,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070082};
83
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053084static int32_t fm_set_volume(struct audio_device *adev, float value, bool persist)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070085{
86 int32_t vol, ret = 0;
87 struct mixer_ctl *ctl;
Banajit Goswami88d6cc52014-04-10 17:59:02 -070088 const char *mixer_ctl_name = FM_RX_VOLUME;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070089
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080090 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070091 ALOGD("%s: (%f)\n", __func__, value);
92
93 if (value < 0.0) {
94 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
95 value = 0.0;
96 } else if (value > 1.0) {
97 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
98 value = 1.0;
99 }
100 vol = lrint((value * 0x2000) + 0.5);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530101 if (persist)
102 fmmod.fm_volume = value;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700103
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530104 if (fmmod.is_fm_muted == true && vol > 0) {
105 ALOGD("%s: fm is muted, applying '0' volume instead of '%d'.",
106 __func__, vol);
107 vol = 0;
108 }
109
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700110 if (!fmmod.is_fm_running) {
111 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
112 return -EIO;
113 }
114
115 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
116 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
117 if (!ctl) {
118 ALOGE("%s: Could not get ctl for mixer cmd - %s",
119 __func__, mixer_ctl_name);
120 return -EINVAL;
121 }
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800122 mixer_ctl_set_value(ctl, 0, vol);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800123 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700124 return ret;
125}
126
127static int32_t fm_stop(struct audio_device *adev)
128{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530129 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700130 struct audio_usecase *uc_info;
131
132 ALOGD("%s: enter", __func__);
133 fmmod.is_fm_running = false;
134
135 /* 1. Close the PCM devices */
136 if (fmmod.fm_pcm_rx) {
137 pcm_close(fmmod.fm_pcm_rx);
138 fmmod.fm_pcm_rx = NULL;
139 }
140 if (fmmod.fm_pcm_tx) {
141 pcm_close(fmmod.fm_pcm_tx);
142 fmmod.fm_pcm_tx = NULL;
143 }
144
145 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
146 if (uc_info == NULL) {
147 ALOGE("%s: Could not find the usecase (%d) in the list",
148 __func__, USECASE_VOICE_CALL);
149 return -EINVAL;
150 }
151
152 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700153 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700154
155 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700156 disable_snd_device(adev, uc_info->out_snd_device);
157 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700158
159 list_remove(&uc_info->list);
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530160 free(uc_info->stream.out);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700161 free(uc_info);
162
163 ALOGD("%s: exit: status(%d)", __func__, ret);
164 return ret;
165}
166
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530167
168static int32_t fm_start(struct audio_device *adev, audio_devices_t outputDevices)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700169{
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530170 struct stream_out *fm_out;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530171 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700172 struct audio_usecase *uc_info;
173 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
174
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530175
176 ALOGD("%s: Start FM over output device %d ", __func__, outputDevices);
Soumya Managolie7651c42018-06-28 16:04:57 +0530177 fmmod.is_fm_running = true;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700178
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530179 fm_out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
180 if (!fm_out)
181 return -ENOMEM;
182
183 fm_out->sample_rate = 48000;
184 fm_out->format = AUDIO_FORMAT_PCM_16_BIT;
185 fm_out->usecase = USECASE_AUDIO_PLAYBACK_FM;
186 fm_out->config = pcm_config_fm;
187 fm_out->devices = outputDevices;
188
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700189 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700190
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530191 if (!uc_info) {
192 free(fm_out);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700193 return -ENOMEM;
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530194 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700195
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700196 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
197 uc_info->type = PCM_PLAYBACK;
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530198 uc_info->stream.out = fm_out;
199 uc_info->devices = outputDevices;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700200 uc_info->in_snd_device = SND_DEVICE_NONE;
201 uc_info->out_snd_device = SND_DEVICE_NONE;
202
203 list_add_tail(&adev->usecase_list, &uc_info->list);
204
205 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
206
207 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
208 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
209
210 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
211 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
212 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
213 ret = -EIO;
214 goto exit;
215 }
216
217 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
218 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
219
220 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800221 __func__, adev->snd_card, pcm_dev_rx_id);
222 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
223 pcm_dev_rx_id,
224 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700225 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
226 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
227 ret = -EIO;
228 goto exit;
229 }
230
231 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800232 __func__, adev->snd_card, pcm_dev_tx_id);
233 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
234 pcm_dev_tx_id,
235 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700236 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
237 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
238 ret = -EIO;
239 goto exit;
240 }
241 pcm_start(fmmod.fm_pcm_rx);
242 pcm_start(fmmod.fm_pcm_tx);
243
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530244 fmmod.fm_device = fm_out->devices;
245 fm_set_volume(adev, fmmod.fm_volume, false);
246
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700247 ALOGD("%s: exit: status(%d)", __func__, ret);
248 return 0;
249
250exit:
251 fm_stop(adev);
252 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
253 return ret;
254}
255
Preetam Singh Ranawat0fae9b52018-12-17 17:08:30 +0530256void audio_extn_fm_get_parameters(struct str_parms *query, struct str_parms *reply)
257{
258 int ret, val;
259 char value[32]={0};
260
261 ALOGV("%s: enter", __func__);
262 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_FM_STATUS, value, sizeof(value));
263 if (ret >= 0) {
264 val = (fmmod.is_fm_running ? 1: 0);
265 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_FM_STATUS, val);
266 }
267}
268
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700269void audio_extn_fm_set_parameters(struct audio_device *adev,
270 struct str_parms *parms)
271{
272 int ret, val;
273 char value[32]={0};
274 float vol =0.0;
275
276 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530277 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
278 if (ret >= 0) {
279 char *snd_card_status = value+2;
280 if (strstr(snd_card_status, "OFFLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530281 fmmod.card_status = CARD_STATUS_OFFLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530282 }
283 else if (strstr(snd_card_status, "ONLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530284 fmmod.card_status = CARD_STATUS_ONLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530285 }
286 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700287 if(fmmod.is_fm_running) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530288 if (fmmod.card_status == CARD_STATUS_OFFLINE) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530289 ALOGD("sound card is OFFLINE, stop FM");
290 fm_stop(adev);
291 fmmod.restart_fm = 1;
292 }
293
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700294 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
295 value, sizeof(value));
296 if (ret >= 0) {
297 val = atoi(value);
298 if(val > 0)
299 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
300 }
301 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530302 if (fmmod.restart_fm && (fmmod.card_status == CARD_STATUS_ONLINE)) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530303 ALOGD("sound card is ONLINE, restart FM");
304 fmmod.restart_fm = 0;
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530305 fm_start(adev, fmmod.fm_device);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530306 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700307
308 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
309 value, sizeof(value));
310 if (ret >= 0) {
311 val = atoi(value);
312 ALOGD("%s: FM usecase", __func__);
313 if (val != 0) {
314 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700315 && fmmod.is_fm_running == false) {
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530316 audio_devices_t OutputDevice = val & ~AUDIO_DEVICE_OUT_FM;
317 fm_start(adev, OutputDevice);
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700318 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530319 && fmmod.is_fm_running == true) {
320 fm_set_volume(adev, 0, false);
321 usleep(FM_LOOPBACK_DRAIN_TIME_MS*1000);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700322 fm_stop(adev);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530323 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700324 }
325 }
326
Preetam Singh Ranawatc889c262018-11-01 11:58:08 +0530327 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_ROUTING,
328 value, sizeof(value));
329 if (ret >= 0 && fmmod.is_fm_running) {
330 val = atoi(value);
331 ALOGD("%s: FM usecase", __func__);
332 if (val != 0) {
333 if(val & AUDIO_DEVICE_OUT_FM) {
334 audio_devices_t OutputDevice = val & ~AUDIO_DEVICE_OUT_FM;
335 fm_set_volume(adev, 0, false);
336 fm_stop(adev);
337 fm_start(adev, OutputDevice);
338 }
339 }
340 }
341
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700342 memset(value, 0, sizeof(value));
343 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
344 value, sizeof(value));
345 if (ret >= 0) {
346 if (sscanf(value, "%f", &vol) != 1){
347 ALOGE("%s: error in retrieving fm volume", __func__);
348 ret = -EIO;
349 goto exit;
350 }
351 ALOGD("%s: set_fm_volume usecase", __func__);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530352 fm_set_volume(adev, vol, true);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700353 }
354
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530355 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_MUTE,
356 value, sizeof(value));
357 if (ret >= 0) {
358 if (value[0] == '1')
359 fmmod.is_fm_muted = true;
360 else
361 fmmod.is_fm_muted = false;
362 ALOGV("%s: set_fm_volume from param mute", __func__);
363 fm_set_volume(adev, fmmod.fm_volume, false);
364 }
365
Yidong Huang7939a3b2018-01-23 17:32:30 +0800366 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_RESTORE_VOLUME,
367 value, sizeof(value));
368 if (ret >= 0) {
369 if (value[0] == '1')
370 fm_set_volume(adev, fmmod.fm_volume, false);
371 ALOGV("%s: set_fm_volume from param restore volume", __func__);
372 }
373
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530374#ifdef RECORD_PLAY_CONCURRENCY
375 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_REC_PLAY_CONC,
376 value, sizeof(value));
377 if ((ret >= 0)
378 && (fmmod.is_fm_running == true)) {
379
380 if (!strncmp("true", value, sizeof("true")))
381 ALOGD("Record play concurrency ON Forcing FM device reroute");
382 else
383 ALOGD("Record play concurrency OFF Forcing FM device reroute");
384
385 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530386 fm_set_volume(adev, fmmod.fm_volume, false);
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530387 }
388#endif
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700389exit:
390 ALOGV("%s: exit", __func__);
391}
Mingming Yin12125e82015-10-26 20:40:36 -0700392#endif /* FM_POWER_OPT end */