blob: 25478dd952bd147130f47e1334600e7c8025f200 [file] [log] [blame]
/* sound/soc/samsung/eax-dai.c
*
* Exynos Audio Mixer slowpath driver
*
* Copyright (c) 2014 Samsung Electronics Co. Ltd.
* Yeongman Seo <yman.seo@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/serio.h>
#include <linux/time.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/clk.h>
#include <linux/mutex.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/uaccess.h>
#include <linux/pm_runtime.h>
#include <linux/iommu.h>
#include <linux/dma-mapping.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/firmware.h>
#include <sound/soc.h>
#include <sound/pcm_params.h>
#include <sound/exynos.h>
#include "lpass.h"
#include "dma.h"
#include "eax.h"
#define EAX_CH_MAX 8
#define EAX_NAME_MAX PLATFORM_NAME_SIZE
#define EAX_RATES SNDRV_PCM_RATE_8000_192000
#define EAX_FMTS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
static struct eax_slowpath_info {
struct platform_device *pdev;
struct mutex mutex;
spinlock_t lock;
int ch_max;
bool master;
struct snd_soc_dai *master_dai;
const struct snd_soc_dai_ops *master_dai_ops;
int (*master_dai_suspend)(struct snd_soc_dai *dai);
int (*master_dai_resume)(struct snd_soc_dai *dai);
} esi;
static LIST_HEAD(slowpath_ch_list);
static DECLARE_WAIT_QUEUE_HEAD(eax_slowpath_wq);
static int eax_slowpath_dai_probe(struct snd_soc_dai *dai);
static int eax_slowpath_dai_remove(struct snd_soc_dai *dai);
static int eax_slowpath_dai_suspend(struct snd_soc_dai *dai);
static int eax_slowpath_dai_resume(struct snd_soc_dai *dai);
static const struct snd_soc_dai_ops eax_slowpath_dai_ops;
int eax_slowpath_dev_register(struct device *dev_master, const char *name,
struct s3c_dma_params *dma_params, int ch)
{
struct ch_info *ci;
int ret, n;
if (ch > EAX_CH_MAX) {
pr_err("%s: Channel error! (max. %d)\n",
__func__, EAX_CH_MAX);
return -EINVAL;
}
eax_slowpath_params_register(dma_params);
for (n = 0; n < ch; n++) {
ci = kzalloc(sizeof(struct ch_info), GFP_KERNEL);
if (!ci) {
pr_err("%s: Memory alloc fails!\n", __func__);
return -ENOMEM;
}
snprintf(ci->name, EAX_NAME_MAX, "samsung-eax-slow.%d", n);
ci->dev_master = dev_master;
ci->dma_params = dma_params;
ci->opened = false;
ci->running = false;
ci->dai_drv.name = name;
ci->dai_drv.symmetric_rates = 1;
ci->dai_drv.probe = eax_slowpath_dai_probe;
ci->dai_drv.remove = eax_slowpath_dai_remove;
ci->dai_drv.ops = &eax_slowpath_dai_ops;
ci->dai_drv.suspend = eax_slowpath_dai_suspend;
ci->dai_drv.resume = eax_slowpath_dai_resume;
ci->dai_drv.playback.channels_min = 2;
ci->dai_drv.playback.channels_max = 2;
ci->dai_drv.playback.rates = EAX_RATES;
ci->dai_drv.playback.formats = EAX_FMTS;
ci->pdev = platform_device_alloc(ci->name, -1);
if (IS_ERR(ci->pdev)) {
kfree(ci);
return -ENODEV;
}
esi.ch_max++;
list_add(&ci->node, &slowpath_ch_list);
platform_set_drvdata(ci->pdev, ci);
ret = platform_device_add(ci->pdev);
if (ret < 0)
return -ENODEV;
}
return 0;
}
int eax_slowpath_dai_register(struct snd_soc_dai *dai,
const struct snd_soc_dai_ops *dai_ops,
int (*dai_suspend)(struct snd_soc_dai *dai),
int (*dai_resume)(struct snd_soc_dai *dai))
{
pr_debug("%s: dai %p, dai_ops %p\n", __func__, dai, dai_ops);
esi.master = true;
esi.master_dai = dai;
esi.master_dai_ops = dai_ops;
esi.master_dai_suspend = dai_suspend;
esi.master_dai_resume = dai_resume;
eax_slowpath_dma_dai_register(dai);
return 0;
}
int eax_slowpath_dai_unregister(void)
{
esi.master = false;
esi.master_dai = NULL;
esi.master_dai_ops = NULL;
esi.master_dai_suspend = NULL;
esi.master_dai_resume = NULL;
eax_slowpath_dma_dai_unregister();
return 0;
}
static inline struct ch_info *to_info(struct snd_soc_dai *dai)
{
return snd_soc_dai_get_drvdata(dai);
}
static inline bool eax_slowpath_dai_any_tx_opened(void)
{
struct ch_info *ci;
list_for_each_entry(ci, &slowpath_ch_list, node) {
if (ci->opened)
return true;
}
return false;
}
static inline bool eax_slowpath_dai_any_tx_running(void)
{
struct ch_info *ci;
list_for_each_entry(ci, &slowpath_ch_list, node) {
if (ci->running)
return true;
}
return false;
}
static int eax_slowpath_dai_trigger(struct snd_pcm_substream *substream,
int cmd, struct snd_soc_dai *dai)
{
struct ch_info *ci = to_info(dai);
unsigned long flags;
int ret = 0;
if (!esi.master)
return -ENODEV;
spin_lock_irqsave(&esi.lock, flags);
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
if (!eax_slowpath_dai_any_tx_running())
ret = (*esi.master_dai_ops->trigger)(substream,
cmd, esi.master_dai);
ci->running = true;
break;
case SNDRV_PCM_TRIGGER_STOP:
ci->running = false;
if (!eax_slowpath_dai_any_tx_running())
ret = (*esi.master_dai_ops->trigger)(substream,
cmd, esi.master_dai);
break;
default:
break;
}
spin_unlock_irqrestore(&esi.lock, flags);
return ret;
}
#ifdef CONFIG_SND_SOC_I2S_1840_TDM
static int eax_slowpath_dai_set_tdm_slot(struct snd_soc_dai *dai,
unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
{
unsigned long flags;
if (!esi.master)
return -ENODEV;
spin_lock_irqsave(&esi.lock, flags);
if (eax_slowpath_dai_any_tx_running()) {
spin_unlock_irqrestore(&esi.lock, flags);
return 0;
}
spin_unlock_irqrestore(&esi.lock, flags);
return (*esi.master_dai_ops->set_tdm_slot)(esi.master_dai,
tx_mask, rx_mask, slots, slot_width);
}
#endif
static int eax_slowpath_dai_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
unsigned long flags;
if (!esi.master)
return -ENODEV;
spin_lock_irqsave(&esi.lock, flags);
if (eax_slowpath_dai_any_tx_running()) {
spin_unlock_irqrestore(&esi.lock, flags);
return 0;
}
spin_unlock_irqrestore(&esi.lock, flags);
return (*esi.master_dai_ops->hw_params)(substream, params,
esi.master_dai);
}
static int eax_slowpath_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
unsigned long flags;
if (!esi.master)
return -ENODEV;
spin_lock_irqsave(&esi.lock, flags);
if (eax_slowpath_dai_any_tx_running()) {
spin_unlock_irqrestore(&esi.lock, flags);
return 0;
}
spin_unlock_irqrestore(&esi.lock, flags);
return (*esi.master_dai_ops->set_fmt)(esi.master_dai, fmt);
}
static int eax_slowpath_dai_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
{
unsigned long flags;
if (!esi.master)
return -ENODEV;
spin_lock_irqsave(&esi.lock, flags);
if (eax_slowpath_dai_any_tx_running()) {
spin_unlock_irqrestore(&esi.lock, flags);
return 0;
}
spin_unlock_irqrestore(&esi.lock, flags);
return (*esi.master_dai_ops->set_clkdiv)(esi.master_dai, div_id, div);
}
static int eax_slowpath_dai_set_sysclk(struct snd_soc_dai *dai,
int clk_id, unsigned int rfs, int dir)
{
unsigned long flags;
if (!esi.master)
return -ENODEV;
spin_lock_irqsave(&esi.lock, flags);
if (eax_slowpath_dai_any_tx_running()) {
spin_unlock_irqrestore(&esi.lock, flags);
return 0;
}
spin_unlock_irqrestore(&esi.lock, flags);
return (*esi.master_dai_ops->set_sysclk)(esi.master_dai,
clk_id, rfs, dir);
}
static int eax_slowpath_dai_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct ch_info *ci = to_info(dai);
int ret = 0;
if (!esi.master)
return -ENODEV;
lpass_add_stream();
mutex_lock(&esi.mutex);
if (!eax_slowpath_dai_any_tx_opened())
ret = (*esi.master_dai_ops->startup)(substream, esi.master_dai);
ci->opened = true;
mutex_unlock(&esi.mutex);
return ret;
}
static void eax_slowpath_dai_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct ch_info *ci = to_info(dai);
if (!esi.master)
return;
mutex_lock(&esi.mutex);
ci->opened = false;
if (!eax_slowpath_dai_any_tx_opened())
(*esi.master_dai_ops->shutdown)(substream, esi.master_dai);
mutex_unlock(&esi.mutex);
lpass_remove_stream();
}
static int eax_slowpath_dai_probe(struct snd_soc_dai *dai)
{
pr_debug("%s\n", __func__);
return 0;
}
static int eax_slowpath_dai_remove(struct snd_soc_dai *dai)
{
pr_debug("%s\n", __func__);
return 0;
}
static int eax_slowpath_dai_suspend(struct snd_soc_dai *dai)
{
if (dai->active && esi.master_dai_suspend)
return (*esi.master_dai_suspend)(esi.master_dai);
return 0;
}
static int eax_slowpath_dai_resume(struct snd_soc_dai *dai)
{
if (dai->active && esi.master_dai_resume)
return (*esi.master_dai_resume)(esi.master_dai);
return 0;
}
static const struct snd_soc_dai_ops eax_slowpath_dai_ops = {
.trigger = eax_slowpath_dai_trigger,
.hw_params = eax_slowpath_dai_hw_params,
.set_fmt = eax_slowpath_dai_set_fmt,
.set_clkdiv = eax_slowpath_dai_set_clkdiv,
.set_sysclk = eax_slowpath_dai_set_sysclk,
#ifdef CONFIG_SND_SOC_I2S_1840_TDM
.set_tdm_slot = eax_slowpath_dai_set_tdm_slot,
#endif
.startup = eax_slowpath_dai_startup,
.shutdown = eax_slowpath_dai_shutdown,
};
static const struct snd_soc_component_driver eax_slowpath_dai_component = {
.name = "samsung-eax-slowpath",
};
static int eax_slowpath_ch_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct ch_info *ci;
ci = platform_get_drvdata(pdev);
snd_soc_register_component(dev, &eax_slowpath_dai_component, &ci->dai_drv, 1);
eax_slowpath_asoc_platform_register(dev);
return 0;
}
static int eax_slowpath_ch_remove(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
snd_soc_unregister_component(dev);
return 0;
}
static const char banner[] =
KERN_INFO "Exynos Audio Slowpath Mixer driver, (c)2015 Samsung Electronics\n";
static int eax_slowpath_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
printk(banner);
mutex_init(&esi.mutex);
spin_lock_init(&esi.lock);
esi.pdev = pdev;
pm_runtime_enable(dev);
return 0;
}
static int eax_slowpath_remove(struct platform_device *pdev)
{
pm_runtime_disable(&pdev->dev);
return 0;
}
#ifdef CONFIG_PM_RUNTIME
static int eax_slowpath_runtime_suspend(struct device *dev)
{
dev_dbg(dev, "%s entered\n", __func__);
return 0;
}
static int eax_slowpath_runtime_resume(struct device *dev)
{
dev_dbg(dev, "%s entered\n", __func__);
return 0;
}
#endif
#if !defined(CONFIG_PM_RUNTIME) && defined(CONFIG_PM_SLEEP)
static int eax_slowpath_suspend(struct device *dev)
{
dev_dbg(dev, "%s entered\n", __func__);
return 0;
}
static int eax_slowpath_resume(struct device *dev)
{
dev_dbg(dev, "%s entered\n", __func__);
return 0;
}
#else
#define eax_slowpath_suspend NULL
#define eax_slowpath_resume NULL
#endif
static struct platform_device_id eax_slowpath_ch_driver_ids[] = {
{ .name = "samsung-eax-slow.0", },
{ .name = "samsung-eax-slow.1", },
{},
};
MODULE_DEVICE_TABLE(platform, eax_slowpath_ch_driver_ids);
static const struct dev_pm_ops eax_slowpath_ch_pmops = {
SET_SYSTEM_SLEEP_PM_OPS(
eax_slowpath_suspend,
eax_slowpath_resume
)
SET_RUNTIME_PM_OPS(
eax_slowpath_runtime_suspend,
eax_slowpath_runtime_resume,
NULL
)
};
static struct platform_driver eax_slowpath_dai_driver = {
.probe = eax_slowpath_ch_probe,
.remove = eax_slowpath_ch_remove,
.id_table = eax_slowpath_ch_driver_ids,
.driver = {
.name = "samsung-eax-slowpath",
.owner = THIS_MODULE,
.pm = &eax_slowpath_ch_pmops,
},
};
module_platform_driver(eax_slowpath_dai_driver);
static struct platform_device_id eax_slowpath_driver_ids[] = {
{
.name = "samsung-amixer-slow",
},
{},
};
MODULE_DEVICE_TABLE(platform, eax_slowpath_driver_ids);
#ifdef CONFIG_OF
static const struct of_device_id exynos_eax_slowpath_match[] = {
{
.compatible = "samsung,exynos-amixer-slowpath",
},
{},
};
MODULE_DEVICE_TABLE(of, exynos_eax_slowpath_match);
#endif
static struct platform_driver eax_slowpath_driver = {
.probe = eax_slowpath_probe,
.remove = eax_slowpath_remove,
.id_table = eax_slowpath_driver_ids,
.driver = {
.name = "samsung-amixer-slow",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(exynos_eax_slowpath_match),
},
};
module_platform_driver(eax_slowpath_driver);
MODULE_AUTHOR("Hyunwoong Kim <khw0178.kim@samsung.com>");
MODULE_DESCRIPTION("Exynos Audio Mixer Slowpath driver");
MODULE_LICENSE("GPL");