Meng Wang | 43bbb87 | 2018-12-10 12:32:05 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Xiao Li | d8bb93c | 2020-01-07 12:59:05 +0800 | [diff] [blame] | 2 | /* Copyright (c) 2013-2014, 2017-2020 The Linux Foundation. All rights reserved. |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include <linux/init.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/time.h> |
| 8 | #include <linux/wait.h> |
| 9 | #include <linux/platform_device.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/dma-mapping.h> |
| 12 | #include <sound/core.h> |
| 13 | #include <sound/soc.h> |
| 14 | #include <sound/pcm.h> |
Laxminath Kasam | 605b42f | 2017-08-01 22:02:15 +0530 | [diff] [blame] | 15 | #include <dsp/q6afe-v2.h> |
| 16 | #include <dsp/q6voice.h> |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 17 | |
| 18 | #include "msm-pcm-q6-v2.h" |
| 19 | #include "msm-pcm-routing-v2.h" |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 20 | |
Meng Wang | ee084a0 | 2018-09-04 16:11:58 +0800 | [diff] [blame] | 21 | #define DRV_NAME "msm-pcm-dtmf-v2" |
| 22 | |
Jhansi Konathala | 29a4c3c | 2019-09-25 16:21:16 +0530 | [diff] [blame] | 23 | #define DTMF_MAX_DURATION 65535 |
| 24 | |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 25 | enum { |
| 26 | DTMF_IN_RX, |
| 27 | DTMF_IN_TX, |
| 28 | }; |
| 29 | |
| 30 | enum format { |
| 31 | FORMAT_S16_LE = 2 |
| 32 | }; |
| 33 | |
| 34 | struct dtmf_det_info { |
| 35 | char session[MAX_SESSION_NAME_LEN]; |
| 36 | uint8_t dir; |
| 37 | uint16_t high_freq; |
| 38 | uint16_t low_freq; |
| 39 | }; |
| 40 | |
| 41 | struct dtmf_buf_node { |
| 42 | struct list_head list; |
| 43 | struct dtmf_det_info dtmf_det_pkt; |
| 44 | }; |
| 45 | |
| 46 | enum dtmf_state { |
| 47 | DTMF_GEN_RX_STOPPED, |
| 48 | DTMF_GEN_RX_STARTED, |
| 49 | }; |
| 50 | |
| 51 | #define DTMF_MAX_Q_LEN 10 |
| 52 | #define DTMF_PKT_SIZE sizeof(struct dtmf_det_info) |
| 53 | |
| 54 | struct dtmf_drv_info { |
| 55 | enum dtmf_state state; |
| 56 | struct snd_pcm_substream *capture_substream; |
| 57 | |
| 58 | struct list_head out_queue; |
| 59 | struct list_head free_out_queue; |
| 60 | |
| 61 | wait_queue_head_t out_wait; |
| 62 | |
| 63 | struct mutex lock; |
| 64 | spinlock_t dsp_lock; |
| 65 | |
| 66 | uint8_t capture_start; |
| 67 | uint8_t capture_instance; |
| 68 | |
| 69 | unsigned int pcm_capture_size; |
| 70 | unsigned int pcm_capture_count; |
| 71 | unsigned int pcm_capture_irq_pos; |
| 72 | unsigned int pcm_capture_buf_pos; |
| 73 | }; |
| 74 | |
| 75 | static struct snd_pcm_hardware msm_pcm_hardware = { |
| 76 | .info = (SNDRV_PCM_INFO_MMAP | |
| 77 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 78 | SNDRV_PCM_INFO_MMAP_VALID | |
| 79 | SNDRV_PCM_INFO_INTERLEAVED), |
| 80 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 81 | .channels_min = 1, |
| 82 | .channels_max = 1, |
| 83 | .buffer_bytes_max = (sizeof(struct dtmf_buf_node) * DTMF_MAX_Q_LEN), |
| 84 | .period_bytes_min = DTMF_PKT_SIZE, |
| 85 | .period_bytes_max = DTMF_PKT_SIZE, |
| 86 | .periods_min = DTMF_MAX_Q_LEN, |
| 87 | .periods_max = DTMF_MAX_Q_LEN, |
| 88 | .fifo_size = 0, |
| 89 | }; |
| 90 | |
| 91 | static int msm_dtmf_rx_generate_put(struct snd_kcontrol *kcontrol, |
| 92 | struct snd_ctl_elem_value *ucontrol) |
| 93 | { |
| 94 | uint16_t low_freq = ucontrol->value.integer.value[0]; |
| 95 | uint16_t high_freq = ucontrol->value.integer.value[1]; |
| 96 | int64_t duration = ucontrol->value.integer.value[2]; |
| 97 | uint16_t gain = ucontrol->value.integer.value[3]; |
| 98 | |
Jhansi Konathala | 29a4c3c | 2019-09-25 16:21:16 +0530 | [diff] [blame] | 99 | pr_debug("%s: low_freq=%d high_freq=%d duration=%lld gain=%d\n", |
Xiao Li | d8bb93c | 2020-01-07 12:59:05 +0800 | [diff] [blame] | 100 | __func__, low_freq, high_freq, duration, gain); |
Jhansi Konathala | 29a4c3c | 2019-09-25 16:21:16 +0530 | [diff] [blame] | 101 | |
| 102 | if (duration == DTMF_MAX_DURATION) |
| 103 | duration = -1; |
| 104 | |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 105 | afe_dtmf_generate_rx(duration, high_freq, low_freq, gain); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int msm_dtmf_rx_generate_get(struct snd_kcontrol *kcontrol, |
| 110 | struct snd_ctl_elem_value *ucontrol) |
| 111 | { |
| 112 | pr_debug("%s:\n", __func__); |
| 113 | ucontrol->value.integer.value[0] = 0; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int msm_dtmf_detect_voice_rx_put(struct snd_kcontrol *kcontrol, |
| 118 | struct snd_ctl_elem_value *ucontrol) |
| 119 | { |
| 120 | int enable = ucontrol->value.integer.value[0]; |
| 121 | |
| 122 | pr_debug("%s: enable=%d\n", __func__, enable); |
| 123 | voc_enable_dtmf_rx_detection(voc_get_session_id(VOICE_SESSION_NAME), |
| 124 | enable); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int msm_dtmf_detect_voice_rx_get(struct snd_kcontrol *kcontrol, |
| 130 | struct snd_ctl_elem_value *ucontrol) |
| 131 | { |
| 132 | ucontrol->value.integer.value[0] = 0; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int msm_dtmf_detect_volte_rx_put(struct snd_kcontrol *kcontrol, |
| 137 | struct snd_ctl_elem_value *ucontrol) |
| 138 | { |
| 139 | int enable = ucontrol->value.integer.value[0]; |
| 140 | |
| 141 | pr_debug("%s: enable=%d\n", __func__, enable); |
| 142 | voc_enable_dtmf_rx_detection(voc_get_session_id(VOLTE_SESSION_NAME), |
| 143 | enable); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int msm_dtmf_detect_volte_rx_get(struct snd_kcontrol *kcontrol, |
| 149 | struct snd_ctl_elem_value *ucontrol) |
| 150 | { |
| 151 | ucontrol->value.integer.value[0] = 0; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static struct snd_kcontrol_new msm_dtmf_controls[] = { |
| 156 | SOC_SINGLE_MULTI_EXT("DTMF_Generate Rx Low High Duration Gain", |
| 157 | SND_SOC_NOPM, 0, 5000, 0, 4, |
| 158 | msm_dtmf_rx_generate_get, |
| 159 | msm_dtmf_rx_generate_put), |
| 160 | SOC_SINGLE_EXT("DTMF_Detect Rx Voice enable", SND_SOC_NOPM, 0, 1, 0, |
| 161 | msm_dtmf_detect_voice_rx_get, |
| 162 | msm_dtmf_detect_voice_rx_put), |
| 163 | SOC_SINGLE_EXT("DTMF_Detect Rx VoLTE enable", SND_SOC_NOPM, 0, 1, 0, |
| 164 | msm_dtmf_detect_volte_rx_get, |
| 165 | msm_dtmf_detect_volte_rx_put), |
| 166 | }; |
| 167 | |
Meng Wang | ee084a0 | 2018-09-04 16:11:58 +0800 | [diff] [blame] | 168 | static int msm_pcm_dtmf_probe(struct snd_soc_component *component) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 169 | { |
Meng Wang | ee084a0 | 2018-09-04 16:11:58 +0800 | [diff] [blame] | 170 | snd_soc_add_component_controls(component, msm_dtmf_controls, |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 171 | ARRAY_SIZE(msm_dtmf_controls)); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static void dtmf_rx_detected_cb(uint8_t *pkt, |
| 176 | char *session, |
| 177 | void *private_data) |
| 178 | { |
| 179 | struct dtmf_buf_node *buf_node = NULL; |
| 180 | struct vss_istream_evt_rx_dtmf_detected *dtmf_det_pkt = |
| 181 | (struct vss_istream_evt_rx_dtmf_detected *)pkt; |
| 182 | struct dtmf_drv_info *prtd = private_data; |
| 183 | unsigned long dsp_flags; |
| 184 | |
| 185 | pr_debug("%s\n", __func__); |
| 186 | if (prtd->capture_substream == NULL) |
| 187 | return; |
| 188 | |
| 189 | /* Copy dtmf detected info into out_queue. */ |
| 190 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 191 | /* discarding dtmf detection info till start is received */ |
| 192 | if (!list_empty(&prtd->free_out_queue) && prtd->capture_start) { |
| 193 | buf_node = list_first_entry(&prtd->free_out_queue, |
| 194 | struct dtmf_buf_node, list); |
| 195 | list_del(&buf_node->list); |
| 196 | buf_node->dtmf_det_pkt.high_freq = dtmf_det_pkt->high_freq; |
| 197 | buf_node->dtmf_det_pkt.low_freq = dtmf_det_pkt->low_freq; |
| 198 | if (session != NULL) |
| 199 | strlcpy(buf_node->dtmf_det_pkt.session, |
| 200 | session, MAX_SESSION_NAME_LEN); |
| 201 | |
| 202 | buf_node->dtmf_det_pkt.dir = DTMF_IN_RX; |
| 203 | pr_debug("high =%d, low=%d session=%s\n", |
| 204 | buf_node->dtmf_det_pkt.high_freq, |
| 205 | buf_node->dtmf_det_pkt.low_freq, |
| 206 | buf_node->dtmf_det_pkt.session); |
| 207 | list_add_tail(&buf_node->list, &prtd->out_queue); |
| 208 | prtd->pcm_capture_irq_pos += prtd->pcm_capture_count; |
| 209 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 210 | snd_pcm_period_elapsed(prtd->capture_substream); |
| 211 | } else { |
| 212 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 213 | pr_err("DTMF detection pkt in Rx dropped, no free node available\n"); |
| 214 | } |
| 215 | |
| 216 | wake_up(&prtd->out_wait); |
| 217 | } |
| 218 | |
| 219 | static int msm_pcm_capture_copy(struct snd_pcm_substream *substream, |
Meng Wang | ac147b7 | 2017-10-30 16:46:16 +0800 | [diff] [blame] | 220 | int channel, unsigned long hwoff, |
| 221 | void __user *buf, unsigned long fbytes) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 222 | { |
| 223 | int ret = 0; |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 224 | struct dtmf_buf_node *buf_node = NULL; |
| 225 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 226 | struct dtmf_drv_info *prtd = runtime->private_data; |
| 227 | unsigned long dsp_flags; |
| 228 | |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 229 | ret = wait_event_interruptible_timeout(prtd->out_wait, |
| 230 | (!list_empty(&prtd->out_queue)), |
| 231 | 1 * HZ); |
| 232 | |
| 233 | if (ret > 0) { |
Meng Wang | ac147b7 | 2017-10-30 16:46:16 +0800 | [diff] [blame] | 234 | if (fbytes <= DTMF_PKT_SIZE) { |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 235 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 236 | buf_node = list_first_entry(&prtd->out_queue, |
| 237 | struct dtmf_buf_node, list); |
| 238 | list_del(&buf_node->list); |
| 239 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 240 | ret = copy_to_user(buf, |
| 241 | &buf_node->dtmf_det_pkt, |
Meng Wang | ac147b7 | 2017-10-30 16:46:16 +0800 | [diff] [blame] | 242 | fbytes); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 243 | if (ret) { |
| 244 | pr_err("%s: Copy to user returned %d\n", |
| 245 | __func__, ret); |
| 246 | ret = -EFAULT; |
| 247 | } |
| 248 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 249 | list_add_tail(&buf_node->list, |
| 250 | &prtd->free_out_queue); |
| 251 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 252 | |
| 253 | } else { |
Meng Wang | ac147b7 | 2017-10-30 16:46:16 +0800 | [diff] [blame] | 254 | pr_err("%s: Read count %lu > DTMF_PKT_SIZE\n", |
| 255 | __func__, fbytes); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 256 | ret = -ENOMEM; |
| 257 | } |
| 258 | } else if (ret == 0) { |
| 259 | pr_err("%s: No UL data available\n", __func__); |
| 260 | ret = -ETIMEDOUT; |
| 261 | } else { |
| 262 | pr_err("%s: Read was interrupted\n", __func__); |
| 263 | ret = -ERESTARTSYS; |
| 264 | } |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static int msm_pcm_copy(struct snd_pcm_substream *substream, int a, |
Meng Wang | ac147b7 | 2017-10-30 16:46:16 +0800 | [diff] [blame] | 269 | unsigned long hwoff, void __user *buf, unsigned long fbytes) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 270 | { |
| 271 | int ret = 0; |
| 272 | |
| 273 | pr_debug("%s() DTMF\n", __func__); |
| 274 | |
| 275 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
Meng Wang | ac147b7 | 2017-10-30 16:46:16 +0800 | [diff] [blame] | 276 | ret = msm_pcm_capture_copy(substream, a, hwoff, buf, fbytes); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 277 | |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int msm_pcm_open(struct snd_pcm_substream *substream) |
| 282 | { |
| 283 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 284 | struct dtmf_drv_info *prtd = NULL; |
| 285 | int ret = 0; |
| 286 | |
| 287 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 288 | prtd = kzalloc(sizeof(struct dtmf_drv_info), GFP_KERNEL); |
| 289 | |
| 290 | if (prtd == NULL) { |
| 291 | ret = -ENOMEM; |
| 292 | goto done; |
| 293 | } |
| 294 | |
| 295 | mutex_init(&prtd->lock); |
| 296 | spin_lock_init(&prtd->dsp_lock); |
| 297 | init_waitqueue_head(&prtd->out_wait); |
| 298 | INIT_LIST_HEAD(&prtd->out_queue); |
| 299 | INIT_LIST_HEAD(&prtd->free_out_queue); |
| 300 | |
| 301 | runtime->hw = msm_pcm_hardware; |
| 302 | |
| 303 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 304 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 305 | if (ret < 0) |
| 306 | pr_info("snd_pcm_hw_constraint_integer failed\n"); |
| 307 | |
| 308 | prtd->capture_substream = substream; |
| 309 | prtd->capture_instance++; |
| 310 | runtime->private_data = prtd; |
| 311 | } |
| 312 | |
| 313 | done: |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | static int msm_pcm_close(struct snd_pcm_substream *substream) |
| 318 | { |
| 319 | int ret = 0; |
| 320 | struct list_head *ptr = NULL; |
| 321 | struct list_head *next = NULL; |
| 322 | struct dtmf_buf_node *buf_node = NULL; |
| 323 | struct snd_dma_buffer *c_dma_buf; |
| 324 | struct snd_pcm_substream *c_substream; |
| 325 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 326 | struct dtmf_drv_info *prtd = runtime->private_data; |
| 327 | unsigned long dsp_flags; |
| 328 | |
| 329 | pr_debug("%s() DTMF\n", __func__); |
| 330 | |
| 331 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 332 | mutex_lock(&prtd->lock); |
| 333 | wake_up(&prtd->out_wait); |
| 334 | |
| 335 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 336 | prtd->capture_instance--; |
| 337 | |
| 338 | if (!prtd->capture_instance) { |
| 339 | if (prtd->state == DTMF_GEN_RX_STARTED) { |
| 340 | prtd->state = DTMF_GEN_RX_STOPPED; |
| 341 | voc_disable_dtmf_det_on_active_sessions(); |
| 342 | voc_register_dtmf_rx_detection_cb(NULL, NULL); |
| 343 | } |
| 344 | /* release all buffer */ |
| 345 | /* release out_queue and free_out_queue */ |
| 346 | pr_debug("release all buffer\n"); |
| 347 | c_substream = prtd->capture_substream; |
| 348 | if (c_substream == NULL) { |
| 349 | pr_debug("c_substream is NULL\n"); |
| 350 | mutex_unlock(&prtd->lock); |
| 351 | return -EINVAL; |
| 352 | } |
| 353 | |
| 354 | c_dma_buf = &c_substream->dma_buffer; |
| 355 | if (c_dma_buf == NULL) { |
| 356 | pr_debug("c_dma_buf is NULL.\n"); |
| 357 | mutex_unlock(&prtd->lock); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | if (c_dma_buf->area != NULL) { |
| 362 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 363 | list_for_each_safe(ptr, next, |
| 364 | &prtd->out_queue) { |
| 365 | buf_node = list_entry(ptr, |
| 366 | struct dtmf_buf_node, list); |
| 367 | list_del(&buf_node->list); |
| 368 | } |
| 369 | |
| 370 | list_for_each_safe(ptr, next, |
| 371 | &prtd->free_out_queue) { |
| 372 | buf_node = list_entry(ptr, |
| 373 | struct dtmf_buf_node, list); |
| 374 | list_del(&buf_node->list); |
| 375 | } |
| 376 | |
| 377 | spin_unlock_irqrestore(&prtd->dsp_lock, |
| 378 | dsp_flags); |
| 379 | dma_free_coherent(c_substream->pcm->card->dev, |
| 380 | runtime->hw.buffer_bytes_max, |
| 381 | c_dma_buf->area, |
| 382 | c_dma_buf->addr); |
| 383 | c_dma_buf->area = NULL; |
| 384 | } |
| 385 | } |
| 386 | prtd->capture_substream = NULL; |
| 387 | mutex_unlock(&prtd->lock); |
| 388 | } |
| 389 | |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | static int msm_pcm_hw_params(struct snd_pcm_substream *substream, |
| 394 | struct snd_pcm_hw_params *params) |
| 395 | { |
| 396 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 397 | struct dtmf_drv_info *prtd = runtime->private_data; |
| 398 | struct snd_dma_buffer *dma_buf = &substream->dma_buffer; |
| 399 | struct dtmf_buf_node *buf_node = NULL; |
| 400 | int i = 0, offset = 0; |
| 401 | int ret = 0; |
| 402 | |
| 403 | pr_debug("%s: DTMF\n", __func__); |
| 404 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 405 | mutex_lock(&prtd->lock); |
| 406 | dma_buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 407 | dma_buf->dev.dev = substream->pcm->card->dev; |
| 408 | dma_buf->private_data = NULL; |
| 409 | |
| 410 | dma_buf->area = dma_alloc_coherent(substream->pcm->card->dev, |
| 411 | runtime->hw.buffer_bytes_max, |
| 412 | &dma_buf->addr, GFP_KERNEL); |
| 413 | if (!dma_buf->area) { |
| 414 | pr_err("%s:MSM DTMF dma_alloc failed\n", __func__); |
| 415 | mutex_unlock(&prtd->lock); |
| 416 | return -ENOMEM; |
| 417 | } |
| 418 | |
| 419 | dma_buf->bytes = runtime->hw.buffer_bytes_max; |
| 420 | memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max); |
| 421 | |
| 422 | for (i = 0; i < DTMF_MAX_Q_LEN; i++) { |
| 423 | pr_debug("node =%d\n", i); |
| 424 | buf_node = (void *) dma_buf->area + offset; |
| 425 | list_add_tail(&buf_node->list, |
| 426 | &prtd->free_out_queue); |
| 427 | offset = offset + sizeof(struct dtmf_buf_node); |
| 428 | } |
| 429 | |
| 430 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 431 | mutex_unlock(&prtd->lock); |
| 432 | } |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream) |
| 438 | { |
| 439 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 440 | struct dtmf_drv_info *prtd = runtime->private_data; |
| 441 | |
| 442 | pr_debug("%s: DTMF\n", __func__); |
| 443 | prtd->pcm_capture_size = snd_pcm_lib_buffer_bytes(substream); |
| 444 | prtd->pcm_capture_count = snd_pcm_lib_period_bytes(substream); |
| 445 | prtd->pcm_capture_irq_pos = 0; |
| 446 | prtd->pcm_capture_buf_pos = 0; |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int msm_pcm_prepare(struct snd_pcm_substream *substream) |
| 451 | { |
| 452 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 453 | struct dtmf_drv_info *prtd = runtime->private_data; |
| 454 | |
| 455 | pr_debug("%s: DTMF\n", __func__); |
| 456 | |
| 457 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 458 | mutex_lock(&prtd->lock); |
| 459 | |
| 460 | msm_pcm_capture_prepare(substream); |
| 461 | |
| 462 | if (runtime->format != FORMAT_S16_LE) { |
| 463 | pr_err("format:%u doesn't match %d\n", |
| 464 | (uint32_t)runtime->format, FORMAT_S16_LE); |
| 465 | mutex_unlock(&prtd->lock); |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | |
| 469 | if (prtd->capture_instance && |
| 470 | (prtd->state != DTMF_GEN_RX_STARTED)) { |
| 471 | voc_register_dtmf_rx_detection_cb(dtmf_rx_detected_cb, |
| 472 | prtd); |
| 473 | prtd->state = DTMF_GEN_RX_STARTED; |
| 474 | } |
| 475 | mutex_unlock(&prtd->lock); |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 482 | { |
| 483 | int ret = 0; |
| 484 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 485 | struct dtmf_drv_info *prtd = runtime->private_data; |
| 486 | |
| 487 | switch (cmd) { |
| 488 | case SNDRV_PCM_TRIGGER_START: |
| 489 | case SNDRV_PCM_TRIGGER_RESUME: |
| 490 | pr_debug("%s: Trigger start\n", __func__); |
| 491 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 492 | prtd->capture_start = 1; |
| 493 | break; |
| 494 | case SNDRV_PCM_TRIGGER_STOP: |
| 495 | pr_debug("SNDRV_PCM_TRIGGER_STOP\n"); |
| 496 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 497 | prtd->capture_start = 0; |
| 498 | break; |
| 499 | default: |
| 500 | ret = -EINVAL; |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream) |
| 508 | { |
| 509 | snd_pcm_uframes_t ret = 0; |
| 510 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 511 | struct dtmf_drv_info *prtd = runtime->private_data; |
| 512 | |
| 513 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 514 | if (prtd->pcm_capture_irq_pos >= prtd->pcm_capture_size) |
| 515 | prtd->pcm_capture_irq_pos = 0; |
| 516 | ret = bytes_to_frames(runtime, (prtd->pcm_capture_irq_pos)); |
| 517 | } |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | static const struct snd_pcm_ops msm_pcm_ops = { |
| 523 | .open = msm_pcm_open, |
Meng Wang | ac147b7 | 2017-10-30 16:46:16 +0800 | [diff] [blame] | 524 | .copy_user = msm_pcm_copy, |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 525 | .hw_params = msm_pcm_hw_params, |
| 526 | .close = msm_pcm_close, |
| 527 | .prepare = msm_pcm_prepare, |
| 528 | .trigger = msm_pcm_trigger, |
| 529 | .pointer = msm_pcm_pointer, |
| 530 | }; |
| 531 | |
| 532 | static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd) |
| 533 | { |
| 534 | struct snd_card *card = rtd->card->snd_card; |
| 535 | int ret = 0; |
| 536 | |
| 537 | if (!card->dev->coherent_dma_mask) |
| 538 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
| 539 | return ret; |
| 540 | } |
| 541 | |
Meng Wang | ee084a0 | 2018-09-04 16:11:58 +0800 | [diff] [blame] | 542 | static struct snd_soc_component_driver msm_soc_component = { |
| 543 | .name = DRV_NAME, |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 544 | .ops = &msm_pcm_ops, |
| 545 | .pcm_new = msm_asoc_pcm_new, |
| 546 | .probe = msm_pcm_dtmf_probe, |
| 547 | }; |
| 548 | |
| 549 | static int msm_pcm_probe(struct platform_device *pdev) |
| 550 | { |
| 551 | pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev)); |
| 552 | |
Meng Wang | ee084a0 | 2018-09-04 16:11:58 +0800 | [diff] [blame] | 553 | return snd_soc_register_component(&pdev->dev, |
| 554 | &msm_soc_component, NULL, 0); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static int msm_pcm_remove(struct platform_device *pdev) |
| 558 | { |
Meng Wang | ee084a0 | 2018-09-04 16:11:58 +0800 | [diff] [blame] | 559 | snd_soc_unregister_component(&pdev->dev); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static const struct of_device_id msm_pcm_dtmf_dt_match[] = { |
| 564 | {.compatible = "qcom,msm-pcm-dtmf"}, |
| 565 | {} |
| 566 | }; |
| 567 | |
| 568 | MODULE_DEVICE_TABLE(of, msm_pcm_dtmf_dt_match); |
| 569 | |
| 570 | |
| 571 | static struct platform_driver msm_pcm_driver = { |
| 572 | .driver = { |
| 573 | .name = "msm-pcm-dtmf", |
| 574 | .owner = THIS_MODULE, |
| 575 | .of_match_table = msm_pcm_dtmf_dt_match, |
Xiaojun Sang | 53cd13a | 2018-06-29 15:14:37 +0800 | [diff] [blame] | 576 | .suppress_bind_attrs = true, |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 577 | }, |
| 578 | .probe = msm_pcm_probe, |
| 579 | .remove = msm_pcm_remove, |
| 580 | }; |
| 581 | |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 582 | int __init msm_pcm_dtmf_init(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 583 | { |
| 584 | return platform_driver_register(&msm_pcm_driver); |
| 585 | } |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 586 | |
Asish Bhattacharya | 5faacb3 | 2017-12-04 17:23:15 +0530 | [diff] [blame] | 587 | void msm_pcm_dtmf_exit(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 588 | { |
| 589 | platform_driver_unregister(&msm_pcm_driver); |
| 590 | } |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 591 | |
| 592 | MODULE_DESCRIPTION("DTMF platform driver"); |
| 593 | MODULE_LICENSE("GPL v2"); |