Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 1 | /* |
Banajit Goswami | 88d6cc5 | 2014-04-10 17:59:02 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved. |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 3 | * 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 | |
| 34 | #ifdef FM_ENABLED |
| 35 | #define AUDIO_PARAMETER_KEY_HANDLE_FM "handle_fm" |
| 36 | #define AUDIO_PARAMETER_KEY_FM_VOLUME "fm_volume" |
| 37 | |
| 38 | static struct pcm_config pcm_config_fm = { |
| 39 | .channels = 2, |
| 40 | .rate = 48000, |
| 41 | .period_size = 256, |
| 42 | .period_count = 4, |
| 43 | .format = PCM_FORMAT_S16_LE, |
| 44 | .start_threshold = 0, |
| 45 | .stop_threshold = INT_MAX, |
| 46 | .avail_min = 0, |
| 47 | }; |
| 48 | |
| 49 | struct fm_module { |
| 50 | struct pcm *fm_pcm_rx; |
| 51 | struct pcm *fm_pcm_tx; |
| 52 | bool is_fm_running; |
Ravi Kumar Alamanda | cb06574 | 2013-11-20 18:31:58 -0800 | [diff] [blame] | 53 | float fm_volume; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static struct fm_module fmmod = { |
| 57 | .fm_pcm_rx = NULL, |
| 58 | .fm_pcm_tx = NULL, |
| 59 | .fm_volume = 0, |
| 60 | .is_fm_running = 0, |
| 61 | }; |
| 62 | |
| 63 | static int32_t fm_set_volume(struct audio_device *adev, float value) |
| 64 | { |
| 65 | int32_t vol, ret = 0; |
| 66 | struct mixer_ctl *ctl; |
Banajit Goswami | 88d6cc5 | 2014-04-10 17:59:02 -0700 | [diff] [blame] | 67 | const char *mixer_ctl_name = FM_RX_VOLUME; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 68 | |
Ravi Kumar Alamanda | cb06574 | 2013-11-20 18:31:58 -0800 | [diff] [blame] | 69 | ALOGV("%s: entry", __func__); |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 70 | ALOGD("%s: (%f)\n", __func__, value); |
| 71 | |
| 72 | if (value < 0.0) { |
| 73 | ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value); |
| 74 | value = 0.0; |
| 75 | } else if (value > 1.0) { |
| 76 | ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value); |
| 77 | value = 1.0; |
| 78 | } |
| 79 | vol = lrint((value * 0x2000) + 0.5); |
Ravi Kumar Alamanda | cb06574 | 2013-11-20 18:31:58 -0800 | [diff] [blame] | 80 | fmmod.fm_volume = value; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 81 | |
| 82 | if (!fmmod.is_fm_running) { |
| 83 | ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__); |
| 84 | return -EIO; |
| 85 | } |
| 86 | |
| 87 | ALOGD("%s: Setting FM volume to %d \n", __func__, vol); |
| 88 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 89 | if (!ctl) { |
| 90 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 91 | __func__, mixer_ctl_name); |
| 92 | return -EINVAL; |
| 93 | } |
Ravi Kumar Alamanda | cb06574 | 2013-11-20 18:31:58 -0800 | [diff] [blame] | 94 | mixer_ctl_set_value(ctl, 0, vol); |
Ravi Kumar Alamanda | cb06574 | 2013-11-20 18:31:58 -0800 | [diff] [blame] | 95 | ALOGV("%s: exit", __func__); |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static int32_t fm_stop(struct audio_device *adev) |
| 100 | { |
| 101 | int32_t i, ret = 0; |
| 102 | struct audio_usecase *uc_info; |
| 103 | |
| 104 | ALOGD("%s: enter", __func__); |
| 105 | fmmod.is_fm_running = false; |
| 106 | |
| 107 | /* 1. Close the PCM devices */ |
| 108 | if (fmmod.fm_pcm_rx) { |
| 109 | pcm_close(fmmod.fm_pcm_rx); |
| 110 | fmmod.fm_pcm_rx = NULL; |
| 111 | } |
| 112 | if (fmmod.fm_pcm_tx) { |
| 113 | pcm_close(fmmod.fm_pcm_tx); |
| 114 | fmmod.fm_pcm_tx = NULL; |
| 115 | } |
| 116 | |
| 117 | uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM); |
| 118 | if (uc_info == NULL) { |
| 119 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 120 | __func__, USECASE_VOICE_CALL); |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | /* 2. Get and set stream specific mixer controls */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 125 | disable_audio_route(adev, uc_info); |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 126 | |
| 127 | /* 3. Disable the rx and tx devices */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 128 | disable_snd_device(adev, uc_info->out_snd_device); |
| 129 | disable_snd_device(adev, uc_info->in_snd_device); |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 130 | |
| 131 | list_remove(&uc_info->list); |
| 132 | free(uc_info); |
| 133 | |
| 134 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | static int32_t fm_start(struct audio_device *adev) |
| 139 | { |
| 140 | int32_t i, ret = 0; |
| 141 | struct audio_usecase *uc_info; |
| 142 | int32_t pcm_dev_rx_id, pcm_dev_tx_id; |
| 143 | |
| 144 | ALOGD("%s: enter", __func__); |
| 145 | |
| 146 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 147 | uc_info->id = USECASE_AUDIO_PLAYBACK_FM; |
| 148 | uc_info->type = PCM_PLAYBACK; |
| 149 | uc_info->stream.out = adev->primary_output; |
| 150 | uc_info->devices = adev->primary_output->devices; |
| 151 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 152 | uc_info->out_snd_device = SND_DEVICE_NONE; |
| 153 | |
| 154 | list_add_tail(&adev->usecase_list, &uc_info->list); |
| 155 | |
| 156 | select_devices(adev, USECASE_AUDIO_PLAYBACK_FM); |
| 157 | |
| 158 | pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); |
| 159 | pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); |
| 160 | |
| 161 | if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) { |
| 162 | ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", |
| 163 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); |
| 164 | ret = -EIO; |
| 165 | goto exit; |
| 166 | } |
| 167 | |
| 168 | ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)", |
| 169 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); |
| 170 | |
| 171 | ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)", |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 172 | __func__, adev->snd_card, pcm_dev_rx_id); |
| 173 | fmmod.fm_pcm_rx = pcm_open(adev->snd_card, |
| 174 | pcm_dev_rx_id, |
| 175 | PCM_OUT, &pcm_config_fm); |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 176 | if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) { |
| 177 | ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx)); |
| 178 | ret = -EIO; |
| 179 | goto exit; |
| 180 | } |
| 181 | |
| 182 | ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)", |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 183 | __func__, adev->snd_card, pcm_dev_tx_id); |
| 184 | fmmod.fm_pcm_tx = pcm_open(adev->snd_card, |
| 185 | pcm_dev_tx_id, |
| 186 | PCM_IN, &pcm_config_fm); |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 187 | if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) { |
| 188 | ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx)); |
| 189 | ret = -EIO; |
| 190 | goto exit; |
| 191 | } |
| 192 | pcm_start(fmmod.fm_pcm_rx); |
| 193 | pcm_start(fmmod.fm_pcm_tx); |
| 194 | |
| 195 | fmmod.is_fm_running = true; |
| 196 | fm_set_volume(adev, fmmod.fm_volume); |
| 197 | |
| 198 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 199 | return 0; |
| 200 | |
| 201 | exit: |
| 202 | fm_stop(adev); |
| 203 | ALOGE("%s: Problem in FM start: status(%d)", __func__, ret); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | void audio_extn_fm_set_parameters(struct audio_device *adev, |
| 208 | struct str_parms *parms) |
| 209 | { |
| 210 | int ret, val; |
| 211 | char value[32]={0}; |
| 212 | float vol =0.0; |
| 213 | |
| 214 | ALOGV("%s: enter", __func__); |
| 215 | if(fmmod.is_fm_running) { |
| 216 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, |
| 217 | value, sizeof(value)); |
| 218 | if (ret >= 0) { |
| 219 | val = atoi(value); |
| 220 | if(val > 0) |
| 221 | select_devices(adev, USECASE_AUDIO_PLAYBACK_FM); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM, |
| 226 | value, sizeof(value)); |
| 227 | if (ret >= 0) { |
| 228 | val = atoi(value); |
| 229 | ALOGD("%s: FM usecase", __func__); |
| 230 | if (val != 0) { |
| 231 | if(val & AUDIO_DEVICE_OUT_FM |
Ravi Kumar Alamanda | 014b6b9 | 2014-04-22 15:19:13 -0700 | [diff] [blame] | 232 | && fmmod.is_fm_running == false) { |
| 233 | adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 234 | fm_start(adev); |
Ravi Kumar Alamanda | 014b6b9 | 2014-04-22 15:19:13 -0700 | [diff] [blame] | 235 | } else if (!(val & AUDIO_DEVICE_OUT_FM) |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 236 | && fmmod.is_fm_running == true) |
| 237 | fm_stop(adev); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | memset(value, 0, sizeof(value)); |
| 242 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME, |
| 243 | value, sizeof(value)); |
| 244 | if (ret >= 0) { |
| 245 | if (sscanf(value, "%f", &vol) != 1){ |
| 246 | ALOGE("%s: error in retrieving fm volume", __func__); |
| 247 | ret = -EIO; |
| 248 | goto exit; |
| 249 | } |
| 250 | ALOGD("%s: set_fm_volume usecase", __func__); |
| 251 | fm_set_volume(adev, vol); |
| 252 | } |
| 253 | |
| 254 | exit: |
| 255 | ALOGV("%s: exit", __func__); |
| 256 | } |
| 257 | #endif /* FM_ENABLED end */ |