xref: /linux/sound/soc/mediatek/common/mtk-afe-platform-driver.c (revision 3ad0876554cafa368f574d4d408468510543e9ff)
1 /*
2  * mtk-afe-platform-driver.c  --  Mediatek afe platform driver
3  *
4  * Copyright (c) 2016 MediaTek Inc.
5  * Author: Garlic Tseng <garlic.tseng@mediatek.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 and
9  * only version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 
17 #include <linux/module.h>
18 #include <linux/dma-mapping.h>
19 #include <sound/soc.h>
20 
21 #include "mtk-afe-platform-driver.h"
22 #include "mtk-base-afe.h"
23 
24 static snd_pcm_uframes_t mtk_afe_pcm_pointer
25 			 (struct snd_pcm_substream *substream)
26 {
27 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
28 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
29 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
30 	struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
31 	const struct mtk_base_memif_data *memif_data = memif->data;
32 	struct regmap *regmap = afe->regmap;
33 	struct device *dev = afe->dev;
34 	int reg_ofs_base = memif_data->reg_ofs_base;
35 	int reg_ofs_cur = memif_data->reg_ofs_cur;
36 	unsigned int hw_ptr = 0, hw_base = 0;
37 	int ret, pcm_ptr_bytes;
38 
39 	ret = regmap_read(regmap, reg_ofs_cur, &hw_ptr);
40 	if (ret || hw_ptr == 0) {
41 		dev_err(dev, "%s hw_ptr err\n", __func__);
42 		pcm_ptr_bytes = 0;
43 		goto POINTER_RETURN_FRAMES;
44 	}
45 
46 	ret = regmap_read(regmap, reg_ofs_base, &hw_base);
47 	if (ret || hw_base == 0) {
48 		dev_err(dev, "%s hw_ptr err\n", __func__);
49 		pcm_ptr_bytes = 0;
50 		goto POINTER_RETURN_FRAMES;
51 	}
52 
53 	pcm_ptr_bytes = hw_ptr - hw_base;
54 
55 POINTER_RETURN_FRAMES:
56 	return bytes_to_frames(substream->runtime, pcm_ptr_bytes);
57 }
58 
59 static const struct snd_pcm_ops mtk_afe_pcm_ops = {
60 	.ioctl = snd_pcm_lib_ioctl,
61 	.pointer = mtk_afe_pcm_pointer,
62 };
63 
64 static int mtk_afe_pcm_new(struct snd_soc_pcm_runtime *rtd)
65 {
66 	size_t size;
67 	struct snd_card *card = rtd->card->snd_card;
68 	struct snd_pcm *pcm = rtd->pcm;
69 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
70 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
71 
72 	size = afe->mtk_afe_hardware->buffer_bytes_max;
73 	return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
74 						     card->dev, size, size);
75 }
76 
77 static void mtk_afe_pcm_free(struct snd_pcm *pcm)
78 {
79 	snd_pcm_lib_preallocate_free_for_all(pcm);
80 }
81 
82 const struct snd_soc_component_driver mtk_afe_pcm_platform = {
83 	.name = AFE_PCM_NAME,
84 	.ops = &mtk_afe_pcm_ops,
85 	.pcm_new = mtk_afe_pcm_new,
86 	.pcm_free = mtk_afe_pcm_free,
87 };
88 EXPORT_SYMBOL_GPL(mtk_afe_pcm_platform);
89 
90 MODULE_DESCRIPTION("Mediatek simple platform driver");
91 MODULE_AUTHOR("Garlic Tseng <garlic.tseng@mediatek.com>");
92 MODULE_LICENSE("GPL v2");
93 
94