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
snow_card_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)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
snow_late_probe(struct snd_soc_card * card)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. */
1127de6b6bcSKuninori Morimoto codec_dai = asoc_rtd_to_codec(rtd, 0);
1135015920aSMengdong Lin
11431c26a6aSTushar Behera /* Set the MCLK rate for the codec */
115aff8d2beSSylwester Nawrocki return snd_soc_dai_set_sysclk(codec_dai, 0,
11631c26a6aSTushar Behera FIN_PLL_RATE, SND_SOC_CLOCK_IN);
11731c26a6aSTushar Behera }
11831c26a6aSTushar Behera
11931c26a6aSTushar Behera static struct snd_soc_card snow_snd = {
12031c26a6aSTushar Behera .name = "Snow-I2S",
12154d8697fSAxel Lin .owner = THIS_MODULE,
12231c26a6aSTushar Behera .late_probe = snow_late_probe,
12331c26a6aSTushar Behera };
12431c26a6aSTushar Behera
snow_probe(struct platform_device * pdev)12531c26a6aSTushar Behera static int snow_probe(struct platform_device *pdev)
12631c26a6aSTushar Behera {
127aff8d2beSSylwester Nawrocki struct device *dev = &pdev->dev;
12831c26a6aSTushar Behera struct snd_soc_card *card = &snow_snd;
129aff8d2beSSylwester Nawrocki struct device_node *cpu, *codec;
130aff8d2beSSylwester Nawrocki struct snd_soc_dai_link *link;
131aff8d2beSSylwester Nawrocki struct snow_priv *priv;
132aff8d2beSSylwester Nawrocki int ret;
13331c26a6aSTushar Behera
134aff8d2beSSylwester Nawrocki priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
135aff8d2beSSylwester Nawrocki if (!priv)
136aff8d2beSSylwester Nawrocki return -ENOMEM;
137aff8d2beSSylwester Nawrocki
138aff8d2beSSylwester Nawrocki link = &priv->dai_link;
139aff8d2beSSylwester Nawrocki
140aff8d2beSSylwester Nawrocki link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
141aff8d2beSSylwester Nawrocki SND_SOC_DAIFMT_CBS_CFS;
142aff8d2beSSylwester Nawrocki
143aff8d2beSSylwester Nawrocki link->name = "Primary";
144aff8d2beSSylwester Nawrocki link->stream_name = link->name;
145aff8d2beSSylwester Nawrocki
146db1623faSKuninori Morimoto link->cpus = links_cpus;
147db1623faSKuninori Morimoto link->num_cpus = ARRAY_SIZE(links_cpus);
148db1623faSKuninori Morimoto link->codecs = links_codecs;
149db1623faSKuninori Morimoto link->num_codecs = ARRAY_SIZE(links_codecs);
1503dfc3e9cSKuninori Morimoto link->platforms = links_platforms;
1513dfc3e9cSKuninori Morimoto link->num_platforms = ARRAY_SIZE(links_platforms);
152db1623faSKuninori Morimoto
153aff8d2beSSylwester Nawrocki card->dai_link = link;
154aff8d2beSSylwester Nawrocki card->num_links = 1;
155aff8d2beSSylwester Nawrocki card->dev = dev;
156aff8d2beSSylwester Nawrocki
157aff8d2beSSylwester Nawrocki /* Try new DT bindings with HDMI support first. */
158aff8d2beSSylwester Nawrocki cpu = of_get_child_by_name(dev->of_node, "cpu");
159aff8d2beSSylwester Nawrocki
160aff8d2beSSylwester Nawrocki if (cpu) {
161aff8d2beSSylwester Nawrocki link->ops = &snow_card_ops;
162aff8d2beSSylwester Nawrocki
163db1623faSKuninori Morimoto link->cpus->of_node = of_parse_phandle(cpu, "sound-dai", 0);
164aff8d2beSSylwester Nawrocki of_node_put(cpu);
165aff8d2beSSylwester Nawrocki
166db1623faSKuninori Morimoto if (!link->cpus->of_node) {
167aff8d2beSSylwester Nawrocki dev_err(dev, "Failed parsing cpu/sound-dai property\n");
168aff8d2beSSylwester Nawrocki return -EINVAL;
169aff8d2beSSylwester Nawrocki }
170aff8d2beSSylwester Nawrocki
171aff8d2beSSylwester Nawrocki codec = of_get_child_by_name(dev->of_node, "codec");
172aff8d2beSSylwester Nawrocki ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
173aff8d2beSSylwester Nawrocki of_node_put(codec);
174aff8d2beSSylwester Nawrocki
175aff8d2beSSylwester Nawrocki if (ret < 0) {
176db1623faSKuninori Morimoto of_node_put(link->cpus->of_node);
177aff8d2beSSylwester Nawrocki dev_err(dev, "Failed parsing codec node\n");
178aff8d2beSSylwester Nawrocki return ret;
179aff8d2beSSylwester Nawrocki }
180aff8d2beSSylwester Nawrocki
181db1623faSKuninori Morimoto priv->clk_i2s_bus = of_clk_get_by_name(link->cpus->of_node,
182aff8d2beSSylwester Nawrocki "i2s_opclk0");
183aff8d2beSSylwester Nawrocki if (IS_ERR(priv->clk_i2s_bus)) {
184aff8d2beSSylwester Nawrocki snd_soc_of_put_dai_link_codecs(link);
185db1623faSKuninori Morimoto of_node_put(link->cpus->of_node);
186aff8d2beSSylwester Nawrocki return PTR_ERR(priv->clk_i2s_bus);
187aff8d2beSSylwester Nawrocki }
188aff8d2beSSylwester Nawrocki } else {
18940faaca0SJulia Lawall link->codecs->dai_name = "HiFi";
190aff8d2beSSylwester Nawrocki
191db1623faSKuninori Morimoto link->cpus->of_node = of_parse_phandle(dev->of_node,
19231c26a6aSTushar Behera "samsung,i2s-controller", 0);
193db1623faSKuninori Morimoto if (!link->cpus->of_node) {
194aff8d2beSSylwester Nawrocki dev_err(dev, "i2s-controller property parse error\n");
19531c26a6aSTushar Behera return -EINVAL;
19631c26a6aSTushar Behera }
19731c26a6aSTushar Behera
198db1623faSKuninori Morimoto link->codecs->of_node = of_parse_phandle(dev->of_node,
19931c26a6aSTushar Behera "samsung,audio-codec", 0);
200db1623faSKuninori Morimoto if (!link->codecs->of_node) {
201db1623faSKuninori Morimoto of_node_put(link->cpus->of_node);
202aff8d2beSSylwester Nawrocki dev_err(dev, "audio-codec property parse error\n");
20331c26a6aSTushar Behera return -EINVAL;
20431c26a6aSTushar Behera }
20531c26a6aSTushar Behera }
20631c26a6aSTushar Behera
2073dfc3e9cSKuninori Morimoto link->platforms->of_node = link->cpus->of_node;
2083dfc3e9cSKuninori Morimoto
20900ad93e2STushar Behera /* Update card-name if provided through DT, else use default name */
21000ad93e2STushar Behera snd_soc_of_parse_card_name(card, "samsung,model");
21100ad93e2STushar Behera
212aff8d2beSSylwester Nawrocki snd_soc_card_set_drvdata(card, priv);
213aff8d2beSSylwester Nawrocki
214aff8d2beSSylwester Nawrocki ret = devm_snd_soc_register_card(dev, card);
21527c6eaebSKuninori Morimoto if (ret)
21627c6eaebSKuninori Morimoto return dev_err_probe(&pdev->dev, ret,
21727c6eaebSKuninori Morimoto "snd_soc_register_card failed\n");
21831c26a6aSTushar Behera
2190c57064eSPierre-Louis Bossart return 0;
22031c26a6aSTushar Behera }
22131c26a6aSTushar Behera
snow_remove(struct platform_device * pdev)222*3224f440SUwe Kleine-König static void snow_remove(struct platform_device *pdev)
223aff8d2beSSylwester Nawrocki {
224aff8d2beSSylwester Nawrocki struct snow_priv *priv = platform_get_drvdata(pdev);
225aff8d2beSSylwester Nawrocki struct snd_soc_dai_link *link = &priv->dai_link;
226aff8d2beSSylwester Nawrocki
227db1623faSKuninori Morimoto of_node_put(link->cpus->of_node);
228db1623faSKuninori Morimoto of_node_put(link->codecs->of_node);
229aff8d2beSSylwester Nawrocki snd_soc_of_put_dai_link_codecs(link);
230aff8d2beSSylwester Nawrocki
231aff8d2beSSylwester Nawrocki clk_put(priv->clk_i2s_bus);
232aff8d2beSSylwester Nawrocki }
233aff8d2beSSylwester Nawrocki
23431c26a6aSTushar Behera static const struct of_device_id snow_of_match[] = {
23531c26a6aSTushar Behera { .compatible = "google,snow-audio-max98090", },
23646aed597STushar Behera { .compatible = "google,snow-audio-max98091", },
23731c26a6aSTushar Behera { .compatible = "google,snow-audio-max98095", },
23831c26a6aSTushar Behera {},
23931c26a6aSTushar Behera };
24062e6a3b6SAndreas Färber MODULE_DEVICE_TABLE(of, snow_of_match);
24131c26a6aSTushar Behera
24231c26a6aSTushar Behera static struct platform_driver snow_driver = {
24331c26a6aSTushar Behera .driver = {
24431c26a6aSTushar Behera .name = "snow-audio",
245deeaa686STushar Behera .pm = &snd_soc_pm_ops,
24631c26a6aSTushar Behera .of_match_table = snow_of_match,
24731c26a6aSTushar Behera },
24831c26a6aSTushar Behera .probe = snow_probe,
249*3224f440SUwe Kleine-König .remove_new = snow_remove,
25031c26a6aSTushar Behera };
25131c26a6aSTushar Behera
25231c26a6aSTushar Behera module_platform_driver(snow_driver);
25331c26a6aSTushar Behera
25431c26a6aSTushar Behera MODULE_DESCRIPTION("ALSA SoC Audio machine driver for Snow");
25531c26a6aSTushar Behera MODULE_LICENSE("GPL");
256