1ebf4c423SSylwester Nawrocki // SPDX-License-Identifier: GPL-2.0 2ebf4c423SSylwester Nawrocki // 3ebf4c423SSylwester Nawrocki // ASoC machine driver for Snow boards 431c26a6aSTushar Behera 5aff8d2beSSylwester Nawrocki #include <linux/clk.h> 631c26a6aSTushar Behera #include <linux/module.h> 731c26a6aSTushar Behera #include <linux/platform_device.h> 831c26a6aSTushar Behera #include <linux/of.h> 931c26a6aSTushar Behera #include <linux/of_device.h> 10aff8d2beSSylwester Nawrocki #include <sound/pcm_params.h> 1131c26a6aSTushar Behera #include <sound/soc.h> 1231c26a6aSTushar Behera 1331c26a6aSTushar Behera #include "i2s.h" 1431c26a6aSTushar Behera 1531c26a6aSTushar Behera #define FIN_PLL_RATE 24000000 1631c26a6aSTushar Behera 173dfc3e9cSKuninori Morimoto SND_SOC_DAILINK_DEFS(links, 183dfc3e9cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_EMPTY()), 193dfc3e9cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_EMPTY()), 20db1623faSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_EMPTY())); 21db1623faSKuninori Morimoto 22aff8d2beSSylwester Nawrocki struct snow_priv { 23aff8d2beSSylwester Nawrocki struct snd_soc_dai_link dai_link; 24aff8d2beSSylwester Nawrocki struct clk *clk_i2s_bus; 25aff8d2beSSylwester Nawrocki }; 26aff8d2beSSylwester Nawrocki 27aff8d2beSSylwester Nawrocki static int snow_card_hw_params(struct snd_pcm_substream *substream, 28aff8d2beSSylwester Nawrocki struct snd_pcm_hw_params *params) 2931c26a6aSTushar Behera { 30aff8d2beSSylwester Nawrocki static const unsigned int pll_rate[] = { 31aff8d2beSSylwester Nawrocki 73728000U, 67737602U, 49152000U, 45158401U, 32768001U 32aff8d2beSSylwester Nawrocki }; 33c101ce88SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 34aff8d2beSSylwester Nawrocki struct snow_priv *priv = snd_soc_card_get_drvdata(rtd->card); 35aff8d2beSSylwester Nawrocki int bfs, psr, rfs, bitwidth; 36aff8d2beSSylwester Nawrocki unsigned long int rclk; 37aff8d2beSSylwester Nawrocki long int freq = -EINVAL; 38aff8d2beSSylwester Nawrocki int ret, i; 39aff8d2beSSylwester Nawrocki 40aff8d2beSSylwester Nawrocki bitwidth = snd_pcm_format_width(params_format(params)); 41aff8d2beSSylwester Nawrocki if (bitwidth < 0) { 42aff8d2beSSylwester Nawrocki dev_err(rtd->card->dev, "Invalid bit-width: %d\n", bitwidth); 43aff8d2beSSylwester Nawrocki return bitwidth; 44aff8d2beSSylwester Nawrocki } 45aff8d2beSSylwester Nawrocki 46aff8d2beSSylwester Nawrocki if (bitwidth != 16 && bitwidth != 24) { 47aff8d2beSSylwester Nawrocki dev_err(rtd->card->dev, "Unsupported bit-width: %d\n", bitwidth); 48aff8d2beSSylwester Nawrocki return -EINVAL; 49aff8d2beSSylwester Nawrocki } 50aff8d2beSSylwester Nawrocki 51aff8d2beSSylwester Nawrocki bfs = 2 * bitwidth; 52aff8d2beSSylwester Nawrocki 53aff8d2beSSylwester Nawrocki switch (params_rate(params)) { 54aff8d2beSSylwester Nawrocki case 16000: 55aff8d2beSSylwester Nawrocki case 22050: 56aff8d2beSSylwester Nawrocki case 24000: 57aff8d2beSSylwester Nawrocki case 32000: 58aff8d2beSSylwester Nawrocki case 44100: 59aff8d2beSSylwester Nawrocki case 48000: 60aff8d2beSSylwester Nawrocki case 88200: 61aff8d2beSSylwester Nawrocki case 96000: 62aff8d2beSSylwester Nawrocki rfs = 8 * bfs; 63aff8d2beSSylwester Nawrocki break; 64aff8d2beSSylwester Nawrocki case 64000: 65aff8d2beSSylwester Nawrocki rfs = 384; 66aff8d2beSSylwester Nawrocki break; 67aff8d2beSSylwester Nawrocki case 8000: 68aff8d2beSSylwester Nawrocki case 11025: 69aff8d2beSSylwester Nawrocki case 12000: 70aff8d2beSSylwester Nawrocki rfs = 16 * bfs; 71aff8d2beSSylwester Nawrocki break; 72aff8d2beSSylwester Nawrocki default: 73aff8d2beSSylwester Nawrocki return -EINVAL; 74aff8d2beSSylwester Nawrocki } 75aff8d2beSSylwester Nawrocki 76aff8d2beSSylwester Nawrocki rclk = params_rate(params) * rfs; 77aff8d2beSSylwester Nawrocki 78aff8d2beSSylwester Nawrocki for (psr = 8; psr > 0; psr /= 2) { 79aff8d2beSSylwester Nawrocki for (i = 0; i < ARRAY_SIZE(pll_rate); i++) { 80aff8d2beSSylwester Nawrocki if ((pll_rate[i] - rclk * psr) <= 2) { 81aff8d2beSSylwester Nawrocki freq = pll_rate[i]; 82aff8d2beSSylwester Nawrocki break; 83aff8d2beSSylwester Nawrocki } 84aff8d2beSSylwester Nawrocki } 85aff8d2beSSylwester Nawrocki } 86aff8d2beSSylwester Nawrocki if (freq < 0) { 87aff8d2beSSylwester Nawrocki dev_err(rtd->card->dev, "Unsupported RCLK rate: %lu\n", rclk); 88aff8d2beSSylwester Nawrocki return -EINVAL; 89aff8d2beSSylwester Nawrocki } 90aff8d2beSSylwester Nawrocki 91aff8d2beSSylwester Nawrocki ret = clk_set_rate(priv->clk_i2s_bus, freq); 92aff8d2beSSylwester Nawrocki if (ret < 0) { 93aff8d2beSSylwester Nawrocki dev_err(rtd->card->dev, "I2S bus clock rate set failed\n"); 94aff8d2beSSylwester Nawrocki return ret; 95aff8d2beSSylwester Nawrocki } 96aff8d2beSSylwester Nawrocki 97aff8d2beSSylwester Nawrocki return 0; 98aff8d2beSSylwester Nawrocki } 99aff8d2beSSylwester Nawrocki 100aff8d2beSSylwester Nawrocki static const struct snd_soc_ops snow_card_ops = { 101aff8d2beSSylwester Nawrocki .hw_params = snow_card_hw_params, 10231c26a6aSTushar Behera }; 10331c26a6aSTushar Behera 10431c26a6aSTushar Behera static int snow_late_probe(struct snd_soc_card *card) 10531c26a6aSTushar Behera { 1065015920aSMengdong Lin struct snd_soc_pcm_runtime *rtd; 1075015920aSMengdong Lin struct snd_soc_dai *codec_dai; 10831c26a6aSTushar Behera 1094468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); 110aff8d2beSSylwester Nawrocki 111aff8d2beSSylwester Nawrocki /* In the multi-codec case codec_dais 0 is MAX98095 and 1 is HDMI. */ 112aff8d2beSSylwester Nawrocki if (rtd->num_codecs > 1) 1137de6b6bcSKuninori Morimoto codec_dai = asoc_rtd_to_codec(rtd, 0); 114aff8d2beSSylwester Nawrocki else 1157de6b6bcSKuninori Morimoto codec_dai = asoc_rtd_to_codec(rtd, 0); 1165015920aSMengdong Lin 11731c26a6aSTushar Behera /* Set the MCLK rate for the codec */ 118aff8d2beSSylwester Nawrocki return snd_soc_dai_set_sysclk(codec_dai, 0, 11931c26a6aSTushar Behera FIN_PLL_RATE, SND_SOC_CLOCK_IN); 12031c26a6aSTushar Behera } 12131c26a6aSTushar Behera 12231c26a6aSTushar Behera static struct snd_soc_card snow_snd = { 12331c26a6aSTushar Behera .name = "Snow-I2S", 12454d8697fSAxel Lin .owner = THIS_MODULE, 12531c26a6aSTushar Behera .late_probe = snow_late_probe, 12631c26a6aSTushar Behera }; 12731c26a6aSTushar Behera 12831c26a6aSTushar Behera static int snow_probe(struct platform_device *pdev) 12931c26a6aSTushar Behera { 130aff8d2beSSylwester Nawrocki struct device *dev = &pdev->dev; 13131c26a6aSTushar Behera struct snd_soc_card *card = &snow_snd; 132aff8d2beSSylwester Nawrocki struct device_node *cpu, *codec; 133aff8d2beSSylwester Nawrocki struct snd_soc_dai_link *link; 134aff8d2beSSylwester Nawrocki struct snow_priv *priv; 135aff8d2beSSylwester Nawrocki int ret; 13631c26a6aSTushar Behera 137aff8d2beSSylwester Nawrocki priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 138aff8d2beSSylwester Nawrocki if (!priv) 139aff8d2beSSylwester Nawrocki return -ENOMEM; 140aff8d2beSSylwester Nawrocki 141aff8d2beSSylwester Nawrocki link = &priv->dai_link; 142aff8d2beSSylwester Nawrocki 143aff8d2beSSylwester Nawrocki link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 144aff8d2beSSylwester Nawrocki SND_SOC_DAIFMT_CBS_CFS; 145aff8d2beSSylwester Nawrocki 146aff8d2beSSylwester Nawrocki link->name = "Primary"; 147aff8d2beSSylwester Nawrocki link->stream_name = link->name; 148aff8d2beSSylwester Nawrocki 149db1623faSKuninori Morimoto link->cpus = links_cpus; 150db1623faSKuninori Morimoto link->num_cpus = ARRAY_SIZE(links_cpus); 151db1623faSKuninori Morimoto link->codecs = links_codecs; 152db1623faSKuninori Morimoto link->num_codecs = ARRAY_SIZE(links_codecs); 1533dfc3e9cSKuninori Morimoto link->platforms = links_platforms; 1543dfc3e9cSKuninori Morimoto link->num_platforms = ARRAY_SIZE(links_platforms); 155db1623faSKuninori Morimoto 156aff8d2beSSylwester Nawrocki card->dai_link = link; 157aff8d2beSSylwester Nawrocki card->num_links = 1; 158aff8d2beSSylwester Nawrocki card->dev = dev; 159aff8d2beSSylwester Nawrocki 160aff8d2beSSylwester Nawrocki /* Try new DT bindings with HDMI support first. */ 161aff8d2beSSylwester Nawrocki cpu = of_get_child_by_name(dev->of_node, "cpu"); 162aff8d2beSSylwester Nawrocki 163aff8d2beSSylwester Nawrocki if (cpu) { 164aff8d2beSSylwester Nawrocki link->ops = &snow_card_ops; 165aff8d2beSSylwester Nawrocki 166db1623faSKuninori Morimoto link->cpus->of_node = of_parse_phandle(cpu, "sound-dai", 0); 167aff8d2beSSylwester Nawrocki of_node_put(cpu); 168aff8d2beSSylwester Nawrocki 169db1623faSKuninori Morimoto if (!link->cpus->of_node) { 170aff8d2beSSylwester Nawrocki dev_err(dev, "Failed parsing cpu/sound-dai property\n"); 171aff8d2beSSylwester Nawrocki return -EINVAL; 172aff8d2beSSylwester Nawrocki } 173aff8d2beSSylwester Nawrocki 174aff8d2beSSylwester Nawrocki codec = of_get_child_by_name(dev->of_node, "codec"); 175aff8d2beSSylwester Nawrocki ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); 176aff8d2beSSylwester Nawrocki of_node_put(codec); 177aff8d2beSSylwester Nawrocki 178aff8d2beSSylwester Nawrocki if (ret < 0) { 179db1623faSKuninori Morimoto of_node_put(link->cpus->of_node); 180aff8d2beSSylwester Nawrocki dev_err(dev, "Failed parsing codec node\n"); 181aff8d2beSSylwester Nawrocki return ret; 182aff8d2beSSylwester Nawrocki } 183aff8d2beSSylwester Nawrocki 184db1623faSKuninori Morimoto priv->clk_i2s_bus = of_clk_get_by_name(link->cpus->of_node, 185aff8d2beSSylwester Nawrocki "i2s_opclk0"); 186aff8d2beSSylwester Nawrocki if (IS_ERR(priv->clk_i2s_bus)) { 187aff8d2beSSylwester Nawrocki snd_soc_of_put_dai_link_codecs(link); 188db1623faSKuninori Morimoto of_node_put(link->cpus->of_node); 189aff8d2beSSylwester Nawrocki return PTR_ERR(priv->clk_i2s_bus); 190aff8d2beSSylwester Nawrocki } 191aff8d2beSSylwester Nawrocki } else { 192*40faaca0SJulia Lawall link->codecs->dai_name = "HiFi"; 193aff8d2beSSylwester Nawrocki 194db1623faSKuninori Morimoto link->cpus->of_node = of_parse_phandle(dev->of_node, 19531c26a6aSTushar Behera "samsung,i2s-controller", 0); 196db1623faSKuninori Morimoto if (!link->cpus->of_node) { 197aff8d2beSSylwester Nawrocki dev_err(dev, "i2s-controller property parse error\n"); 19831c26a6aSTushar Behera return -EINVAL; 19931c26a6aSTushar Behera } 20031c26a6aSTushar Behera 201db1623faSKuninori Morimoto link->codecs->of_node = of_parse_phandle(dev->of_node, 20231c26a6aSTushar Behera "samsung,audio-codec", 0); 203db1623faSKuninori Morimoto if (!link->codecs->of_node) { 204db1623faSKuninori Morimoto of_node_put(link->cpus->of_node); 205aff8d2beSSylwester Nawrocki dev_err(dev, "audio-codec property parse error\n"); 20631c26a6aSTushar Behera return -EINVAL; 20731c26a6aSTushar Behera } 20831c26a6aSTushar Behera } 20931c26a6aSTushar Behera 2103dfc3e9cSKuninori Morimoto link->platforms->of_node = link->cpus->of_node; 2113dfc3e9cSKuninori Morimoto 21200ad93e2STushar Behera /* Update card-name if provided through DT, else use default name */ 21300ad93e2STushar Behera snd_soc_of_parse_card_name(card, "samsung,model"); 21400ad93e2STushar Behera 215aff8d2beSSylwester Nawrocki snd_soc_card_set_drvdata(card, priv); 216aff8d2beSSylwester Nawrocki 217aff8d2beSSylwester Nawrocki ret = devm_snd_soc_register_card(dev, card); 21831c26a6aSTushar Behera if (ret) { 2191a1b3743SMarek Szyprowski if (ret != -EPROBE_DEFER) 2201a1b3743SMarek Szyprowski dev_err(&pdev->dev, 2211a1b3743SMarek Szyprowski "snd_soc_register_card failed (%d)\n", ret); 22231c26a6aSTushar Behera return ret; 22331c26a6aSTushar Behera } 22431c26a6aSTushar Behera 22531c26a6aSTushar Behera return ret; 22631c26a6aSTushar Behera } 22731c26a6aSTushar Behera 228aff8d2beSSylwester Nawrocki static int snow_remove(struct platform_device *pdev) 229aff8d2beSSylwester Nawrocki { 230aff8d2beSSylwester Nawrocki struct snow_priv *priv = platform_get_drvdata(pdev); 231aff8d2beSSylwester Nawrocki struct snd_soc_dai_link *link = &priv->dai_link; 232aff8d2beSSylwester Nawrocki 233db1623faSKuninori Morimoto of_node_put(link->cpus->of_node); 234db1623faSKuninori Morimoto of_node_put(link->codecs->of_node); 235aff8d2beSSylwester Nawrocki snd_soc_of_put_dai_link_codecs(link); 236aff8d2beSSylwester Nawrocki 237aff8d2beSSylwester Nawrocki clk_put(priv->clk_i2s_bus); 238aff8d2beSSylwester Nawrocki 239aff8d2beSSylwester Nawrocki return 0; 240aff8d2beSSylwester Nawrocki } 241aff8d2beSSylwester Nawrocki 24231c26a6aSTushar Behera static const struct of_device_id snow_of_match[] = { 24331c26a6aSTushar Behera { .compatible = "google,snow-audio-max98090", }, 24446aed597STushar Behera { .compatible = "google,snow-audio-max98091", }, 24531c26a6aSTushar Behera { .compatible = "google,snow-audio-max98095", }, 24631c26a6aSTushar Behera {}, 24731c26a6aSTushar Behera }; 24862e6a3b6SAndreas Färber MODULE_DEVICE_TABLE(of, snow_of_match); 24931c26a6aSTushar Behera 25031c26a6aSTushar Behera static struct platform_driver snow_driver = { 25131c26a6aSTushar Behera .driver = { 25231c26a6aSTushar Behera .name = "snow-audio", 253deeaa686STushar Behera .pm = &snd_soc_pm_ops, 25431c26a6aSTushar Behera .of_match_table = snow_of_match, 25531c26a6aSTushar Behera }, 25631c26a6aSTushar Behera .probe = snow_probe, 257aff8d2beSSylwester Nawrocki .remove = snow_remove, 25831c26a6aSTushar Behera }; 25931c26a6aSTushar Behera 26031c26a6aSTushar Behera module_platform_driver(snow_driver); 26131c26a6aSTushar Behera 26231c26a6aSTushar Behera MODULE_DESCRIPTION("ALSA SoC Audio machine driver for Snow"); 26331c26a6aSTushar Behera MODULE_LICENSE("GPL"); 264