blob: b34bb220678f66fc909347498723f0e37b1bf210 [file] [log] [blame]
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001/*
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05302 * Copyright (c) 2013-2017, 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"
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053045#define FM_LOOPBACK_DRAIN_TIME_MS 2
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070046
47static struct pcm_config pcm_config_fm = {
48 .channels = 2,
49 .rate = 48000,
50 .period_size = 256,
51 .period_count = 4,
52 .format = PCM_FORMAT_S16_LE,
53 .start_threshold = 0,
54 .stop_threshold = INT_MAX,
55 .avail_min = 0,
56};
57
58struct fm_module {
59 struct pcm *fm_pcm_rx;
60 struct pcm *fm_pcm_tx;
61 bool is_fm_running;
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053062 bool is_fm_muted;
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080063 float fm_volume;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053064 bool restart_fm;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053065 card_status_t card_status;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070066};
67
68static struct fm_module fmmod = {
69 .fm_pcm_rx = NULL,
70 .fm_pcm_tx = NULL,
71 .fm_volume = 0,
72 .is_fm_running = 0,
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053073 .is_fm_muted = 0,
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053074 .restart_fm = 0,
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053075 .card_status = CARD_STATUS_ONLINE,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070076};
77
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053078static int32_t fm_set_volume(struct audio_device *adev, float value, bool persist)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070079{
80 int32_t vol, ret = 0;
81 struct mixer_ctl *ctl;
Banajit Goswami88d6cc52014-04-10 17:59:02 -070082 const char *mixer_ctl_name = FM_RX_VOLUME;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070083
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080084 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070085 ALOGD("%s: (%f)\n", __func__, value);
86
87 if (value < 0.0) {
88 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
89 value = 0.0;
90 } else if (value > 1.0) {
91 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
92 value = 1.0;
93 }
94 vol = lrint((value * 0x2000) + 0.5);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +053095 if (persist)
96 fmmod.fm_volume = value;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070097
Dhananjay Kumareeb6b312016-01-25 19:45:19 +053098 if (fmmod.is_fm_muted == true && vol > 0) {
99 ALOGD("%s: fm is muted, applying '0' volume instead of '%d'.",
100 __func__, vol);
101 vol = 0;
102 }
103
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700104 if (!fmmod.is_fm_running) {
105 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
106 return -EIO;
107 }
108
109 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
110 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
111 if (!ctl) {
112 ALOGE("%s: Could not get ctl for mixer cmd - %s",
113 __func__, mixer_ctl_name);
114 return -EINVAL;
115 }
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800116 mixer_ctl_set_value(ctl, 0, vol);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800117 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700118 return ret;
119}
120
121static int32_t fm_stop(struct audio_device *adev)
122{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530123 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700124 struct audio_usecase *uc_info;
125
126 ALOGD("%s: enter", __func__);
127 fmmod.is_fm_running = false;
128
129 /* 1. Close the PCM devices */
130 if (fmmod.fm_pcm_rx) {
131 pcm_close(fmmod.fm_pcm_rx);
132 fmmod.fm_pcm_rx = NULL;
133 }
134 if (fmmod.fm_pcm_tx) {
135 pcm_close(fmmod.fm_pcm_tx);
136 fmmod.fm_pcm_tx = NULL;
137 }
138
139 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
140 if (uc_info == NULL) {
141 ALOGE("%s: Could not find the usecase (%d) in the list",
142 __func__, USECASE_VOICE_CALL);
143 return -EINVAL;
144 }
145
146 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700147 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700148
149 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700150 disable_snd_device(adev, uc_info->out_snd_device);
151 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700152
153 list_remove(&uc_info->list);
154 free(uc_info);
155
156 ALOGD("%s: exit: status(%d)", __func__, ret);
157 return ret;
158}
159
160static int32_t fm_start(struct audio_device *adev)
161{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530162 int32_t ret = 0;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700163 struct audio_usecase *uc_info;
164 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
165
166 ALOGD("%s: enter", __func__);
167
168 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700169
170 if (!uc_info)
171 return -ENOMEM;
172
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700173 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
174 uc_info->type = PCM_PLAYBACK;
175 uc_info->stream.out = adev->primary_output;
176 uc_info->devices = adev->primary_output->devices;
177 uc_info->in_snd_device = SND_DEVICE_NONE;
178 uc_info->out_snd_device = SND_DEVICE_NONE;
179
180 list_add_tail(&adev->usecase_list, &uc_info->list);
181
182 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
183
184 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
185 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
186
187 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
188 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
189 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
190 ret = -EIO;
191 goto exit;
192 }
193
194 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
195 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
196
197 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800198 __func__, adev->snd_card, pcm_dev_rx_id);
199 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
200 pcm_dev_rx_id,
201 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700202 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
203 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
204 ret = -EIO;
205 goto exit;
206 }
207
208 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800209 __func__, adev->snd_card, pcm_dev_tx_id);
210 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
211 pcm_dev_tx_id,
212 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700213 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
214 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
215 ret = -EIO;
216 goto exit;
217 }
218 pcm_start(fmmod.fm_pcm_rx);
219 pcm_start(fmmod.fm_pcm_tx);
220
221 fmmod.is_fm_running = true;
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530222 fm_set_volume(adev, fmmod.fm_volume, false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700223
224 ALOGD("%s: exit: status(%d)", __func__, ret);
225 return 0;
226
227exit:
228 fm_stop(adev);
229 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
230 return ret;
231}
232
233void audio_extn_fm_set_parameters(struct audio_device *adev,
234 struct str_parms *parms)
235{
236 int ret, val;
237 char value[32]={0};
238 float vol =0.0;
239
240 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530241 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
242 if (ret >= 0) {
243 char *snd_card_status = value+2;
244 if (strstr(snd_card_status, "OFFLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530245 fmmod.card_status = CARD_STATUS_OFFLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530246 }
247 else if (strstr(snd_card_status, "ONLINE")) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530248 fmmod.card_status = CARD_STATUS_ONLINE;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530249 }
250 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700251 if(fmmod.is_fm_running) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530252 if (fmmod.card_status == CARD_STATUS_OFFLINE) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530253 ALOGD("sound card is OFFLINE, stop FM");
254 fm_stop(adev);
255 fmmod.restart_fm = 1;
256 }
257
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700258 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
259 value, sizeof(value));
260 if (ret >= 0) {
261 val = atoi(value);
262 if(val > 0)
263 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
264 }
265 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530266 if (fmmod.restart_fm && (fmmod.card_status == CARD_STATUS_ONLINE)) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530267 ALOGD("sound card is ONLINE, restart FM");
268 fmmod.restart_fm = 0;
269 fm_start(adev);
270 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700271
272 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
273 value, sizeof(value));
274 if (ret >= 0) {
275 val = atoi(value);
276 ALOGD("%s: FM usecase", __func__);
277 if (val != 0) {
278 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700279 && fmmod.is_fm_running == false) {
280 adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700281 fm_start(adev);
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700282 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530283 && fmmod.is_fm_running == true) {
284 fm_set_volume(adev, 0, false);
285 usleep(FM_LOOPBACK_DRAIN_TIME_MS*1000);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700286 fm_stop(adev);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530287 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700288 }
289 }
290
291 memset(value, 0, sizeof(value));
292 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
293 value, sizeof(value));
294 if (ret >= 0) {
295 if (sscanf(value, "%f", &vol) != 1){
296 ALOGE("%s: error in retrieving fm volume", __func__);
297 ret = -EIO;
298 goto exit;
299 }
300 ALOGD("%s: set_fm_volume usecase", __func__);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530301 fm_set_volume(adev, vol, true);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700302 }
303
Dhananjay Kumareeb6b312016-01-25 19:45:19 +0530304 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_MUTE,
305 value, sizeof(value));
306 if (ret >= 0) {
307 if (value[0] == '1')
308 fmmod.is_fm_muted = true;
309 else
310 fmmod.is_fm_muted = false;
311 ALOGV("%s: set_fm_volume from param mute", __func__);
312 fm_set_volume(adev, fmmod.fm_volume, false);
313 }
314
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530315#ifdef RECORD_PLAY_CONCURRENCY
316 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_REC_PLAY_CONC,
317 value, sizeof(value));
318 if ((ret >= 0)
319 && (fmmod.is_fm_running == true)) {
320
321 if (!strncmp("true", value, sizeof("true")))
322 ALOGD("Record play concurrency ON Forcing FM device reroute");
323 else
324 ALOGD("Record play concurrency OFF Forcing FM device reroute");
325
326 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
Dhananjay Kumardf8aecf2015-10-01 13:38:37 +0530327 fm_set_volume(adev, fmmod.fm_volume, false);
Naresh Tanniru9e95c242014-12-08 16:49:22 +0530328 }
329#endif
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700330exit:
331 ALOGV("%s: exit", __func__);
332}
Mingming Yin12125e82015-10-26 20:40:36 -0700333#endif /* FM_POWER_OPT end */