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