blob: 3c1ec6836218f9613bf289bbbcce6db48813d87a [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
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070041#define AUDIO_PARAMETER_KEY_HANDLE_FM "handle_fm"
42#define AUDIO_PARAMETER_KEY_FM_VOLUME "fm_volume"
Naresh Tanniru9e95c242014-12-08 16:49:22 +053043#define AUDIO_PARAMETER_KEY_REC_PLAY_CONC "rec_play_conc_on"
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053044#define AUDIO_PARAMETER_KEY_FM_MUTE "fm_mute"
Yidong Huang7939a3b2018-01-23 17:32:30 +080045#define AUDIO_PARAMETER_KEY_FM_RESTORE_VOLUME "fm_restore_volume"
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +053046#define AUDIO_PARAMETER_KEY_FM_ROUTING "fm_routing"
Preetam Singh Ranawat3fcfa262018-12-17 17:08:30 +053047#define AUDIO_PARAMETER_KEY_FM_STATUS "fm_status"
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053048#define FM_LOOPBACK_DRAIN_TIME_MS 2
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070049
50static struct pcm_config pcm_config_fm = {
51 .channels = 2,
52 .rate = 48000,
53 .period_size = 256,
54 .period_count = 4,
55 .format = PCM_FORMAT_S16_LE,
56 .start_threshold = 0,
57 .stop_threshold = INT_MAX,
58 .avail_min = 0,
59};
60
61struct fm_module {
62 struct pcm *fm_pcm_rx;
63 struct pcm *fm_pcm_tx;
64 bool is_fm_running;
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053065 bool is_fm_muted;
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080066 float fm_volume;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053067 bool restart_fm;
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +053068 audio_devices_t fm_device;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053069 card_status_t card_status;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070070};
71
72static struct fm_module fmmod = {
73 .fm_pcm_rx = NULL,
74 .fm_pcm_tx = NULL,
75 .fm_volume = 0,
76 .is_fm_running = 0,
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053077 .is_fm_muted = 0,
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053078 .restart_fm = 0,
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +053079 .fm_device = 0,
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053080 .card_status = CARD_STATUS_ONLINE,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070081};
82
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053083static int32_t fm_set_volume(struct audio_device *adev, float value, bool persist)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070084{
85 int32_t vol, ret = 0;
86 struct mixer_ctl *ctl;
Banajit Goswami88d6cc52014-04-10 17:59:02 -070087 const char *mixer_ctl_name = FM_RX_VOLUME;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070088
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080089 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070090 ALOGD("%s: (%f)\n", __func__, value);
91
92 if (value < 0.0) {
93 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
94 value = 0.0;
95 } else if (value > 1.0) {
96 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
97 value = 1.0;
98 }
99 vol = lrint((value * 0x2000) + 0.5);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530100 if (persist)
101 fmmod.fm_volume = value;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700102
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530103 if (fmmod.is_fm_muted == true && vol > 0) {
104 ALOGD("%s: fm is muted, applying '0' volume instead of '%d'.",
105 __func__, vol);
106 vol = 0;
107 }
108
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700109 if (!fmmod.is_fm_running) {
110 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
111 return -EIO;
112 }
113
114 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
115 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
116 if (!ctl) {
117 ALOGE("%s: Could not get ctl for mixer cmd - %s",
118 __func__, mixer_ctl_name);
119 return -EINVAL;
120 }
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800121 mixer_ctl_set_value(ctl, 0, vol);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800122 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700123 return ret;
124}
125
126static int32_t fm_stop(struct audio_device *adev)
127{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530128 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700129 struct audio_usecase *uc_info;
130
131 ALOGD("%s: enter", __func__);
132 fmmod.is_fm_running = false;
133
134 /* 1. Close the PCM devices */
135 if (fmmod.fm_pcm_rx) {
136 pcm_close(fmmod.fm_pcm_rx);
137 fmmod.fm_pcm_rx = NULL;
138 }
139 if (fmmod.fm_pcm_tx) {
140 pcm_close(fmmod.fm_pcm_tx);
141 fmmod.fm_pcm_tx = NULL;
142 }
143
144 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
145 if (uc_info == NULL) {
146 ALOGE("%s: Could not find the usecase (%d) in the list",
147 __func__, USECASE_VOICE_CALL);
148 return -EINVAL;
149 }
150
151 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700152 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700153
154 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700155 disable_snd_device(adev, uc_info->out_snd_device);
156 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700157
158 list_remove(&uc_info->list);
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530159 free(uc_info->stream.out);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700160 free(uc_info);
161
162 ALOGD("%s: exit: status(%d)", __func__, ret);
163 return ret;
164}
165
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530166
167static int32_t fm_start(struct audio_device *adev, audio_devices_t outputDevices)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700168{
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530169 struct stream_out *fm_out;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530170 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700171 struct audio_usecase *uc_info;
172 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
173
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530174 ALOGD("%s: Start FM over output device %d ", __func__, outputDevices);
175
176 fm_out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
177 if (!fm_out)
178 return -ENOMEM;
179
180 fm_out->sample_rate = 48000;
181 fm_out->format = AUDIO_FORMAT_PCM_16_BIT;
182 fm_out->usecase = USECASE_AUDIO_PLAYBACK_FM;
183 fm_out->config = pcm_config_fm;
184 fm_out->devices = outputDevices;
Soumya Managoli6993b762018-06-28 16:04:57 +0530185 fmmod.is_fm_running = true;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700186
187 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700188
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530189 if (!uc_info) {
190 free(fm_out);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700191 return -ENOMEM;
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530192 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700193
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700194 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
195 uc_info->type = PCM_PLAYBACK;
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530196 uc_info->stream.out = fm_out;
197 uc_info->devices = outputDevices;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700198 uc_info->in_snd_device = SND_DEVICE_NONE;
199 uc_info->out_snd_device = SND_DEVICE_NONE;
200
201 list_add_tail(&adev->usecase_list, &uc_info->list);
202
203 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
204
205 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
206 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
207
208 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
209 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
210 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
211 ret = -EIO;
212 goto exit;
213 }
214
215 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
216 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
217
218 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800219 __func__, adev->snd_card, pcm_dev_rx_id);
220 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
221 pcm_dev_rx_id,
222 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700223 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
224 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
225 ret = -EIO;
226 goto exit;
227 }
228
229 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800230 __func__, adev->snd_card, pcm_dev_tx_id);
231 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
232 pcm_dev_tx_id,
233 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700234 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
235 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
236 ret = -EIO;
237 goto exit;
238 }
239 pcm_start(fmmod.fm_pcm_rx);
240 pcm_start(fmmod.fm_pcm_tx);
241
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530242 fmmod.fm_device = fm_out->devices;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700243
244 ALOGD("%s: exit: status(%d)", __func__, ret);
245 return 0;
246
247exit:
248 fm_stop(adev);
249 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
250 return ret;
251}
252
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800253void fm_get_parameters(struct str_parms *query, struct str_parms *reply)
Preetam Singh Ranawat3fcfa262018-12-17 17:08:30 +0530254{
255 int ret, val;
256 char value[32]={0};
257
258 ALOGV("%s: enter", __func__);
259 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_FM_STATUS, value, sizeof(value));
260 if (ret >= 0) {
261 val = (fmmod.is_fm_running ? 1: 0);
262 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_FM_STATUS, val);
263 }
264}
265
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800266void fm_set_parameters(struct audio_device *adev,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700267 struct str_parms *parms)
268{
269 int ret, val;
270 char value[32]={0};
271 float vol =0.0;
272
273 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530274 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
275 if (ret >= 0) {
276 char *snd_card_status = value+2;
277 if (strstr(snd_card_status, "OFFLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530278 fmmod.card_status = CARD_STATUS_OFFLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530279 }
280 else if (strstr(snd_card_status, "ONLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530281 fmmod.card_status = CARD_STATUS_ONLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530282 }
283 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700284 if(fmmod.is_fm_running) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530285 if (fmmod.card_status == CARD_STATUS_OFFLINE) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530286 ALOGD("sound card is OFFLINE, stop FM");
287 fm_stop(adev);
288 fmmod.restart_fm = 1;
289 }
290
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700291 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
292 value, sizeof(value));
293 if (ret >= 0) {
294 val = atoi(value);
295 if(val > 0)
296 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
297 }
298 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530299 if (fmmod.restart_fm && (fmmod.card_status == CARD_STATUS_ONLINE)) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530300 ALOGD("sound card is ONLINE, restart FM");
301 fmmod.restart_fm = 0;
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530302 fm_start(adev, fmmod.fm_device);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530303 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700304
305 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
306 value, sizeof(value));
307 if (ret >= 0) {
308 val = atoi(value);
309 ALOGD("%s: FM usecase", __func__);
310 if (val != 0) {
311 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700312 && fmmod.is_fm_running == false) {
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530313 audio_devices_t OutputDevice = val & ~AUDIO_DEVICE_OUT_FM;
314 fm_start(adev, OutputDevice);
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700315 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530316 && fmmod.is_fm_running == true) {
317 fm_set_volume(adev, 0, false);
318 usleep(FM_LOOPBACK_DRAIN_TIME_MS*1000);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700319 fm_stop(adev);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530320 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700321 }
322 }
323
Preetam Singh Ranawat0e4d9e02018-11-01 11:58:08 +0530324 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_ROUTING,
325 value, sizeof(value));
326 if (ret >= 0 && fmmod.is_fm_running) {
327 val = atoi(value);
328 ALOGD("%s: FM usecase", __func__);
329 if (val != 0) {
330 if(val & AUDIO_DEVICE_OUT_FM) {
331 audio_devices_t OutputDevice = val & ~AUDIO_DEVICE_OUT_FM;
332 fm_set_volume(adev, 0, false);
333 fm_stop(adev);
334 fm_start(adev, OutputDevice);
335 }
336 }
337 }
338
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700339 memset(value, 0, sizeof(value));
340 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
341 value, sizeof(value));
342 if (ret >= 0) {
343 if (sscanf(value, "%f", &vol) != 1){
344 ALOGE("%s: error in retrieving fm volume", __func__);
345 ret = -EIO;
346 goto exit;
347 }
348 ALOGD("%s: set_fm_volume usecase", __func__);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530349 fm_set_volume(adev, vol, true);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700350 }
351
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530352 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_MUTE,
353 value, sizeof(value));
354 if (ret >= 0) {
355 if (value[0] == '1')
356 fmmod.is_fm_muted = true;
357 else
358 fmmod.is_fm_muted = false;
359 ALOGV("%s: set_fm_volume from param mute", __func__);
360 fm_set_volume(adev, fmmod.fm_volume, false);
361 }
362
Yidong Huang7939a3b2018-01-23 17:32:30 +0800363 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_RESTORE_VOLUME,
364 value, sizeof(value));
365 if (ret >= 0) {
366 if (value[0] == '1')
367 fm_set_volume(adev, fmmod.fm_volume, false);
368 ALOGV("%s: set_fm_volume from param restore volume", __func__);
369 }
370
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530371#ifdef RECORD_PLAY_CONCURRENCY
372 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_REC_PLAY_CONC,
373 value, sizeof(value));
374 if ((ret >= 0)
375 && (fmmod.is_fm_running == true)) {
376
377 if (!strncmp("true", value, sizeof("true")))
378 ALOGD("Record play concurrency ON Forcing FM device reroute");
379 else
380 ALOGD("Record play concurrency OFF Forcing FM device reroute");
381
382 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530383 fm_set_volume(adev, fmmod.fm_volume, false);
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530384 }
385#endif
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700386exit:
387 ALOGV("%s: exit", __func__);
388}