Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // Copyright (c) 2014, Insignal Co., Ltd.
0004 //
0005 //  Author: Claude <claude@insginal.co.kr>
0006 
0007 #include <linux/module.h>
0008 #include <linux/of_device.h>
0009 #include <linux/platform_device.h>
0010 #include <linux/clk.h>
0011 
0012 #include <sound/soc.h>
0013 #include <sound/soc-dapm.h>
0014 #include <sound/pcm.h>
0015 #include <sound/pcm_params.h>
0016 
0017 #include "../codecs/wm8994.h"
0018 #include "i2s.h"
0019 
0020 static int arndale_rt5631_hw_params(struct snd_pcm_substream *substream,
0021                     struct snd_pcm_hw_params *params)
0022 {
0023     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0024     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0025     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0026     int rfs, ret;
0027     unsigned long rclk;
0028 
0029     rfs = 256;
0030 
0031     rclk = params_rate(params) * rfs;
0032 
0033     ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
0034                     0, SND_SOC_CLOCK_OUT);
0035     if (ret < 0)
0036         return ret;
0037 
0038     ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_RCLKSRC_0,
0039                     0, SND_SOC_CLOCK_OUT);
0040 
0041     if (ret < 0)
0042         return ret;
0043 
0044     ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk, SND_SOC_CLOCK_OUT);
0045     if (ret < 0)
0046         return ret;
0047 
0048     return 0;
0049 }
0050 
0051 static const struct snd_soc_ops arndale_rt5631_ops = {
0052     .hw_params = arndale_rt5631_hw_params,
0053 };
0054 
0055 static int arndale_wm1811_hw_params(struct snd_pcm_substream *substream,
0056                     struct snd_pcm_hw_params *params)
0057 {
0058     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0059     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0060     unsigned int rfs, rclk;
0061 
0062     /* Ensure AIF1CLK is >= 3 MHz for optimal performance */
0063     if (params_width(params) == 24)
0064         rfs = 384;
0065     else if (params_rate(params) == 8000 || params_rate(params) == 11025)
0066         rfs = 512;
0067     else
0068         rfs = 256;
0069 
0070     rclk = params_rate(params) * rfs;
0071 
0072     /*
0073      * We add 1 to the frequency value to ensure proper EPLL setting
0074      * for each audio sampling rate (see epll_24mhz_tbl in drivers/clk/
0075      * samsung/clk-exynos5250.c for list of available EPLL rates).
0076      * The CODEC uses clk API and the value will be rounded hence the MCLK1
0077      * clock's frequency will still be exact multiple of the sample rate.
0078      */
0079     return snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK1,
0080                     rclk + 1, SND_SOC_CLOCK_IN);
0081 }
0082 
0083 static const struct snd_soc_ops arndale_wm1811_ops = {
0084     .hw_params = arndale_wm1811_hw_params,
0085 };
0086 
0087 SND_SOC_DAILINK_DEFS(rt5631_hifi,
0088     DAILINK_COMP_ARRAY(COMP_EMPTY()),
0089     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5631-aif1")),
0090     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0091 
0092 static struct snd_soc_dai_link arndale_rt5631_dai[] = {
0093     {
0094         .name = "RT5631 HiFi",
0095         .stream_name = "Primary",
0096         .dai_fmt = SND_SOC_DAIFMT_I2S
0097             | SND_SOC_DAIFMT_NB_NF
0098             | SND_SOC_DAIFMT_CBS_CFS,
0099         .ops = &arndale_rt5631_ops,
0100         SND_SOC_DAILINK_REG(rt5631_hifi),
0101     },
0102 };
0103 
0104 SND_SOC_DAILINK_DEFS(wm1811_hifi,
0105     DAILINK_COMP_ARRAY(COMP_EMPTY()),
0106     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif1")),
0107     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0108 
0109 static struct snd_soc_dai_link arndale_wm1811_dai[] = {
0110     {
0111         .name = "WM1811 HiFi",
0112         .stream_name = "Primary",
0113         .dai_fmt = SND_SOC_DAIFMT_I2S
0114             | SND_SOC_DAIFMT_NB_NF
0115             | SND_SOC_DAIFMT_CBM_CFM,
0116         .ops = &arndale_wm1811_ops,
0117         SND_SOC_DAILINK_REG(wm1811_hifi),
0118     },
0119 };
0120 
0121 static struct snd_soc_card arndale_rt5631 = {
0122     .name = "Arndale RT5631",
0123     .owner = THIS_MODULE,
0124     .dai_link = arndale_rt5631_dai,
0125     .num_links = ARRAY_SIZE(arndale_rt5631_dai),
0126 };
0127 
0128 static struct snd_soc_card arndale_wm1811 = {
0129     .name = "Arndale WM1811",
0130     .owner = THIS_MODULE,
0131     .dai_link = arndale_wm1811_dai,
0132     .num_links = ARRAY_SIZE(arndale_wm1811_dai),
0133 };
0134 
0135 static void arndale_put_of_nodes(struct snd_soc_card *card)
0136 {
0137     struct snd_soc_dai_link *dai_link;
0138     int i;
0139 
0140     for_each_card_prelinks(card, i, dai_link) {
0141         of_node_put(dai_link->cpus->of_node);
0142         of_node_put(dai_link->codecs->of_node);
0143     }
0144 }
0145 
0146 static int arndale_audio_probe(struct platform_device *pdev)
0147 {
0148     struct device_node *np = pdev->dev.of_node;
0149     struct snd_soc_card *card;
0150     struct snd_soc_dai_link *dai_link;
0151     int ret;
0152 
0153     card = (struct snd_soc_card *)of_device_get_match_data(&pdev->dev);
0154     card->dev = &pdev->dev;
0155     dai_link = card->dai_link;
0156 
0157     dai_link->cpus->of_node = of_parse_phandle(np, "samsung,audio-cpu", 0);
0158     if (!dai_link->cpus->of_node) {
0159         dev_err(&pdev->dev,
0160             "Property 'samsung,audio-cpu' missing or invalid\n");
0161         return -EINVAL;
0162     }
0163 
0164     if (!dai_link->platforms->name)
0165         dai_link->platforms->of_node = dai_link->cpus->of_node;
0166 
0167     dai_link->codecs->of_node = of_parse_phandle(np, "samsung,audio-codec", 0);
0168     if (!dai_link->codecs->of_node) {
0169         dev_err(&pdev->dev,
0170             "Property 'samsung,audio-codec' missing or invalid\n");
0171         ret = -EINVAL;
0172         goto err_put_of_nodes;
0173     }
0174 
0175     ret = devm_snd_soc_register_card(card->dev, card);
0176     if (ret) {
0177         dev_err_probe(&pdev->dev, ret,
0178                   "snd_soc_register_card() failed\n");
0179         goto err_put_of_nodes;
0180     }
0181     return 0;
0182 
0183 err_put_of_nodes:
0184     arndale_put_of_nodes(card);
0185     return ret;
0186 }
0187 
0188 static int arndale_audio_remove(struct platform_device *pdev)
0189 {
0190     struct snd_soc_card *card = platform_get_drvdata(pdev);
0191 
0192     arndale_put_of_nodes(card);
0193     return 0;
0194 }
0195 
0196 static const struct of_device_id arndale_audio_of_match[] = {
0197     { .compatible = "samsung,arndale-rt5631",  .data = &arndale_rt5631 },
0198     { .compatible = "samsung,arndale-alc5631", .data = &arndale_rt5631 },
0199     { .compatible = "samsung,arndale-wm1811",  .data = &arndale_wm1811 },
0200     {},
0201 };
0202 MODULE_DEVICE_TABLE(of, arndale_audio_of_match);
0203 
0204 static struct platform_driver arndale_audio_driver = {
0205     .driver = {
0206         .name = "arndale-audio",
0207         .pm = &snd_soc_pm_ops,
0208         .of_match_table = arndale_audio_of_match,
0209     },
0210     .probe = arndale_audio_probe,
0211     .remove = arndale_audio_remove,
0212 };
0213 
0214 module_platform_driver(arndale_audio_driver);
0215 
0216 MODULE_AUTHOR("Claude <claude@insignal.co.kr>");
0217 MODULE_DESCRIPTION("ALSA SoC Driver for Arndale Board");
0218 MODULE_LICENSE("GPL");