0001
0002
0003
0004 #include <linux/init.h>
0005 #include <linux/err.h>
0006 #include <linux/module.h>
0007 #include <linux/platform_device.h>
0008 #include <linux/slab.h>
0009 #include <sound/soc.h>
0010 #include <sound/soc-dapm.h>
0011 #include <sound/pcm.h>
0012 #include <asm/dma.h>
0013 #include <linux/dma-mapping.h>
0014 #include <linux/of_device.h>
0015 #include <sound/pcm_params.h>
0016 #include "q6apm.h"
0017
0018 #define DRV_NAME "q6apm-dai"
0019
0020 #define PLAYBACK_MIN_NUM_PERIODS 2
0021 #define PLAYBACK_MAX_NUM_PERIODS 8
0022 #define PLAYBACK_MAX_PERIOD_SIZE 65536
0023 #define PLAYBACK_MIN_PERIOD_SIZE 128
0024 #define CAPTURE_MIN_NUM_PERIODS 2
0025 #define CAPTURE_MAX_NUM_PERIODS 8
0026 #define CAPTURE_MAX_PERIOD_SIZE 4096
0027 #define CAPTURE_MIN_PERIOD_SIZE 320
0028 #define BUFFER_BYTES_MAX (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE)
0029 #define BUFFER_BYTES_MIN (PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE)
0030 #define SID_MASK_DEFAULT 0xF
0031
0032 enum stream_state {
0033 Q6APM_STREAM_IDLE = 0,
0034 Q6APM_STREAM_STOPPED,
0035 Q6APM_STREAM_RUNNING,
0036 };
0037
0038 struct q6apm_dai_rtd {
0039 struct snd_pcm_substream *substream;
0040 struct snd_compr_stream *cstream;
0041 struct snd_compr_params codec_param;
0042 struct snd_dma_buffer dma_buffer;
0043 phys_addr_t phys;
0044 unsigned int pcm_size;
0045 unsigned int pcm_count;
0046 unsigned int pos;
0047 unsigned int periods;
0048 unsigned int bytes_sent;
0049 unsigned int bytes_received;
0050 unsigned int copied_total;
0051 uint16_t bits_per_sample;
0052 uint16_t source;
0053 uint16_t session_id;
0054 enum stream_state state;
0055 struct q6apm_graph *graph;
0056 };
0057
0058 struct q6apm_dai_data {
0059 long long sid;
0060 };
0061
0062 static struct snd_pcm_hardware q6apm_dai_hardware_capture = {
0063 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
0064 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
0065 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
0066 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
0067 .rates = SNDRV_PCM_RATE_8000_48000,
0068 .rate_min = 8000,
0069 .rate_max = 48000,
0070 .channels_min = 2,
0071 .channels_max = 4,
0072 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
0073 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
0074 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
0075 .periods_min = CAPTURE_MIN_NUM_PERIODS,
0076 .periods_max = CAPTURE_MAX_NUM_PERIODS,
0077 .fifo_size = 0,
0078 };
0079
0080 static struct snd_pcm_hardware q6apm_dai_hardware_playback = {
0081 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
0082 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
0083 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
0084 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
0085 .rates = SNDRV_PCM_RATE_8000_192000,
0086 .rate_min = 8000,
0087 .rate_max = 192000,
0088 .channels_min = 2,
0089 .channels_max = 8,
0090 .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE),
0091 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
0092 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
0093 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
0094 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
0095 .fifo_size = 0,
0096 };
0097
0098 static void event_handler(uint32_t opcode, uint32_t token, uint32_t *payload, void *priv)
0099 {
0100 struct q6apm_dai_rtd *prtd = priv;
0101 struct snd_pcm_substream *substream = prtd->substream;
0102
0103 switch (opcode) {
0104 case APM_CLIENT_EVENT_CMD_EOS_DONE:
0105 prtd->state = Q6APM_STREAM_STOPPED;
0106 break;
0107 case APM_CLIENT_EVENT_DATA_WRITE_DONE:
0108 prtd->pos += prtd->pcm_count;
0109 snd_pcm_period_elapsed(substream);
0110 if (prtd->state == Q6APM_STREAM_RUNNING)
0111 q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
0112
0113 break;
0114 case APM_CLIENT_EVENT_DATA_READ_DONE:
0115 prtd->pos += prtd->pcm_count;
0116 snd_pcm_period_elapsed(substream);
0117 if (prtd->state == Q6APM_STREAM_RUNNING)
0118 q6apm_read(prtd->graph);
0119
0120 break;
0121 default:
0122 break;
0123 }
0124 }
0125
0126 static int q6apm_dai_prepare(struct snd_soc_component *component,
0127 struct snd_pcm_substream *substream)
0128 {
0129 struct snd_pcm_runtime *runtime = substream->runtime;
0130 struct q6apm_dai_rtd *prtd = runtime->private_data;
0131 struct audioreach_module_config cfg;
0132 struct device *dev = component->dev;
0133 struct q6apm_dai_data *pdata;
0134 int ret;
0135
0136 pdata = snd_soc_component_get_drvdata(component);
0137 if (!pdata)
0138 return -EINVAL;
0139
0140 if (!prtd || !prtd->graph) {
0141 dev_err(dev, "%s: private data null or audio client freed\n", __func__);
0142 return -EINVAL;
0143 }
0144
0145 cfg.direction = substream->stream;
0146 cfg.sample_rate = runtime->rate;
0147 cfg.num_channels = runtime->channels;
0148 cfg.bit_width = prtd->bits_per_sample;
0149
0150 if (prtd->state) {
0151
0152 q6apm_graph_stop(prtd->graph);
0153 q6apm_unmap_memory_regions(prtd->graph, substream->stream);
0154 }
0155
0156 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
0157 prtd->pos = 0;
0158
0159 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
0160 if (ret < 0) {
0161 dev_err(dev, "%s: q6apm_open_write failed\n", __func__);
0162 return ret;
0163 }
0164
0165 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
0166 if (ret < 0)
0167 dev_err(dev, "%s: CMD Format block failed\n", __func__);
0168
0169 ret = q6apm_map_memory_regions(prtd->graph, substream->stream, prtd->phys,
0170 (prtd->pcm_size / prtd->periods), prtd->periods);
0171
0172 if (ret < 0) {
0173 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret);
0174 return -ENOMEM;
0175 }
0176
0177 ret = q6apm_graph_prepare(prtd->graph);
0178 if (ret) {
0179 dev_err(dev, "Failed to prepare Graph %d\n", ret);
0180 return ret;
0181 }
0182
0183 ret = q6apm_graph_start(prtd->graph);
0184 if (ret) {
0185 dev_err(dev, "Failed to Start Graph %d\n", ret);
0186 return ret;
0187 }
0188
0189 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
0190 int i;
0191
0192 for (i = 0; i < runtime->periods; i++)
0193 q6apm_read(prtd->graph);
0194
0195 }
0196
0197
0198 prtd->state = Q6APM_STREAM_RUNNING;
0199
0200 return 0;
0201 }
0202
0203 static int q6apm_dai_trigger(struct snd_soc_component *component,
0204 struct snd_pcm_substream *substream, int cmd)
0205 {
0206 struct snd_pcm_runtime *runtime = substream->runtime;
0207 struct q6apm_dai_rtd *prtd = runtime->private_data;
0208 int ret = 0;
0209
0210 switch (cmd) {
0211 case SNDRV_PCM_TRIGGER_START:
0212 case SNDRV_PCM_TRIGGER_RESUME:
0213 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0214
0215 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0216 ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
0217 break;
0218 case SNDRV_PCM_TRIGGER_STOP:
0219
0220 prtd->state = Q6APM_STREAM_STOPPED;
0221 break;
0222 case SNDRV_PCM_TRIGGER_SUSPEND:
0223 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0224 break;
0225 default:
0226 ret = -EINVAL;
0227 break;
0228 }
0229
0230 return ret;
0231 }
0232
0233 static int q6apm_dai_open(struct snd_soc_component *component,
0234 struct snd_pcm_substream *substream)
0235 {
0236 struct snd_pcm_runtime *runtime = substream->runtime;
0237 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
0238 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_prtd, 0);
0239 struct device *dev = component->dev;
0240 struct q6apm_dai_data *pdata;
0241 struct q6apm_dai_rtd *prtd;
0242 int graph_id, ret;
0243
0244 graph_id = cpu_dai->driver->id;
0245
0246 pdata = snd_soc_component_get_drvdata(component);
0247 if (!pdata) {
0248 dev_err(dev, "Drv data not found ..\n");
0249 return -EINVAL;
0250 }
0251
0252 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
0253 if (prtd == NULL)
0254 return -ENOMEM;
0255
0256 prtd->substream = substream;
0257 prtd->graph = q6apm_graph_open(dev, (q6apm_cb)event_handler, prtd, graph_id);
0258 if (IS_ERR(prtd->graph)) {
0259 dev_err(dev, "%s: Could not allocate memory\n", __func__);
0260 ret = PTR_ERR(prtd->graph);
0261 goto err;
0262 }
0263
0264 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0265 runtime->hw = q6apm_dai_hardware_playback;
0266 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
0267 runtime->hw = q6apm_dai_hardware_capture;
0268
0269
0270 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
0271 if (ret < 0) {
0272 dev_err(dev, "snd_pcm_hw_constraint_integer failed\n");
0273 goto err;
0274 }
0275
0276 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
0277 ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
0278 BUFFER_BYTES_MIN, BUFFER_BYTES_MAX);
0279 if (ret < 0) {
0280 dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret);
0281 goto err;
0282 }
0283 }
0284
0285 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
0286 if (ret < 0) {
0287 dev_err(dev, "constraint for period bytes step ret = %d\n", ret);
0288 goto err;
0289 }
0290
0291 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
0292 if (ret < 0) {
0293 dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret);
0294 goto err;
0295 }
0296
0297 runtime->private_data = prtd;
0298 runtime->dma_bytes = BUFFER_BYTES_MAX;
0299 if (pdata->sid < 0)
0300 prtd->phys = substream->dma_buffer.addr;
0301 else
0302 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
0303
0304 return 0;
0305 err:
0306 kfree(prtd);
0307
0308 return ret;
0309 }
0310
0311 static int q6apm_dai_close(struct snd_soc_component *component,
0312 struct snd_pcm_substream *substream)
0313 {
0314 struct snd_pcm_runtime *runtime = substream->runtime;
0315 struct q6apm_dai_rtd *prtd = runtime->private_data;
0316
0317 if (prtd->state) {
0318 q6apm_graph_stop(prtd->graph);
0319 q6apm_unmap_memory_regions(prtd->graph, substream->stream);
0320 }
0321
0322 q6apm_graph_close(prtd->graph);
0323 prtd->graph = NULL;
0324 kfree(prtd);
0325 runtime->private_data = NULL;
0326
0327 return 0;
0328 }
0329
0330 static snd_pcm_uframes_t q6apm_dai_pointer(struct snd_soc_component *component,
0331 struct snd_pcm_substream *substream)
0332 {
0333 struct snd_pcm_runtime *runtime = substream->runtime;
0334 struct q6apm_dai_rtd *prtd = runtime->private_data;
0335
0336 if (prtd->pos == prtd->pcm_size)
0337 prtd->pos = 0;
0338
0339 return bytes_to_frames(runtime, prtd->pos);
0340 }
0341
0342 static int q6apm_dai_hw_params(struct snd_soc_component *component,
0343 struct snd_pcm_substream *substream,
0344 struct snd_pcm_hw_params *params)
0345 {
0346 struct snd_pcm_runtime *runtime = substream->runtime;
0347 struct q6apm_dai_rtd *prtd = runtime->private_data;
0348
0349 prtd->pcm_size = params_buffer_bytes(params);
0350 prtd->periods = params_periods(params);
0351
0352 switch (params_format(params)) {
0353 case SNDRV_PCM_FORMAT_S16_LE:
0354 prtd->bits_per_sample = 16;
0355 break;
0356 case SNDRV_PCM_FORMAT_S24_LE:
0357 prtd->bits_per_sample = 24;
0358 break;
0359 default:
0360 return -EINVAL;
0361 }
0362
0363 return 0;
0364 }
0365
0366 static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
0367 {
0368 int size = BUFFER_BYTES_MAX;
0369
0370 return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, component->dev, size);
0371 }
0372
0373 static const struct snd_soc_component_driver q6apm_fe_dai_component = {
0374 .name = DRV_NAME,
0375 .open = q6apm_dai_open,
0376 .close = q6apm_dai_close,
0377 .prepare = q6apm_dai_prepare,
0378 .pcm_construct = q6apm_dai_pcm_new,
0379 .hw_params = q6apm_dai_hw_params,
0380 .pointer = q6apm_dai_pointer,
0381 .trigger = q6apm_dai_trigger,
0382 };
0383
0384 static int q6apm_dai_probe(struct platform_device *pdev)
0385 {
0386 struct device *dev = &pdev->dev;
0387 struct device_node *node = dev->of_node;
0388 struct q6apm_dai_data *pdata;
0389 struct of_phandle_args args;
0390 int rc;
0391
0392 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0393 if (!pdata)
0394 return -ENOMEM;
0395
0396 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
0397 if (rc < 0)
0398 pdata->sid = -1;
0399 else
0400 pdata->sid = args.args[0] & SID_MASK_DEFAULT;
0401
0402 dev_set_drvdata(dev, pdata);
0403
0404 return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0);
0405 }
0406
0407 #ifdef CONFIG_OF
0408 static const struct of_device_id q6apm_dai_device_id[] = {
0409 { .compatible = "qcom,q6apm-dais" },
0410 {},
0411 };
0412 MODULE_DEVICE_TABLE(of, q6apm_dai_device_id);
0413 #endif
0414
0415 static struct platform_driver q6apm_dai_platform_driver = {
0416 .driver = {
0417 .name = "q6apm-dai",
0418 .of_match_table = of_match_ptr(q6apm_dai_device_id),
0419 },
0420 .probe = q6apm_dai_probe,
0421 };
0422 module_platform_driver(q6apm_dai_platform_driver);
0423
0424 MODULE_DESCRIPTION("Q6APM dai driver");
0425 MODULE_LICENSE("GPL");