| /* sound/soc/samsung/eax-dai.c |
| * |
| * Exynos Audio Mixer 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_RATES SNDRV_PCM_RATE_8000_192000 |
| #define EAX_FMTS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE) |
| |
| |
| static struct eax_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); |
| } ei; |
| |
| static LIST_HEAD(ch_list); |
| |
| static DECLARE_WAIT_QUEUE_HEAD(eax_wq); |
| |
| static int eax_dai_probe(struct snd_soc_dai *dai); |
| static int eax_dai_remove(struct snd_soc_dai *dai); |
| static int eax_dai_suspend(struct snd_soc_dai *dai); |
| static int eax_dai_resume(struct snd_soc_dai *dai); |
| static const struct snd_soc_dai_ops eax_dai_ops; |
| |
| int eax_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_dma_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.%d", n); |
| |
| ci->dev_master = dev_master; |
| ci->dma_params = dma_params; |
| ci->opened = false; |
| ci->running = false; |
| ci->ch_id = n; |
| ci->dai_drv.name = name; |
| ci->dai_drv.symmetric_rates = 1; |
| ci->dai_drv.probe = eax_dai_probe; |
| ci->dai_drv.remove = eax_dai_remove; |
| ci->dai_drv.ops = &eax_dai_ops; |
| ci->dai_drv.suspend = eax_dai_suspend; |
| ci->dai_drv.resume = eax_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; |
| } |
| |
| ei.ch_max++; |
| list_add(&ci->node, &ch_list); |
| platform_set_drvdata(ci->pdev, ci); |
| ret = platform_device_add(ci->pdev); |
| if (ret < 0) |
| return -ENODEV; |
| } |
| |
| return 0; |
| } |
| |
| int eax_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); |
| |
| ei.master = true; |
| ei.master_dai = dai; |
| ei.master_dai_ops = dai_ops; |
| ei.master_dai_suspend = dai_suspend; |
| ei.master_dai_resume = dai_resume; |
| |
| eax_dma_dai_register(dai); |
| |
| return 0; |
| } |
| |
| int eax_dai_unregister(void) |
| { |
| ei.master = false; |
| ei.master_dai = NULL; |
| ei.master_dai_ops = NULL; |
| ei.master_dai_suspend = NULL; |
| ei.master_dai_resume = NULL; |
| |
| eax_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_dai_any_tx_opened(void) |
| { |
| struct ch_info *ci; |
| |
| list_for_each_entry(ci, &ch_list, node) { |
| if (ci->opened) |
| return true; |
| } |
| |
| return false; |
| } |
| |
| static inline bool eax_dai_any_tx_running(void) |
| { |
| struct ch_info *ci; |
| |
| list_for_each_entry(ci, &ch_list, node) { |
| if (ci->running) |
| return true; |
| } |
| |
| return false; |
| } |
| |
| static int eax_dai_trigger(struct snd_pcm_substream *substream, |
| int cmd, struct snd_soc_dai *dai) |
| { |
| struct ch_info *ci = to_info(dai); |
| int ret = 0; |
| |
| if (!ei.master) |
| return -ENODEV; |
| |
| spin_lock(&ei.lock); |
| |
| switch (cmd) { |
| case SNDRV_PCM_TRIGGER_START: |
| if (!eax_dai_any_tx_running()) |
| ret = (*ei.master_dai_ops->trigger)(substream, |
| cmd, ei.master_dai); |
| ci->running = true; |
| break; |
| case SNDRV_PCM_TRIGGER_STOP: |
| ci->running = false; |
| if (!eax_dai_any_tx_running()) |
| ret = (*ei.master_dai_ops->trigger)(substream, |
| cmd, ei.master_dai); |
| break; |
| default: |
| break; |
| } |
| |
| spin_unlock(&ei.lock); |
| |
| return ret; |
| } |
| |
| #ifdef CONFIG_SND_SOC_I2S_1840_TDM |
| static int eax_dai_set_tdm_slot(struct snd_soc_dai *dai, |
| unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) |
| { |
| if (!ei.master) |
| return -ENODEV; |
| |
| spin_lock(&ei.lock); |
| if (eax_dai_any_tx_running()) { |
| spin_unlock(&ei.lock); |
| return 0; |
| } |
| spin_unlock(&ei.lock); |
| |
| return (*ei.master_dai_ops->set_tdm_slot)(ei.master_dai, |
| tx_mask, rx_mask, slots, slot_width); |
| } |
| #endif |
| |
| static int eax_dai_hw_params(struct snd_pcm_substream *substream, |
| struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) |
| { |
| if (!ei.master) |
| return -ENODEV; |
| |
| spin_lock(&ei.lock); |
| if (eax_dai_any_tx_running()) { |
| spin_unlock(&ei.lock); |
| return 0; |
| } |
| spin_unlock(&ei.lock); |
| |
| return (*ei.master_dai_ops->hw_params)(substream, params, |
| ei.master_dai); |
| } |
| |
| static int eax_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| { |
| if (!ei.master) |
| return -ENODEV; |
| |
| spin_lock(&ei.lock); |
| if (eax_dai_any_tx_running()) { |
| spin_unlock(&ei.lock); |
| return 0; |
| } |
| spin_unlock(&ei.lock); |
| |
| return (*ei.master_dai_ops->set_fmt)(ei.master_dai, fmt); |
| } |
| |
| static int eax_dai_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div) |
| { |
| if (!ei.master) |
| return -ENODEV; |
| |
| spin_lock(&ei.lock); |
| if (eax_dai_any_tx_running()) { |
| spin_unlock(&ei.lock); |
| return 0; |
| } |
| spin_unlock(&ei.lock); |
| |
| return (*ei.master_dai_ops->set_clkdiv)(ei.master_dai, div_id, div); |
| } |
| |
| static int eax_dai_set_sysclk(struct snd_soc_dai *dai, |
| int clk_id, unsigned int rfs, int dir) |
| { |
| if (!ei.master) |
| return -ENODEV; |
| |
| spin_lock(&ei.lock); |
| if (eax_dai_any_tx_running()) { |
| spin_unlock(&ei.lock); |
| return 0; |
| } |
| spin_unlock(&ei.lock); |
| |
| return (*ei.master_dai_ops->set_sysclk)(ei.master_dai, |
| clk_id, rfs, dir); |
| } |
| |
| static int eax_dai_startup(struct snd_pcm_substream *substream, |
| struct snd_soc_dai *dai) |
| { |
| struct ch_info *ci = to_info(dai); |
| int ret = 0; |
| |
| if (!ei.master) |
| return -ENODEV; |
| |
| lpass_add_stream(); |
| |
| mutex_lock(&ei.mutex); |
| |
| if (!eax_dai_any_tx_opened()) |
| ret = (*ei.master_dai_ops->startup)(substream, ei.master_dai); |
| |
| ci->opened = true; |
| |
| mutex_unlock(&ei.mutex); |
| |
| return ret; |
| } |
| |
| static void eax_dai_shutdown(struct snd_pcm_substream *substream, |
| struct snd_soc_dai *dai) |
| { |
| struct ch_info *ci = to_info(dai); |
| |
| if (!ei.master) |
| return; |
| |
| mutex_lock(&ei.mutex); |
| |
| ci->opened = false; |
| |
| if (!eax_dai_any_tx_opened()) |
| (*ei.master_dai_ops->shutdown)(substream, ei.master_dai); |
| |
| mutex_unlock(&ei.mutex); |
| |
| lpass_remove_stream(); |
| } |
| |
| static int eax_dai_probe(struct snd_soc_dai *dai) |
| { |
| pr_debug("%s\n", __func__); |
| |
| return 0; |
| } |
| |
| static int eax_dai_remove(struct snd_soc_dai *dai) |
| { |
| pr_debug("%s\n", __func__); |
| |
| return 0; |
| } |
| |
| static int eax_dai_suspend(struct snd_soc_dai *dai) |
| { |
| if (dai->active && ei.master_dai_suspend) |
| return (*ei.master_dai_suspend)(ei.master_dai); |
| |
| return 0; |
| } |
| |
| static int eax_dai_resume(struct snd_soc_dai *dai) |
| { |
| if (dai->active && ei.master_dai_resume) |
| return (*ei.master_dai_resume)(ei.master_dai); |
| |
| return 0; |
| } |
| |
| static const struct snd_soc_dai_ops eax_dai_ops = { |
| .trigger = eax_dai_trigger, |
| .hw_params = eax_dai_hw_params, |
| .set_fmt = eax_dai_set_fmt, |
| .set_clkdiv = eax_dai_set_clkdiv, |
| .set_sysclk = eax_dai_set_sysclk, |
| #ifdef CONFIG_SND_SOC_I2S_1840_TDM |
| .set_tdm_slot = eax_dai_set_tdm_slot, |
| #endif |
| .startup = eax_dai_startup, |
| .shutdown = eax_dai_shutdown, |
| }; |
| |
| static const struct snd_soc_component_driver eax_dai_component = { |
| .name = "samsung-eax", |
| }; |
| |
| static int eax_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_dai_component, &ci->dai_drv, 1); |
| eax_asoc_platform_register(dev); |
| |
| return 0; |
| } |
| |
| static int eax_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 Mixer driver, (c)2014 Samsung Electronics\n"; |
| |
| static int eax_probe(struct platform_device *pdev) |
| { |
| struct device *dev = &pdev->dev; |
| |
| printk(banner); |
| |
| mutex_init(&ei.mutex); |
| spin_lock_init(&ei.lock); |
| |
| ei.pdev = pdev; |
| |
| pm_runtime_enable(dev); |
| |
| return 0; |
| } |
| |
| static int eax_remove(struct platform_device *pdev) |
| { |
| pm_runtime_disable(&pdev->dev); |
| |
| return 0; |
| } |
| |
| #ifdef CONFIG_PM |
| static int eax_runtime_suspend(struct device *dev) |
| { |
| dev_dbg(dev, "%s entered\n", __func__); |
| |
| return 0; |
| } |
| |
| static int eax_runtime_resume(struct device *dev) |
| { |
| dev_dbg(dev, "%s entered\n", __func__); |
| |
| return 0; |
| } |
| #endif |
| |
| #if !defined(CONFIG_PM) && defined(CONFIG_PM_SLEEP) |
| static int eax_suspend(struct device *dev) |
| { |
| dev_dbg(dev, "%s entered\n", __func__); |
| |
| return 0; |
| } |
| |
| static int eax_resume(struct device *dev) |
| { |
| dev_dbg(dev, "%s entered\n", __func__); |
| |
| return 0; |
| } |
| #else |
| #define eax_suspend NULL |
| #define eax_resume NULL |
| #endif |
| |
| static struct platform_device_id eax_ch_driver_ids[] = { |
| { .name = "samsung-eax.0", }, |
| { .name = "samsung-eax.1", }, |
| { .name = "samsung-eax.2", }, |
| { .name = "samsung-eax.3", }, |
| { .name = "samsung-eax.4", }, |
| { .name = "samsung-eax.5", }, |
| { .name = "samsung-eax.6", }, |
| { .name = "samsung-eax.7", }, |
| {}, |
| }; |
| MODULE_DEVICE_TABLE(platform, eax_ch_driver_ids); |
| |
| static const struct dev_pm_ops eax_ch_pmops = { |
| SET_SYSTEM_SLEEP_PM_OPS( |
| eax_suspend, |
| eax_resume |
| ) |
| SET_RUNTIME_PM_OPS( |
| eax_runtime_suspend, |
| eax_runtime_resume, |
| NULL |
| ) |
| }; |
| |
| static struct platform_driver eax_dai_driver = { |
| .probe = eax_ch_probe, |
| .remove = eax_ch_remove, |
| .id_table = eax_ch_driver_ids, |
| .driver = { |
| .name = "samsung-eax", |
| .owner = THIS_MODULE, |
| .pm = &eax_ch_pmops, |
| }, |
| }; |
| |
| module_platform_driver(eax_dai_driver); |
| |
| static struct platform_device_id eax_driver_ids[] = { |
| { |
| .name = "samsung-amixer", |
| }, |
| {}, |
| }; |
| MODULE_DEVICE_TABLE(platform, eax_driver_ids); |
| |
| #ifdef CONFIG_OF |
| static const struct of_device_id exynos_eax_match[] = { |
| { |
| .compatible = "samsung,exynos-amixer", |
| }, |
| {}, |
| }; |
| MODULE_DEVICE_TABLE(of, exynos_eax_match); |
| #endif |
| |
| static struct platform_driver eax_driver = { |
| .probe = eax_probe, |
| .remove = eax_remove, |
| .id_table = eax_driver_ids, |
| .driver = { |
| .name = "samsung-amixer", |
| .owner = THIS_MODULE, |
| .of_match_table = of_match_ptr(exynos_eax_match), |
| }, |
| }; |
| |
| module_platform_driver(eax_driver); |
| |
| MODULE_AUTHOR("Yeongman Seo <yman.seo@samsung.com>"); |
| MODULE_DESCRIPTION("Exynos Audio Mixer driver"); |
| MODULE_LICENSE("GPL"); |