blob: e8bdf13fdc6c8d0f6c464b69302dacc6f48b28a0 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/*
2 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/bitops.h>
18#include <linux/slimbus/slimbus.h>
19#include <sound/soc.h>
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053022#include "msm-slim-dma.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053023
24#define SLIM_DEV_NAME "msm-dai-slim"
25
26#define SLIM_DAI_RATES (SNDRV_PCM_RATE_48000 | \
27 SNDRV_PCM_RATE_8000 | \
28 SNDRV_PCM_RATE_16000 | \
29 SNDRV_PCM_RATE_96000 | \
30 SNDRV_PCM_RATE_192000 | \
31 SNDRV_PCM_RATE_384000)
32
33#define SLIM_DAI_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
34 SNDRV_PCM_FMTBIT_S24_LE | \
35 SNDRV_PCM_FMTBIT_S32_LE)
36
37#define DAI_STATE_INITIALIZED (0x01 << 0)
38#define DAI_STATE_PREPARED (0x01 << 1)
39#define DAI_STATE_RUNNING (0x01 << 2)
40
41#define SET_DAI_STATE(status, state) \
42 (status |= state)
43
44#define CLR_DAI_STATE(status, state) \
45 (status = status & (~state))
46
47enum {
48 MSM_DAI_SLIM0 = 0,
49 NUM_SLIM_DAIS,
50};
51
52struct msm_slim_dai_data {
53 unsigned int dai_id;
54 u16 *chan_h;
55 u16 *sh_ch;
56 u16 grph;
57 u32 rate;
58 u16 bits;
59 u16 ch_cnt;
60 u8 status;
61 struct snd_soc_dai_driver *dai_drv;
62 struct msm_slim_dma_data dma_data;
63 struct slim_port_cfg port_cfg;
64};
65
66struct msm_dai_slim_drv_data {
67 struct slim_device *sdev;
68 u16 num_dais;
69 struct msm_slim_dai_data slim_dai_data[NUM_SLIM_DAIS];
70};
71
72struct msm_slim_dai_data *msm_slim_get_dai_data(
73 struct msm_dai_slim_drv_data *drv_data,
74 struct snd_soc_dai *dai)
75{
76 struct msm_slim_dai_data *dai_data_t;
77 int i;
78
79 for (i = 0; i < drv_data->num_dais; i++) {
80 dai_data_t = &drv_data->slim_dai_data[i];
81 if (dai_data_t->dai_id == dai->id)
82 return dai_data_t;
83 }
84
85 dev_err(dai->dev,
86 "%s: no dai data found for dai_id %d\n",
87 __func__, dai->id);
88 return NULL;
89}
90
91static int msm_dai_slim_ch_ctl(struct msm_slim_dma_data *dma_data,
92 struct snd_soc_dai *dai, bool enable)
93{
94 struct slim_device *sdev;
95 struct msm_dai_slim_drv_data *drv_data;
96 struct msm_slim_dai_data *dai_data;
97 int rc, rc1, i;
98
99 if (!dma_data || !dma_data->sdev) {
100 pr_err("%s: Invalid %s\n", __func__,
101 (!dma_data) ? "dma_data" : "slim_device");
102 return -EINVAL;
103 }
104
105 sdev = dma_data->sdev;
106 drv_data = dev_get_drvdata(&sdev->dev);
107 dai_data = msm_slim_get_dai_data(drv_data, dai);
108
109 if (!dai_data) {
110 dev_err(dai->dev,
111 "%s: Invalid dai_data for dai_id %d\n",
112 __func__, dai->id);
113 return -EINVAL;
114 }
115
116 dev_dbg(&sdev->dev,
117 "%s: enable = %s, rate = %u\n", __func__,
118 enable ? "true" : "false",
119 dai_data->rate);
120
121 if (enable) {
122 if (!(dai_data->status & DAI_STATE_PREPARED)) {
123 dev_err(&sdev->dev,
124 "%s: dai id (%d) has invalid state 0x%x\n",
125 __func__, dai->id, dai_data->status);
126 return -EINVAL;
127 }
128
129 rc = slim_alloc_mgrports(sdev,
130 SLIM_REQ_DEFAULT, dai_data->ch_cnt,
131 &(dma_data->ph),
132 sizeof(dma_data->ph));
133 if (rc < 0) {
134 dev_err(&sdev->dev,
135 "%s:alloc mgrport failed rc %d\n",
136 __func__, rc);
137 goto done;
138 }
139
140 rc = slim_config_mgrports(sdev, &(dma_data->ph),
141 dai_data->ch_cnt,
142 &(dai_data->port_cfg));
143 if (rc < 0) {
144 dev_err(&sdev->dev,
145 "%s: config mgrport failed rc %d\n",
146 __func__, rc);
147 goto err_done;
148 }
149
150 for (i = 0; i < dai_data->ch_cnt; i++) {
151 rc = slim_connect_sink(sdev,
152 &dma_data->ph, 1,
153 dai_data->chan_h[i]);
154 if (rc < 0) {
155 dev_err(&sdev->dev,
156 "%s: slim_connect_sink failed, ch = %d, err = %d\n",
157 __func__, i, rc);
158 goto err_done;
159 }
160 }
161
162 rc = slim_control_ch(sdev,
163 dai_data->grph,
164 SLIM_CH_ACTIVATE, true);
165 if (rc < 0) {
166 dev_err(&sdev->dev,
167 "%s: slim activate ch failed, err = %d\n",
168 __func__, rc);
169 goto err_done;
170 }
171 /* Mark dai status as running */
172 SET_DAI_STATE(dai_data->status, DAI_STATE_RUNNING);
173 } else {
174 if (!(dai_data->status & DAI_STATE_RUNNING)) {
175 dev_err(&sdev->dev,
176 "%s: dai id (%d) has invalid state 0x%x\n",
177 __func__, dai->id, dai_data->status);
178 return -EINVAL;
179 }
180
181 rc = slim_control_ch(sdev,
182 dai_data->grph,
183 SLIM_CH_REMOVE, true);
184 if (rc < 0) {
185 dev_err(&sdev->dev,
186 "%s: slim activate ch failed, err = %d\n",
187 __func__, rc);
188 goto done;
189 }
190
191 rc = slim_dealloc_mgrports(sdev,
192 &dma_data->ph, 1);
193 if (rc < 0) {
194 dev_err(&sdev->dev,
195 "%s: dealloc mgrport failed, err = %d\n",
196 __func__, rc);
197 goto done;
198 }
199 /* clear running state for dai*/
200 CLR_DAI_STATE(dai_data->status, DAI_STATE_RUNNING);
201 }
202
203 return rc;
204
205err_done:
206 rc1 = slim_dealloc_mgrports(sdev,
207 &dma_data->ph, 1);
208 if (rc1 < 0)
209 dev_err(&sdev->dev,
210 "%s: dealloc mgrport failed, err = %d\n",
211 __func__, rc1);
212done:
213 return rc;
214}
215
216static int msm_dai_slim_hw_params(
217 struct snd_pcm_substream *substream,
218 struct snd_pcm_hw_params *params,
219 struct snd_soc_dai *dai)
220{
221 struct msm_dai_slim_drv_data *drv_data = dev_get_drvdata(dai->dev);
222 struct msm_slim_dai_data *dai_data;
223 int rc = 0;
224
225 dai_data = msm_slim_get_dai_data(drv_data, dai);
226 if (!dai_data) {
227 dev_err(dai->dev,
228 "%s: Invalid dai_data for dai_id %d\n",
229 __func__, dai->id);
230 rc = -EINVAL;
231 goto done;
232 }
233
234 if (!dai_data->ch_cnt || dai_data->ch_cnt != params_channels(params)) {
235 dev_err(dai->dev, "%s: invalid ch_cnt %d %d\n",
236 __func__, dai_data->ch_cnt, params_channels(params));
237 rc = -EINVAL;
238 goto done;
239 }
240
241 dai_data->rate = params_rate(params);
242 dai_data->port_cfg.port_opts = SLIM_OPT_NONE;
243 if (dai_data->rate >= SNDRV_PCM_RATE_48000)
244 dai_data->port_cfg.watermark = 16;
245 else
246 dai_data->port_cfg.watermark = 8;
247
248 switch (params_format(params)) {
249 case SNDRV_PCM_FORMAT_S16_LE:
250 dai_data->bits = 16;
251 break;
252 case SNDRV_PCM_FORMAT_S24_LE:
253 dai_data->bits = 24;
254 break;
255 case SNDRV_PCM_FORMAT_S32_LE:
256 dai_data->bits = 32;
257 break;
258 default:
259 dev_err(dai->dev, "%s: invalid format %d\n", __func__,
260 params_format(params));
261 rc = -EINVAL;
262 goto done;
263 }
264
265 dev_dbg(dai->dev, "%s: ch_cnt=%u rate=%u, bit_width = %u\n",
266 __func__, dai_data->ch_cnt, dai_data->rate,
267 dai_data->bits);
268done:
269 return rc;
270}
271
272static int msm_dai_slim_set_channel_map(struct snd_soc_dai *dai,
273 unsigned int tx_num, unsigned int *tx_slot,
274 unsigned int rx_num, unsigned int *rx_slot)
275{
276 struct msm_dai_slim_drv_data *drv_data = dev_get_drvdata(dai->dev);
277 struct msm_slim_dai_data *dai_data;
278 struct snd_soc_dai_driver *dai_drv;
279 u8 i = 0;
280
281 dev_dbg(dai->dev,
282 "%s: tx_num=%u, rx_num=%u\n",
283 __func__, tx_num, rx_num);
284
285 dai_data = msm_slim_get_dai_data(drv_data, dai);
286 if (!dai_data) {
287 dev_err(dai->dev,
288 "%s: Invalid dai_data for dai_id %d\n",
289 __func__, dai->id);
290 return -EINVAL;
291 }
292
293 dai_drv = dai_data->dai_drv;
294
295 if (tx_num > dai_drv->capture.channels_max) {
296 dev_err(dai->dev, "%s: tx_num %u max out master port cnt\n",
297 __func__, tx_num);
298 return -EINVAL;
299 }
300
301 for (i = 0; i < tx_num; i++)
302 dai_data->sh_ch[i] = tx_slot[i];
303
304 dai_data->ch_cnt = tx_num;
305 return 0;
306}
307
308static int msm_dai_slim_prepare(struct snd_pcm_substream *substream,
309 struct snd_soc_dai *dai)
310{
311 struct msm_dai_slim_drv_data *drv_data = dev_get_drvdata(dai->dev);
312 struct msm_slim_dma_data *dma_data;
313 struct msm_slim_dai_data *dai_data = NULL;
314 struct slim_ch prop;
315 int rc;
316 u8 i, j;
317
318 dai_data = msm_slim_get_dai_data(drv_data, dai);
319 if (!dai_data) {
320 dev_err(dai->dev,
321 "%s: Invalid dai_data for dai %d\n",
322 __func__, dai->id);
323 return -EINVAL;
324 }
325
326 if (!(dai_data->status & DAI_STATE_INITIALIZED)) {
327 dev_err(dai->dev,
328 "%s: dai id (%d) has invalid state 0x%x\n",
329 __func__, dai->id, dai_data->status);
330 return -EINVAL;
331 }
332
333 if (dai_data->status & DAI_STATE_PREPARED) {
334 dev_dbg(dai->dev,
335 "%s: dai id (%d) has already prepared.\n",
336 __func__, dai->id);
337 return 0;
338 }
339
340 dma_data = &dai_data->dma_data;
341 snd_soc_dai_set_dma_data(dai, substream, dma_data);
342
343 for (i = 0; i < dai_data->ch_cnt; i++) {
344 rc = slim_query_ch(drv_data->sdev, dai_data->sh_ch[i],
345 &dai_data->chan_h[i]);
346 if (rc) {
347 dev_err(dai->dev, "%s:query chan handle failed rc %d\n",
348 __func__, rc);
349 goto error_chan_query;
350 }
351 }
352
353 prop.prot = SLIM_AUTO_ISO;
354 prop.baser = SLIM_RATE_4000HZ;
355 prop.dataf = SLIM_CH_DATAF_NOT_DEFINED;
356 prop.auxf = SLIM_CH_AUXF_NOT_APPLICABLE;
357 prop.ratem = (dai_data->rate/4000);
358 prop.sampleszbits = dai_data->bits;
359
360 rc = slim_define_ch(drv_data->sdev, &prop, dai_data->chan_h,
361 dai_data->ch_cnt, true, &dai_data->grph);
362
363 if (rc) {
364 dev_err(dai->dev, "%s:define chan failed rc %d\n",
365 __func__, rc);
366 goto error_define_chan;
367 }
368
369 /* Mark stream status as prepared */
370 SET_DAI_STATE(dai_data->status, DAI_STATE_PREPARED);
371
372 return rc;
373
374error_define_chan:
375error_chan_query:
376 for (j = 0; j < i; j++)
377 slim_dealloc_ch(drv_data->sdev, dai_data->chan_h[j]);
378 return rc;
379}
380
381static void msm_dai_slim_shutdown(struct snd_pcm_substream *stream,
382 struct snd_soc_dai *dai)
383{
384 struct msm_dai_slim_drv_data *drv_data = dev_get_drvdata(dai->dev);
385 struct msm_slim_dma_data *dma_data = NULL;
386 struct msm_slim_dai_data *dai_data;
387 int i, rc = 0;
388
389 dai_data = msm_slim_get_dai_data(drv_data, dai);
390 dma_data = snd_soc_dai_get_dma_data(dai, stream);
391 if (!dma_data || !dai_data) {
392 dev_err(dai->dev,
393 "%s: Invalid %s\n", __func__,
394 (!dma_data) ? "dma_data" : "dai_data");
395 return;
396 }
397
398 if ((!(dai_data->status & DAI_STATE_PREPARED)) ||
399 dai_data->status & DAI_STATE_RUNNING) {
400 dev_err(dai->dev,
401 "%s: dai id (%d) has invalid state 0x%x\n",
402 __func__, dai->id, dai_data->status);
403 return;
404 }
405
406 for (i = 0; i < dai_data->ch_cnt; i++) {
407 rc = slim_dealloc_ch(drv_data->sdev, dai_data->chan_h[i]);
408 if (rc) {
409 dev_err(dai->dev,
410 "%s: dealloc_ch failed, err = %d\n",
411 __func__, rc);
412 }
413 }
414
415 snd_soc_dai_set_dma_data(dai, stream, NULL);
416 /* clear prepared state for the dai */
417 CLR_DAI_STATE(dai_data->status, DAI_STATE_PREPARED);
418}
419
420static const struct snd_soc_component_driver msm_dai_slim_component = {
421 .name = "msm-dai-slim-cmpnt",
422};
423
424static struct snd_soc_dai_ops msm_dai_slim_ops = {
425 .prepare = msm_dai_slim_prepare,
426 .hw_params = msm_dai_slim_hw_params,
427 .shutdown = msm_dai_slim_shutdown,
428 .set_channel_map = msm_dai_slim_set_channel_map,
429};
430
431static struct snd_soc_dai_driver msm_slim_dais[] = {
432 {
433 /*
434 * The first dai name should be same as device name
435 * to support registering single and multile dais.
436 */
437 .name = SLIM_DEV_NAME,
438 .id = MSM_DAI_SLIM0,
439 .capture = {
440 .rates = SLIM_DAI_RATES,
441 .formats = SLIM_DAI_FORMATS,
442 .channels_min = 1,
443 /*
444 * max channels allowed is
445 * dependent on platform and
446 * will be updated before this
447 * dai driver is registered.
448 */
449 .channels_max = 1,
450 .rate_min = 8000,
451 .rate_max = 384000,
452 .stream_name = "SLIM_DAI0 Capture",
453 },
454 .ops = &msm_dai_slim_ops,
455 },
456 /*
457 * If multiple dais are needed,
458 * add dais here and update the
459 * dai_id enum.
460 */
461};
462
463static void msm_dai_slim_remove_dai_data(
464 struct device *dev,
465 struct msm_dai_slim_drv_data *drv_data)
466{
467 int i;
468 struct msm_slim_dai_data *dai_data_t;
469
470 for (i = 0; i < drv_data->num_dais; i++) {
471 dai_data_t = &drv_data->slim_dai_data[i];
472
473 kfree(dai_data_t->chan_h);
474 dai_data_t->chan_h = NULL;
475 kfree(dai_data_t->sh_ch);
476 dai_data_t->sh_ch = NULL;
477 }
478}
479
480static int msm_dai_slim_populate_dai_data(struct device *dev,
481 struct msm_dai_slim_drv_data *drv_data)
482{
483 struct snd_soc_dai_driver *dai_drv;
484 struct msm_slim_dai_data *dai_data_t;
485 u8 num_ch;
486 int i, j, rc;
487
488 for (i = 0; i < drv_data->num_dais; i++) {
489 num_ch = 0;
490 dai_drv = &msm_slim_dais[i];
491 num_ch += dai_drv->capture.channels_max;
492 num_ch += dai_drv->playback.channels_max;
493
494 dai_data_t = &drv_data->slim_dai_data[i];
495 dai_data_t->dai_drv = dai_drv;
496 dai_data_t->dai_id = dai_drv->id;
497 dai_data_t->dma_data.sdev = drv_data->sdev;
498 dai_data_t->dma_data.dai_channel_ctl =
499 msm_dai_slim_ch_ctl;
500 SET_DAI_STATE(dai_data_t->status,
501 DAI_STATE_INITIALIZED);
502
503 dai_data_t->chan_h = devm_kzalloc(dev,
504 sizeof(u16) * num_ch,
505 GFP_KERNEL);
506 if (!dai_data_t->chan_h) {
507 dev_err(dev,
508 "%s: DAI ID %d, Failed to alloc channel handles\n",
509 __func__, i);
510 rc = -ENOMEM;
511 goto err_mem_alloc;
512 }
513
514 dai_data_t->sh_ch = devm_kzalloc(dev,
515 sizeof(u16) * num_ch,
516 GFP_KERNEL);
517 if (!dai_data_t->sh_ch) {
518 dev_err(dev,
519 "%s: DAI ID %d, Failed to alloc sh_ch\n",
520 __func__, i);
521 rc = -ENOMEM;
522 goto err_mem_alloc;
523 }
524 }
525 return 0;
526
527err_mem_alloc:
528 for (j = 0; j < i; j++) {
529 dai_data_t = &drv_data->slim_dai_data[i];
530
531 devm_kfree(dev, dai_data_t->chan_h);
532 dai_data_t->chan_h = NULL;
533
534 devm_kfree(dev, dai_data_t->sh_ch);
535 dai_data_t->sh_ch = NULL;
536 }
537 return rc;
538}
539
540static int msm_dai_slim_dev_probe(struct slim_device *sdev)
541{
542 int rc, i;
543 u8 max_channels;
544 u32 apps_ch_pipes;
545 struct msm_dai_slim_drv_data *drv_data;
546 struct device *dev = &sdev->dev;
547 struct snd_soc_dai_driver *dai_drv;
548
549 if (!dev->of_node ||
550 !dev->of_node->parent) {
551 dev_err(dev,
552 "%s: Invalid %s\n", __func__,
553 (!dev->of_node) ? "of_node" : "parent_of_node");
554 return -EINVAL;
555 }
556
557 rc = of_property_read_u32(dev->of_node->parent,
558 "qcom,apps-ch-pipes",
559 &apps_ch_pipes);
560 if (rc) {
561 dev_err(dev,
562 "%s: Failed to lookup property %s in node %s, err = %d\n",
563 __func__, "qcom,apps-ch-pipes",
564 dev->of_node->parent->full_name, rc);
565 goto err_ret;
566 }
567
568 max_channels = hweight_long(apps_ch_pipes);
569 if (max_channels <= 0) {
570 dev_err(dev,
571 "%s: Invalid apps owned ports %d\n",
572 __func__, max_channels);
573 goto err_ret;
574 }
575
576 dev_dbg(dev, "%s: max channels = %u\n",
577 __func__, max_channels);
578
579 for (i = 0; i < ARRAY_SIZE(msm_slim_dais); i++) {
580 dai_drv = &msm_slim_dais[i];
581 dai_drv->capture.channels_max = max_channels;
582 dai_drv->playback.channels_max = max_channels;
583 }
584
585 drv_data = devm_kzalloc(dev, sizeof(*drv_data),
586 GFP_KERNEL);
587 if (!drv_data) {
588 rc = -ENOMEM;
589 goto err_ret;
590 }
591
592 drv_data->sdev = sdev;
593 drv_data->num_dais = NUM_SLIM_DAIS;
594
595 rc = msm_dai_slim_populate_dai_data(dev, drv_data);
596 if (rc) {
597 dev_err(dev,
598 "%s: failed to setup dai_data, err = %d\n",
599 __func__, rc);
600 goto err_populate_dai;
601 }
602
603 rc = snd_soc_register_component(&sdev->dev, &msm_dai_slim_component,
604 msm_slim_dais, NUM_SLIM_DAIS);
605 if (rc < 0) {
606 dev_err(dev, "%s: failed to register DAI, err = %d\n",
607 __func__, rc);
608 goto err_reg_comp;
609 }
610
611 dev_set_drvdata(dev, drv_data);
612 return rc;
613
614err_reg_comp:
615 msm_dai_slim_remove_dai_data(dev, drv_data);
616
617err_populate_dai:
618 devm_kfree(dev, drv_data);
619
620err_ret:
621 return rc;
622}
623
624static int msm_dai_slim_dev_remove(struct slim_device *sdev)
625{
626 snd_soc_unregister_component(&sdev->dev);
627 return 0;
628}
629
630static const struct slim_device_id msm_dai_slim_dt_match[] = {
631 {SLIM_DEV_NAME, 0 },
632 {}
633};
634
635static struct slim_driver msm_dai_slim_driver = {
636 .driver = {
637 .name = SLIM_DEV_NAME,
638 .owner = THIS_MODULE,
639 },
640 .probe = msm_dai_slim_dev_probe,
641 .remove = msm_dai_slim_dev_remove,
642 .id_table = msm_dai_slim_dt_match,
643};
644
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530645int __init msm_dai_slim_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530646{
647 int rc;
648
649 rc = slim_driver_register(&msm_dai_slim_driver);
650 if (rc)
651 pr_err("%s: failed to register with slimbus driver rc = %d",
652 __func__, rc);
653 return rc;
654}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530655
Asish Bhattacharya5faacb32017-12-04 17:23:15 +0530656void msm_dai_slim_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530657{
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530658 slim_driver_unregister(&msm_dai_slim_driver);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530659}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530660
661/* Module information */
662MODULE_DESCRIPTION("Slimbus apps-owned channel handling driver");
663MODULE_LICENSE("GPL v2");