xref: /openbmc/linux/sound/soc/fsl/eukrea-tlv320.c (revision 2058ea1c)
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 SND_SOC_DAILINK_DEFS(hifi,
65 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
66 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic23-hifi")));
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_CBM_CFM,
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;
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 	} else {
137 		eukrea_tlv320_dai.cpus->dai_name = "imx-ssi.0";
138 		eukrea_tlv320_dai.codecs->name = "tlv320aic23-codec.0-001a";
139 		eukrea_tlv320.name = "cpuimx-audio";
140 	}
141 
142 	if (machine_is_eukrea_cpuimx27() ||
143 	    of_find_compatible_node(NULL, NULL, "fsl,imx21-audmux")) {
144 		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR1_SSI0,
145 			IMX_AUDMUX_V1_PCR_SYN |
146 			IMX_AUDMUX_V1_PCR_TFSDIR |
147 			IMX_AUDMUX_V1_PCR_TCLKDIR |
148 			IMX_AUDMUX_V1_PCR_RFSDIR |
149 			IMX_AUDMUX_V1_PCR_RCLKDIR |
150 			IMX_AUDMUX_V1_PCR_TFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
151 			IMX_AUDMUX_V1_PCR_RFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
152 			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4)
153 		);
154 		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR3_SSI_PINS_4,
155 			IMX_AUDMUX_V1_PCR_SYN |
156 			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR1_SSI0)
157 		);
158 	} else if (machine_is_eukrea_cpuimx25sd() ||
159 		   machine_is_eukrea_cpuimx35sd() ||
160 		   machine_is_eukrea_cpuimx51sd() ||
161 		   of_find_compatible_node(NULL, NULL, "fsl,imx31-audmux")) {
162 		if (!np)
163 			ext_port = machine_is_eukrea_cpuimx25sd() ?
164 				4 : 3;
165 
166 		imx_audmux_v2_configure_port(int_port,
167 			IMX_AUDMUX_V2_PTCR_SYN |
168 			IMX_AUDMUX_V2_PTCR_TFSDIR |
169 			IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
170 			IMX_AUDMUX_V2_PTCR_TCLKDIR |
171 			IMX_AUDMUX_V2_PTCR_TCSEL(ext_port),
172 			IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)
173 		);
174 		imx_audmux_v2_configure_port(ext_port,
175 			IMX_AUDMUX_V2_PTCR_SYN,
176 			IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)
177 		);
178 	} else {
179 		if (np) {
180 			/* The eukrea,asoc-tlv320 driver was explicitly
181 			 * requested (through the device tree).
182 			 */
183 			dev_err(&pdev->dev,
184 				"Missing or invalid audmux DT node.\n");
185 			return -ENODEV;
186 		} else {
187 			/* Return happy.
188 			 * We might run on a totally different machine.
189 			 */
190 			return 0;
191 		}
192 	}
193 
194 	ret = snd_soc_register_card(&eukrea_tlv320);
195 err:
196 	if (ret)
197 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
198 	of_node_put(ssi_np);
199 
200 	return ret;
201 }
202 
203 static int eukrea_tlv320_remove(struct platform_device *pdev)
204 {
205 	snd_soc_unregister_card(&eukrea_tlv320);
206 
207 	return 0;
208 }
209 
210 static const struct of_device_id imx_tlv320_dt_ids[] = {
211 	{ .compatible = "eukrea,asoc-tlv320"},
212 	{ /* sentinel */ }
213 };
214 MODULE_DEVICE_TABLE(of, imx_tlv320_dt_ids);
215 
216 static struct platform_driver eukrea_tlv320_driver = {
217 	.driver = {
218 		.name = "eukrea_tlv320",
219 		.of_match_table = imx_tlv320_dt_ids,
220 	},
221 	.probe = eukrea_tlv320_probe,
222 	.remove = eukrea_tlv320_remove,
223 };
224 
225 module_platform_driver(eukrea_tlv320_driver);
226 
227 MODULE_AUTHOR("Eric Bénard <eric@eukrea.com>");
228 MODULE_DESCRIPTION("CPUIMX ALSA SoC driver");
229 MODULE_LICENSE("GPL");
230 MODULE_ALIAS("platform:eukrea_tlv320");
231