1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SoC audio for EDB93xx
4  *
5  * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru>
6  *
7  * This driver support CS4271 codec being master or slave, working
8  * in control port mode, connected either via SPI or I2C.
9  * The data format accepted is I2S or left-justified.
10  * DAPM support not implemented.
11  */
12 
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/soc/cirrus/ep93xx.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/soc.h>
19 #include <asm/mach-types.h>
20 
edb93xx_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)21 static int edb93xx_hw_params(struct snd_pcm_substream *substream,
22 			     struct snd_pcm_hw_params *params)
23 {
24 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
25 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
26 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
27 	int err;
28 	unsigned int mclk_rate;
29 	unsigned int rate = params_rate(params);
30 
31 	/*
32 	 * According to CS4271 datasheet we use MCLK/LRCK=256 for
33 	 * rates below 50kHz and 128 for higher sample rates
34 	 */
35 	if (rate < 50000)
36 		mclk_rate = rate * 64 * 4;
37 	else
38 		mclk_rate = rate * 64 * 2;
39 
40 	err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate,
41 				     SND_SOC_CLOCK_IN);
42 	if (err)
43 		return err;
44 
45 	return snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate,
46 				      SND_SOC_CLOCK_OUT);
47 }
48 
49 static const struct snd_soc_ops edb93xx_ops = {
50 	.hw_params	= edb93xx_hw_params,
51 };
52 
53 SND_SOC_DAILINK_DEFS(hifi,
54 	DAILINK_COMP_ARRAY(COMP_CPU("ep93xx-i2s")),
55 	DAILINK_COMP_ARRAY(COMP_CODEC("spi0.0", "cs4271-hifi")),
56 	DAILINK_COMP_ARRAY(COMP_PLATFORM("ep93xx-i2s")));
57 
58 static struct snd_soc_dai_link edb93xx_dai = {
59 	.name		= "CS4271",
60 	.stream_name	= "CS4271 HiFi",
61 	.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
62 			  SND_SOC_DAIFMT_CBC_CFC,
63 	.ops		= &edb93xx_ops,
64 	SND_SOC_DAILINK_REG(hifi),
65 };
66 
67 static struct snd_soc_card snd_soc_edb93xx = {
68 	.name		= "EDB93XX",
69 	.owner		= THIS_MODULE,
70 	.dai_link	= &edb93xx_dai,
71 	.num_links	= 1,
72 };
73 
edb93xx_probe(struct platform_device * pdev)74 static int edb93xx_probe(struct platform_device *pdev)
75 {
76 	struct snd_soc_card *card = &snd_soc_edb93xx;
77 	int ret;
78 
79 	ret = ep93xx_i2s_acquire();
80 	if (ret)
81 		return ret;
82 
83 	card->dev = &pdev->dev;
84 
85 	ret = snd_soc_register_card(card);
86 	if (ret) {
87 		dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
88 			ret);
89 		ep93xx_i2s_release();
90 	}
91 
92 	return ret;
93 }
94 
edb93xx_remove(struct platform_device * pdev)95 static void edb93xx_remove(struct platform_device *pdev)
96 {
97 	struct snd_soc_card *card = platform_get_drvdata(pdev);
98 
99 	snd_soc_unregister_card(card);
100 	ep93xx_i2s_release();
101 }
102 
103 static struct platform_driver edb93xx_driver = {
104 	.driver		= {
105 		.name	= "edb93xx-audio",
106 	},
107 	.probe		= edb93xx_probe,
108 	.remove_new	= edb93xx_remove,
109 };
110 
111 module_platform_driver(edb93xx_driver);
112 
113 MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>");
114 MODULE_DESCRIPTION("ALSA SoC EDB93xx");
115 MODULE_LICENSE("GPL");
116 MODULE_ALIAS("platform:edb93xx-audio");
117