xref: /linux/sound/soc/fsl/eukrea-tlv320.c (revision 3760befa5c08b229df76ab458520beeb26024716)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // eukrea-tlv320.c  --  SoC audio for eukrea_cpuimxXX in I2S mode
4 //
5 // Copyright 2010 Eric Bénard, Eukréa Electromatique <eric@eukrea.com>
6 //
7 // based on sound/soc/s3c24xx/s3c24xx_simtec_tlv320aic23.c
8 // which is Copyright 2009 Simtec Electronics
9 // and on sound/soc/imx/phycore-ac97.c which is
10 // Copyright 2009 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
11 
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/of.h>
16 #include <linux/of_platform.h>
17 #include <linux/device.h>
18 #include <linux/i2c.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/soc.h>
22 
23 #include "../codecs/tlv320aic23.h"
24 #include "imx-ssi.h"
25 #include "imx-audmux.h"
26 
27 #define CODEC_CLOCK 12000000
28 
29 static int eukrea_tlv320_hw_params(struct snd_pcm_substream *substream,
30 			    struct snd_pcm_hw_params *params)
31 {
32 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
33 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
34 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
35 	int ret;
36 
37 	ret = snd_soc_dai_set_sysclk(codec_dai, 0,
38 				     CODEC_CLOCK, SND_SOC_CLOCK_OUT);
39 	if (ret) {
40 		dev_err(cpu_dai->dev,
41 			"Failed to set the codec sysclk.\n");
42 		return ret;
43 	}
44 
45 	snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 0);
46 
47 	ret = snd_soc_dai_set_sysclk(cpu_dai, IMX_SSP_SYS_CLK, 0,
48 				SND_SOC_CLOCK_IN);
49 	/* fsl_ssi lacks the set_sysclk ops */
50 	if (ret && ret != -EINVAL) {
51 		dev_err(cpu_dai->dev,
52 			"Can't set the IMX_SSP_SYS_CLK CPU system clock.\n");
53 		return ret;
54 	}
55 
56 	return 0;
57 }
58 
59 static const struct snd_soc_ops eukrea_tlv320_snd_ops = {
60 	.hw_params	= eukrea_tlv320_hw_params,
61 };
62 
63 SND_SOC_DAILINK_DEFS(hifi,
64 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
65 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic23-hifi")),
66 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
67 
68 static struct snd_soc_dai_link eukrea_tlv320_dai = {
69 	.name		= "tlv320aic23",
70 	.stream_name	= "TLV320AIC23",
71 	.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
72 			  SND_SOC_DAIFMT_CBP_CFP,
73 	.ops		= &eukrea_tlv320_snd_ops,
74 	SND_SOC_DAILINK_REG(hifi),
75 };
76 
77 static struct snd_soc_card eukrea_tlv320 = {
78 	.owner		= THIS_MODULE,
79 	.dai_link	= &eukrea_tlv320_dai,
80 	.num_links	= 1,
81 };
82 
83 static int eukrea_tlv320_probe(struct platform_device *pdev)
84 {
85 	int ret;
86 	int int_port = 0, ext_port;
87 	struct device_node *np = pdev->dev.of_node;
88 	struct device_node *ssi_np = NULL, *codec_np = NULL, *tmp_np = NULL;
89 
90 	eukrea_tlv320.dev = &pdev->dev;
91 	if (np) {
92 		ret = snd_soc_of_parse_card_name(&eukrea_tlv320,
93 						 "eukrea,model");
94 		if (ret) {
95 			dev_err(&pdev->dev,
96 				"eukrea,model node missing or invalid.\n");
97 			goto err;
98 		}
99 
100 		ssi_np = of_parse_phandle(pdev->dev.of_node,
101 					  "ssi-controller", 0);
102 		if (!ssi_np) {
103 			dev_err(&pdev->dev,
104 				"ssi-controller missing or invalid.\n");
105 			ret = -ENODEV;
106 			goto err;
107 		}
108 
109 		codec_np = of_parse_phandle(ssi_np, "codec-handle", 0);
110 		if (codec_np)
111 			eukrea_tlv320_dai.codecs->of_node = codec_np;
112 		else
113 			dev_err(&pdev->dev, "codec-handle node missing or invalid.\n");
114 
115 		ret = of_property_read_u32(np, "fsl,mux-int-port", &int_port);
116 		if (ret) {
117 			dev_err(&pdev->dev,
118 				"fsl,mux-int-port node missing or invalid.\n");
119 			goto err;
120 		}
121 		ret = of_property_read_u32(np, "fsl,mux-ext-port", &ext_port);
122 		if (ret) {
123 			dev_err(&pdev->dev,
124 				"fsl,mux-ext-port node missing or invalid.\n");
125 			goto err;
126 		}
127 
128 		/*
129 		 * The port numbering in the hardware manual starts at 1, while
130 		 * the audmux API expects it starts at 0.
131 		 */
132 		int_port--;
133 		ext_port--;
134 
135 		eukrea_tlv320_dai.cpus->of_node = ssi_np;
136 		eukrea_tlv320_dai.platforms->of_node = ssi_np;
137 	} else {
138 		eukrea_tlv320_dai.cpus->dai_name = "imx-ssi.0";
139 		eukrea_tlv320_dai.platforms->name = "imx-ssi.0";
140 		eukrea_tlv320_dai.codecs->name = "tlv320aic23-codec.0-001a";
141 		eukrea_tlv320.name = "cpuimx-audio";
142 	}
143 
144 	if (of_machine_is_compatible("eukrea,cpuimx27") ||
145 	    (tmp_np = of_find_compatible_node(NULL, NULL, "fsl,imx21-audmux"))) {
146 		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR1_SSI0,
147 			IMX_AUDMUX_V1_PCR_SYN |
148 			IMX_AUDMUX_V1_PCR_TFSDIR |
149 			IMX_AUDMUX_V1_PCR_TCLKDIR |
150 			IMX_AUDMUX_V1_PCR_RFSDIR |
151 			IMX_AUDMUX_V1_PCR_RCLKDIR |
152 			IMX_AUDMUX_V1_PCR_TFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
153 			IMX_AUDMUX_V1_PCR_RFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
154 			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4)
155 		);
156 		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR3_SSI_PINS_4,
157 			IMX_AUDMUX_V1_PCR_SYN |
158 			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR1_SSI0)
159 		);
160 		of_node_put(tmp_np);
161 	} else if (of_machine_is_compatible("eukrea,cpuimx25") ||
162 		   of_machine_is_compatible("eukrea,cpuimx35") ||
163 		   of_machine_is_compatible("eukrea,cpuimx51") ||
164 		   (tmp_np = of_find_compatible_node(NULL, NULL, "fsl,imx31-audmux"))) {
165 		if (!np)
166 			ext_port = of_machine_is_compatible("eukrea,cpuimx25") ?
167 				4 : 3;
168 
169 		imx_audmux_v2_configure_port(int_port,
170 			IMX_AUDMUX_V2_PTCR_SYN |
171 			IMX_AUDMUX_V2_PTCR_TFSDIR |
172 			IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
173 			IMX_AUDMUX_V2_PTCR_TCLKDIR |
174 			IMX_AUDMUX_V2_PTCR_TCSEL(ext_port),
175 			IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)
176 		);
177 		imx_audmux_v2_configure_port(ext_port,
178 			IMX_AUDMUX_V2_PTCR_SYN,
179 			IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)
180 		);
181 		of_node_put(tmp_np);
182 	} else {
183 		if (np) {
184 			/* The eukrea,asoc-tlv320 driver was explicitly
185 			 * requested (through the device tree).
186 			 */
187 			dev_err(&pdev->dev,
188 				"Missing or invalid audmux DT node.\n");
189 			return -ENODEV;
190 		} else {
191 			/* Return happy.
192 			 * We might run on a totally different machine.
193 			 */
194 			return 0;
195 		}
196 	}
197 
198 	ret = devm_snd_soc_register_card(&pdev->dev, &eukrea_tlv320);
199 err:
200 	if (ret)
201 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
202 	of_node_put(ssi_np);
203 
204 	return ret;
205 }
206 
207 static const struct of_device_id imx_tlv320_dt_ids[] = {
208 	{ .compatible = "eukrea,asoc-tlv320"},
209 	{ /* sentinel */ }
210 };
211 MODULE_DEVICE_TABLE(of, imx_tlv320_dt_ids);
212 
213 static struct platform_driver eukrea_tlv320_driver = {
214 	.driver = {
215 		.name = "eukrea_tlv320",
216 		.of_match_table = imx_tlv320_dt_ids,
217 	},
218 	.probe = eukrea_tlv320_probe,
219 };
220 
221 module_platform_driver(eukrea_tlv320_driver);
222 
223 MODULE_AUTHOR("Eric Bénard <eric@eukrea.com>");
224 MODULE_DESCRIPTION("CPUIMX ALSA SoC driver");
225 MODULE_LICENSE("GPL");
226 MODULE_ALIAS("platform:eukrea_tlv320");
227