blob: 785da6599e6d0bd98a4150808e3344c3ba133555 [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>
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
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053034#ifdef DYNAMIC_LOG_ENABLED
35#include <log_xml_parser.h>
36#define LOG_MASK HAL_MOD_FILE_FM
37#include <log_utils.h>
38#endif
39
Mingming Yin12125e82015-10-26 20:40:36 -070040#ifdef FM_POWER_OPT
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"
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053046#define FM_LOOPBACK_DRAIN_TIME_MS 2
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070047
48static struct pcm_config pcm_config_fm = {
49 .channels = 2,
50 .rate = 48000,
51 .period_size = 256,
52 .period_count = 4,
53 .format = PCM_FORMAT_S16_LE,
54 .start_threshold = 0,
55 .stop_threshold = INT_MAX,
56 .avail_min = 0,
57};
58
59struct fm_module {
60 struct pcm *fm_pcm_rx;
61 struct pcm *fm_pcm_tx;
62 bool is_fm_running;
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053063 bool is_fm_muted;
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080064 float fm_volume;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053065 bool restart_fm;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053066 card_status_t card_status;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070067};
68
69static struct fm_module fmmod = {
70 .fm_pcm_rx = NULL,
71 .fm_pcm_tx = NULL,
72 .fm_volume = 0,
73 .is_fm_running = 0,
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053074 .is_fm_muted = 0,
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053075 .restart_fm = 0,
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053076 .card_status = CARD_STATUS_ONLINE,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070077};
78
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053079static int32_t fm_set_volume(struct audio_device *adev, float value, bool persist)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070080{
81 int32_t vol, ret = 0;
82 struct mixer_ctl *ctl;
Banajit Goswami88d6cc52014-04-10 17:59:02 -070083 const char *mixer_ctl_name = FM_RX_VOLUME;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070084
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080085 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070086 ALOGD("%s: (%f)\n", __func__, value);
87
88 if (value < 0.0) {
89 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
90 value = 0.0;
91 } else if (value > 1.0) {
92 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
93 value = 1.0;
94 }
95 vol = lrint((value * 0x2000) + 0.5);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053096 if (persist)
97 fmmod.fm_volume = value;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070098
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053099 if (fmmod.is_fm_muted == true && vol > 0) {
100 ALOGD("%s: fm is muted, applying '0' volume instead of '%d'.",
101 __func__, vol);
102 vol = 0;
103 }
104
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700105 if (!fmmod.is_fm_running) {
106 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
107 return -EIO;
108 }
109
110 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
111 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
112 if (!ctl) {
113 ALOGE("%s: Could not get ctl for mixer cmd - %s",
114 __func__, mixer_ctl_name);
115 return -EINVAL;
116 }
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800117 mixer_ctl_set_value(ctl, 0, vol);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800118 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700119 return ret;
120}
121
122static int32_t fm_stop(struct audio_device *adev)
123{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530124 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700125 struct audio_usecase *uc_info;
126
127 ALOGD("%s: enter", __func__);
128 fmmod.is_fm_running = false;
129
130 /* 1. Close the PCM devices */
131 if (fmmod.fm_pcm_rx) {
132 pcm_close(fmmod.fm_pcm_rx);
133 fmmod.fm_pcm_rx = NULL;
134 }
135 if (fmmod.fm_pcm_tx) {
136 pcm_close(fmmod.fm_pcm_tx);
137 fmmod.fm_pcm_tx = NULL;
138 }
139
140 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
141 if (uc_info == NULL) {
142 ALOGE("%s: Could not find the usecase (%d) in the list",
143 __func__, USECASE_VOICE_CALL);
144 return -EINVAL;
145 }
146
147 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700148 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700149
150 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700151 disable_snd_device(adev, uc_info->out_snd_device);
152 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700153
154 list_remove(&uc_info->list);
155 free(uc_info);
156
157 ALOGD("%s: exit: status(%d)", __func__, ret);
158 return ret;
159}
160
161static int32_t fm_start(struct audio_device *adev)
162{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530163 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700164 struct audio_usecase *uc_info;
165 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
166
167 ALOGD("%s: enter", __func__);
168
169 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700170
171 if (!uc_info)
172 return -ENOMEM;
173
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700174 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
175 uc_info->type = PCM_PLAYBACK;
176 uc_info->stream.out = adev->primary_output;
177 uc_info->devices = adev->primary_output->devices;
178 uc_info->in_snd_device = SND_DEVICE_NONE;
179 uc_info->out_snd_device = SND_DEVICE_NONE;
180
181 list_add_tail(&adev->usecase_list, &uc_info->list);
182
183 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
184
185 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
186 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
187
188 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
189 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
190 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
191 ret = -EIO;
192 goto exit;
193 }
194
195 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
196 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
197
198 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800199 __func__, adev->snd_card, pcm_dev_rx_id);
200 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
201 pcm_dev_rx_id,
202 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700203 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
204 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
205 ret = -EIO;
206 goto exit;
207 }
208
209 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800210 __func__, adev->snd_card, pcm_dev_tx_id);
211 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
212 pcm_dev_tx_id,
213 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700214 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
215 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
216 ret = -EIO;
217 goto exit;
218 }
219 pcm_start(fmmod.fm_pcm_rx);
220 pcm_start(fmmod.fm_pcm_tx);
221
222 fmmod.is_fm_running = true;
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530223 fm_set_volume(adev, fmmod.fm_volume, false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700224
225 ALOGD("%s: exit: status(%d)", __func__, ret);
226 return 0;
227
228exit:
229 fm_stop(adev);
230 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
231 return ret;
232}
233
234void audio_extn_fm_set_parameters(struct audio_device *adev,
235 struct str_parms *parms)
236{
237 int ret, val;
238 char value[32]={0};
239 float vol =0.0;
240
241 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530242 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
243 if (ret >= 0) {
244 char *snd_card_status = value+2;
245 if (strstr(snd_card_status, "OFFLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530246 fmmod.card_status = CARD_STATUS_OFFLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530247 }
248 else if (strstr(snd_card_status, "ONLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530249 fmmod.card_status = CARD_STATUS_ONLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530250 }
251 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700252 if(fmmod.is_fm_running) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530253 if (fmmod.card_status == CARD_STATUS_OFFLINE) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530254 ALOGD("sound card is OFFLINE, stop FM");
255 fm_stop(adev);
256 fmmod.restart_fm = 1;
257 }
258
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700259 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
260 value, sizeof(value));
261 if (ret >= 0) {
262 val = atoi(value);
263 if(val > 0)
264 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
265 }
266 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530267 if (fmmod.restart_fm && (fmmod.card_status == CARD_STATUS_ONLINE)) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530268 ALOGD("sound card is ONLINE, restart FM");
269 fmmod.restart_fm = 0;
270 fm_start(adev);
271 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700272
273 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
274 value, sizeof(value));
275 if (ret >= 0) {
276 val = atoi(value);
277 ALOGD("%s: FM usecase", __func__);
278 if (val != 0) {
279 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700280 && fmmod.is_fm_running == false) {
281 adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700282 fm_start(adev);
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700283 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530284 && fmmod.is_fm_running == true) {
285 fm_set_volume(adev, 0, false);
286 usleep(FM_LOOPBACK_DRAIN_TIME_MS*1000);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700287 fm_stop(adev);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530288 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700289 }
290 }
291
292 memset(value, 0, sizeof(value));
293 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
294 value, sizeof(value));
295 if (ret >= 0) {
296 if (sscanf(value, "%f", &vol) != 1){
297 ALOGE("%s: error in retrieving fm volume", __func__);
298 ret = -EIO;
299 goto exit;
300 }
301 ALOGD("%s: set_fm_volume usecase", __func__);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530302 fm_set_volume(adev, vol, true);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700303 }
304
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530305 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_MUTE,
306 value, sizeof(value));
307 if (ret >= 0) {
308 if (value[0] == '1')
309 fmmod.is_fm_muted = true;
310 else
311 fmmod.is_fm_muted = false;
312 ALOGV("%s: set_fm_volume from param mute", __func__);
313 fm_set_volume(adev, fmmod.fm_volume, false);
314 }
315
Yidong Huang7939a3b2018-01-23 17:32:30 +0800316 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_RESTORE_VOLUME,
317 value, sizeof(value));
318 if (ret >= 0) {
319 if (value[0] == '1')
320 fm_set_volume(adev, fmmod.fm_volume, false);
321 ALOGV("%s: set_fm_volume from param restore volume", __func__);
322 }
323
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530324#ifdef RECORD_PLAY_CONCURRENCY
325 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_REC_PLAY_CONC,
326 value, sizeof(value));
327 if ((ret >= 0)
328 && (fmmod.is_fm_running == true)) {
329
330 if (!strncmp("true", value, sizeof("true")))
331 ALOGD("Record play concurrency ON Forcing FM device reroute");
332 else
333 ALOGD("Record play concurrency OFF Forcing FM device reroute");
334
335 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530336 fm_set_volume(adev, fmmod.fm_volume, false);
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530337 }
338#endif
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700339exit:
340 ALOGV("%s: exit", __func__);
341}
Mingming Yin12125e82015-10-26 20:40:36 -0700342#endif /* FM_POWER_OPT end */