Commit df799b95 authored by Eason Yen's avatar Eason Yen Committed by Mark Brown

ASoC: mediatek: common: refine hw_params and hw_prepare

Refine mtk_afe_fe_hw_params and mtk_afe_fe_prepare by
these helpers.
- mtk_memif_set_enable
- mtk_memif_set_disable
- mtk_memif_set_addr
- mtk_memif_set_channel
- mtk_memif_set_rate
- mtk_memif_set_rate_substream
- mtk_memif_set_format
- mtk_memif_set_pbuf_size
Signed-off-by: default avatarEason Yen <eason.yen@mediatek.com>
Link: https://lore.kernel.org/r/1573814926-15805-3-git-send-email-eason.yen@mediatek.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 9cdf85a1
...@@ -6,11 +6,13 @@ ...@@ -6,11 +6,13 @@
* Author: Garlic Tseng <garlic.tseng@mediatek.com> * Author: Garlic Tseng <garlic.tseng@mediatek.com>
*/ */
#include <linux/io.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/pm_runtime.h> #include <linux/pm_runtime.h>
#include <linux/regmap.h> #include <linux/regmap.h>
#include <sound/soc.h> #include <sound/soc.h>
#include "mtk-afe-platform-driver.h" #include "mtk-afe-platform-driver.h"
#include <sound/pcm_params.h>
#include "mtk-afe-fe-dai.h" #include "mtk-afe-fe-dai.h"
#include "mtk-base-afe.h" #include "mtk-base-afe.h"
...@@ -120,50 +122,64 @@ int mtk_afe_fe_hw_params(struct snd_pcm_substream *substream, ...@@ -120,50 +122,64 @@ int mtk_afe_fe_hw_params(struct snd_pcm_substream *substream,
{ {
struct snd_soc_pcm_runtime *rtd = substream->private_data; struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai); struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id]; int id = rtd->cpu_dai->id;
int msb_at_bit33 = 0; struct mtk_base_afe_memif *memif = &afe->memif[id];
int ret, fs = 0; int ret;
unsigned int channels = params_channels(params);
unsigned int rate = params_rate(params);
snd_pcm_format_t format = params_format(params);
ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params)); ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
if (ret < 0) if (ret < 0)
return ret; return ret;
msb_at_bit33 = upper_32_bits(substream->runtime->dma_addr) ? 1 : 0; if (afe->request_dram_resource)
memif->phys_buf_addr = lower_32_bits(substream->runtime->dma_addr); afe->request_dram_resource(afe->dev);
memif->buffer_size = substream->runtime->dma_bytes;
dev_dbg(afe->dev, "%s(), %s, ch %d, rate %d, fmt %d, dma_addr %pad, dma_area %p, dma_bytes 0x%zx\n",
/* start */ __func__, memif->data->name,
mtk_regmap_write(afe->regmap, memif->data->reg_ofs_base, channels, rate, format,
memif->phys_buf_addr); &substream->runtime->dma_addr,
/* end */ substream->runtime->dma_area,
mtk_regmap_write(afe->regmap, substream->runtime->dma_bytes);
memif->data->reg_ofs_base + AFE_BASE_END_OFFSET,
memif->phys_buf_addr + memif->buffer_size - 1); memset_io(substream->runtime->dma_area, 0,
substream->runtime->dma_bytes);
/* set MSB to 33-bit */
mtk_regmap_update_bits(afe->regmap, memif->data->msb_reg, /* set addr */
1, msb_at_bit33, memif->data->msb_shift); ret = mtk_memif_set_addr(afe, id,
substream->runtime->dma_area,
substream->runtime->dma_addr,
substream->runtime->dma_bytes);
if (ret) {
dev_err(afe->dev, "%s(), error, id %d, set addr, ret %d\n",
__func__, id, ret);
return ret;
}
/* set channel */ /* set channel */
if (memif->data->mono_shift >= 0) { ret = mtk_memif_set_channel(afe, id, channels);
unsigned int mono = (params_channels(params) == 1) ? 1 : 0; if (ret) {
dev_err(afe->dev, "%s(), error, id %d, set channel %d, ret %d\n",
mtk_regmap_update_bits(afe->regmap, memif->data->mono_reg, __func__, id, channels, ret);
1, mono, memif->data->mono_shift); return ret;
} }
/* set rate */ /* set rate */
if (memif->data->fs_shift < 0) ret = mtk_memif_set_rate_substream(substream, id, rate);
return 0; if (ret) {
dev_err(afe->dev, "%s(), error, id %d, set rate %d, ret %d\n",
fs = afe->memif_fs(substream, params_rate(params)); __func__, id, rate, ret);
return ret;
if (fs < 0) }
return -EINVAL;
mtk_regmap_update_bits(afe->regmap, memif->data->fs_reg, /* set format */
memif->data->fs_maskbit, fs, ret = mtk_memif_set_format(afe, id, format);
memif->data->fs_shift); if (ret) {
dev_err(afe->dev, "%s(), error, id %d, set format %d, ret %d\n",
__func__, id, format, ret);
return ret;
}
return 0; return 0;
} }
...@@ -172,6 +188,11 @@ EXPORT_SYMBOL_GPL(mtk_afe_fe_hw_params); ...@@ -172,6 +188,11 @@ EXPORT_SYMBOL_GPL(mtk_afe_fe_hw_params);
int mtk_afe_fe_hw_free(struct snd_pcm_substream *substream, int mtk_afe_fe_hw_free(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai) struct snd_soc_dai *dai)
{ {
struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
if (afe->release_dram_resource)
afe->release_dram_resource(afe->dev);
return snd_pcm_lib_free_pages(substream); return snd_pcm_lib_free_pages(substream);
} }
EXPORT_SYMBOL_GPL(mtk_afe_fe_hw_free); EXPORT_SYMBOL_GPL(mtk_afe_fe_hw_free);
...@@ -182,20 +203,25 @@ int mtk_afe_fe_trigger(struct snd_pcm_substream *substream, int cmd, ...@@ -182,20 +203,25 @@ int mtk_afe_fe_trigger(struct snd_pcm_substream *substream, int cmd,
struct snd_soc_pcm_runtime *rtd = substream->private_data; struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct snd_pcm_runtime * const runtime = substream->runtime; struct snd_pcm_runtime * const runtime = substream->runtime;
struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai); struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id]; int id = rtd->cpu_dai->id;
struct mtk_base_afe_memif *memif = &afe->memif[id];
struct mtk_base_afe_irq *irqs = &afe->irqs[memif->irq_usage]; struct mtk_base_afe_irq *irqs = &afe->irqs[memif->irq_usage];
const struct mtk_base_irq_data *irq_data = irqs->irq_data; const struct mtk_base_irq_data *irq_data = irqs->irq_data;
unsigned int counter = runtime->period_size; unsigned int counter = runtime->period_size;
int fs; int fs;
int ret;
dev_dbg(afe->dev, "%s %s cmd=%d\n", __func__, memif->data->name, cmd); dev_dbg(afe->dev, "%s %s cmd=%d\n", __func__, memif->data->name, cmd);
switch (cmd) { switch (cmd) {
case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_RESUME: case SNDRV_PCM_TRIGGER_RESUME:
mtk_regmap_update_bits(afe->regmap, ret = mtk_memif_set_enable(afe, id);
memif->data->enable_reg, if (ret) {
1, 1, memif->data->enable_shift); dev_err(afe->dev, "%s(), error, id %d, memif enable, ret %d\n",
__func__, id, ret);
return ret;
}
/* set irq counter */ /* set irq counter */
mtk_regmap_update_bits(afe->regmap, irq_data->irq_cnt_reg, mtk_regmap_update_bits(afe->regmap, irq_data->irq_cnt_reg,
...@@ -219,15 +245,19 @@ int mtk_afe_fe_trigger(struct snd_pcm_substream *substream, int cmd, ...@@ -219,15 +245,19 @@ int mtk_afe_fe_trigger(struct snd_pcm_substream *substream, int cmd,
return 0; return 0;
case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_STOP:
case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_SUSPEND:
mtk_regmap_update_bits(afe->regmap, memif->data->enable_reg, ret = mtk_memif_set_disable(afe, id);
1, 0, memif->data->enable_shift); if (ret) {
dev_err(afe->dev, "%s(), error, id %d, memif enable, ret %d\n",
__func__, id, ret);
}
/* disable interrupt */ /* disable interrupt */
mtk_regmap_update_bits(afe->regmap, irq_data->irq_en_reg, mtk_regmap_update_bits(afe->regmap, irq_data->irq_en_reg,
1, 0, irq_data->irq_en_shift); 1, 0, irq_data->irq_en_shift);
/* and clear pending IRQ */ /* and clear pending IRQ */
mtk_regmap_write(afe->regmap, irq_data->irq_clr_reg, mtk_regmap_write(afe->regmap, irq_data->irq_clr_reg,
1 << irq_data->irq_clr_shift); 1 << irq_data->irq_clr_shift);
return 0; return ret;
default: default:
return -EINVAL; return -EINVAL;
} }
...@@ -239,34 +269,15 @@ int mtk_afe_fe_prepare(struct snd_pcm_substream *substream, ...@@ -239,34 +269,15 @@ int mtk_afe_fe_prepare(struct snd_pcm_substream *substream,
{ {
struct snd_soc_pcm_runtime *rtd = substream->private_data; struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai); struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id]; int id = rtd->cpu_dai->id;
int hd_audio = 0; int pbuf_size;
int hd_align = 0;
/* set hd mode */ if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
switch (substream->runtime->format) { if (afe->get_memif_pbuf_size) {
case SNDRV_PCM_FORMAT_S16_LE: pbuf_size = afe->get_memif_pbuf_size(substream);
hd_audio = 0; mtk_memif_set_pbuf_size(afe, id, pbuf_size);
break; }
case SNDRV_PCM_FORMAT_S32_LE:
hd_audio = 1;
hd_align = 1;
break;
case SNDRV_PCM_FORMAT_S24_LE:
hd_audio = 1;
break;
default:
dev_err(afe->dev, "%s() error: unsupported format %d\n",
__func__, substream->runtime->format);
break;
} }
mtk_regmap_update_bits(afe->regmap, memif->data->hd_reg,
1, hd_audio, memif->data->hd_shift);
mtk_regmap_update_bits(afe->regmap, memif->data->hd_align_reg,
1, hd_align, memif->data->hd_align_mshift);
return 0; return 0;
} }
EXPORT_SYMBOL_GPL(mtk_afe_fe_prepare); EXPORT_SYMBOL_GPL(mtk_afe_fe_prepare);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment