xref: /openbmc/linux/sound/soc/fsl/eukrea-tlv320.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1ecf4630fSAndra Danciu // SPDX-License-Identifier: GPL-2.0+
2ecf4630fSAndra Danciu //
3ecf4630fSAndra Danciu // eukrea-tlv320.c  --  SoC audio for eukrea_cpuimxXX in I2S mode
4ecf4630fSAndra Danciu //
5ecf4630fSAndra Danciu // Copyright 2010 Eric Bénard, Eukréa Electromatique <eric@eukrea.com>
6ecf4630fSAndra Danciu //
7ecf4630fSAndra Danciu // based on sound/soc/s3c24xx/s3c24xx_simtec_tlv320aic23.c
8ecf4630fSAndra Danciu // which is Copyright 2009 Simtec Electronics
9ecf4630fSAndra Danciu // and on sound/soc/imx/phycore-ac97.c which is
10ecf4630fSAndra Danciu // Copyright 2009 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
11a23dc694SShawn Guo 
1266f23290SDenis Carikli #include <linux/errno.h>
13a23dc694SShawn Guo #include <linux/module.h>
14a23dc694SShawn Guo #include <linux/moduleparam.h>
1566f23290SDenis Carikli #include <linux/of.h>
1666f23290SDenis Carikli #include <linux/of_platform.h>
17a23dc694SShawn Guo #include <linux/device.h>
18a23dc694SShawn Guo #include <linux/i2c.h>
19a23dc694SShawn Guo #include <sound/core.h>
20a23dc694SShawn Guo #include <sound/pcm.h>
21a23dc694SShawn Guo #include <sound/soc.h>
22a23dc694SShawn Guo #include <asm/mach-types.h>
23a23dc694SShawn Guo 
24a23dc694SShawn Guo #include "../codecs/tlv320aic23.h"
25a23dc694SShawn Guo #include "imx-ssi.h"
26a23dc694SShawn Guo #include "imx-audmux.h"
27a23dc694SShawn Guo 
28a23dc694SShawn Guo #define CODEC_CLOCK 12000000
29a23dc694SShawn Guo 
eukrea_tlv320_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)30a23dc694SShawn Guo static int eukrea_tlv320_hw_params(struct snd_pcm_substream *substream,
31a23dc694SShawn Guo 			    struct snd_pcm_hw_params *params)
32a23dc694SShawn Guo {
339f5f078aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3417198ae7SKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
3517198ae7SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
36a23dc694SShawn Guo 	int ret;
37a23dc694SShawn Guo 
38a23dc694SShawn Guo 	ret = snd_soc_dai_set_sysclk(codec_dai, 0,
39a23dc694SShawn Guo 				     CODEC_CLOCK, SND_SOC_CLOCK_OUT);
40a23dc694SShawn Guo 	if (ret) {
41154bab12SDenis Carikli 		dev_err(cpu_dai->dev,
42154bab12SDenis Carikli 			"Failed to set the codec sysclk.\n");
43a23dc694SShawn Guo 		return ret;
44a23dc694SShawn Guo 	}
4566f23290SDenis Carikli 
46d0077aafSLars-Peter Clausen 	snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 0);
47a23dc694SShawn Guo 
48a23dc694SShawn Guo 	ret = snd_soc_dai_set_sysclk(cpu_dai, IMX_SSP_SYS_CLK, 0,
49a23dc694SShawn Guo 				SND_SOC_CLOCK_IN);
5066f23290SDenis Carikli 	/* fsl_ssi lacks the set_sysclk ops */
5166f23290SDenis Carikli 	if (ret && ret != -EINVAL) {
52154bab12SDenis Carikli 		dev_err(cpu_dai->dev,
53154bab12SDenis Carikli 			"Can't set the IMX_SSP_SYS_CLK CPU system clock.\n");
54a23dc694SShawn Guo 		return ret;
55a23dc694SShawn Guo 	}
56a23dc694SShawn Guo 
57a23dc694SShawn Guo 	return 0;
58a23dc694SShawn Guo }
59a23dc694SShawn Guo 
605ace37bdSBhumika Goyal static const struct snd_soc_ops eukrea_tlv320_snd_ops = {
61a23dc694SShawn Guo 	.hw_params	= eukrea_tlv320_hw_params,
62a23dc694SShawn Guo };
63a23dc694SShawn Guo 
643219d9c7SKuninori Morimoto SND_SOC_DAILINK_DEFS(hifi,
653219d9c7SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
6668707c37SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic23-hifi")),
6768707c37SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
683219d9c7SKuninori Morimoto 
69a23dc694SShawn Guo static struct snd_soc_dai_link eukrea_tlv320_dai = {
70a23dc694SShawn Guo 	.name		= "tlv320aic23",
71a23dc694SShawn Guo 	.stream_name	= "TLV320AIC23",
724de59dbdSLars-Peter Clausen 	.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
734348be63SMark Brown 			  SND_SOC_DAIFMT_CBP_CFP,
74a23dc694SShawn Guo 	.ops		= &eukrea_tlv320_snd_ops,
753219d9c7SKuninori Morimoto 	SND_SOC_DAILINK_REG(hifi),
76a23dc694SShawn Guo };
77a23dc694SShawn Guo 
78a23dc694SShawn Guo static struct snd_soc_card eukrea_tlv320 = {
79a23dc694SShawn Guo 	.owner		= THIS_MODULE,
80a23dc694SShawn Guo 	.dai_link	= &eukrea_tlv320_dai,
81a23dc694SShawn Guo 	.num_links	= 1,
82a23dc694SShawn Guo };
83a23dc694SShawn Guo 
eukrea_tlv320_probe(struct platform_device * pdev)84a0a3d518SBill Pemberton static int eukrea_tlv320_probe(struct platform_device *pdev)
85a23dc694SShawn Guo {
86a23dc694SShawn Guo 	int ret;
87a23dc694SShawn Guo 	int int_port = 0, ext_port;
8866f23290SDenis Carikli 	struct device_node *np = pdev->dev.of_node;
89bfb735a3SLiang He 	struct device_node *ssi_np = NULL, *codec_np = NULL, *tmp_np = NULL;
90a23dc694SShawn Guo 
9166f23290SDenis Carikli 	eukrea_tlv320.dev = &pdev->dev;
9266f23290SDenis Carikli 	if (np) {
9366f23290SDenis Carikli 		ret = snd_soc_of_parse_card_name(&eukrea_tlv320,
9466f23290SDenis Carikli 						 "eukrea,model");
9566f23290SDenis Carikli 		if (ret) {
9666f23290SDenis Carikli 			dev_err(&pdev->dev,
9766f23290SDenis Carikli 				"eukrea,model node missing or invalid.\n");
9866f23290SDenis Carikli 			goto err;
9966f23290SDenis Carikli 		}
10066f23290SDenis Carikli 
10166f23290SDenis Carikli 		ssi_np = of_parse_phandle(pdev->dev.of_node,
10266f23290SDenis Carikli 					  "ssi-controller", 0);
10366f23290SDenis Carikli 		if (!ssi_np) {
10466f23290SDenis Carikli 			dev_err(&pdev->dev,
10566f23290SDenis Carikli 				"ssi-controller missing or invalid.\n");
10666f23290SDenis Carikli 			ret = -ENODEV;
10766f23290SDenis Carikli 			goto err;
10866f23290SDenis Carikli 		}
10966f23290SDenis Carikli 
11066f23290SDenis Carikli 		codec_np = of_parse_phandle(ssi_np, "codec-handle", 0);
11166f23290SDenis Carikli 		if (codec_np)
1123219d9c7SKuninori Morimoto 			eukrea_tlv320_dai.codecs->of_node = codec_np;
11366f23290SDenis Carikli 		else
11466f23290SDenis Carikli 			dev_err(&pdev->dev, "codec-handle node missing or invalid.\n");
11566f23290SDenis Carikli 
11666f23290SDenis Carikli 		ret = of_property_read_u32(np, "fsl,mux-int-port", &int_port);
11766f23290SDenis Carikli 		if (ret) {
11866f23290SDenis Carikli 			dev_err(&pdev->dev,
11966f23290SDenis Carikli 				"fsl,mux-int-port node missing or invalid.\n");
120b820d52eSWen Yang 			goto err;
12166f23290SDenis Carikli 		}
12266f23290SDenis Carikli 		ret = of_property_read_u32(np, "fsl,mux-ext-port", &ext_port);
12366f23290SDenis Carikli 		if (ret) {
12466f23290SDenis Carikli 			dev_err(&pdev->dev,
12566f23290SDenis Carikli 				"fsl,mux-ext-port node missing or invalid.\n");
126b820d52eSWen Yang 			goto err;
12766f23290SDenis Carikli 		}
12866f23290SDenis Carikli 
12966f23290SDenis Carikli 		/*
13066f23290SDenis Carikli 		 * The port numbering in the hardware manual starts at 1, while
13166f23290SDenis Carikli 		 * the audmux API expects it starts at 0.
13266f23290SDenis Carikli 		 */
13366f23290SDenis Carikli 		int_port--;
13466f23290SDenis Carikli 		ext_port--;
13566f23290SDenis Carikli 
1363219d9c7SKuninori Morimoto 		eukrea_tlv320_dai.cpus->of_node = ssi_np;
13768707c37SKuninori Morimoto 		eukrea_tlv320_dai.platforms->of_node = ssi_np;
13866f23290SDenis Carikli 	} else {
1393219d9c7SKuninori Morimoto 		eukrea_tlv320_dai.cpus->dai_name = "imx-ssi.0";
14068707c37SKuninori Morimoto 		eukrea_tlv320_dai.platforms->name = "imx-ssi.0";
1413219d9c7SKuninori Morimoto 		eukrea_tlv320_dai.codecs->name = "tlv320aic23-codec.0-001a";
14266f23290SDenis Carikli 		eukrea_tlv320.name = "cpuimx-audio";
14366f23290SDenis Carikli 	}
14466f23290SDenis Carikli 
14566f23290SDenis Carikli 	if (machine_is_eukrea_cpuimx27() ||
146bfb735a3SLiang He 	    (tmp_np = of_find_compatible_node(NULL, NULL, "fsl,imx21-audmux"))) {
147a23dc694SShawn Guo 		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR1_SSI0,
148a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_SYN |
149a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_TFSDIR |
150a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_TCLKDIR |
151a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_RFSDIR |
152a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_RCLKDIR |
153a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_TFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
154a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_RFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
155a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4)
156a23dc694SShawn Guo 		);
157a23dc694SShawn Guo 		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR3_SSI_PINS_4,
158a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_SYN |
159a23dc694SShawn Guo 			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR1_SSI0)
160a23dc694SShawn Guo 		);
161bfb735a3SLiang He 		of_node_put(tmp_np);
162a23dc694SShawn Guo 	} else if (machine_is_eukrea_cpuimx25sd() ||
163a23dc694SShawn Guo 		   machine_is_eukrea_cpuimx35sd() ||
16466f23290SDenis Carikli 		   machine_is_eukrea_cpuimx51sd() ||
165bfb735a3SLiang He 		   (tmp_np = of_find_compatible_node(NULL, NULL, "fsl,imx31-audmux"))) {
16666f23290SDenis Carikli 		if (!np)
16766f23290SDenis Carikli 			ext_port = machine_is_eukrea_cpuimx25sd() ?
16866f23290SDenis Carikli 				4 : 3;
16966f23290SDenis Carikli 
170a23dc694SShawn Guo 		imx_audmux_v2_configure_port(int_port,
171a23dc694SShawn Guo 			IMX_AUDMUX_V2_PTCR_SYN |
172a23dc694SShawn Guo 			IMX_AUDMUX_V2_PTCR_TFSDIR |
173a23dc694SShawn Guo 			IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
174a23dc694SShawn Guo 			IMX_AUDMUX_V2_PTCR_TCLKDIR |
175a23dc694SShawn Guo 			IMX_AUDMUX_V2_PTCR_TCSEL(ext_port),
176a23dc694SShawn Guo 			IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)
177a23dc694SShawn Guo 		);
178a23dc694SShawn Guo 		imx_audmux_v2_configure_port(ext_port,
179a23dc694SShawn Guo 			IMX_AUDMUX_V2_PTCR_SYN,
180a23dc694SShawn Guo 			IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)
181a23dc694SShawn Guo 		);
182bfb735a3SLiang He 		of_node_put(tmp_np);
183a23dc694SShawn Guo 	} else {
18466f23290SDenis Carikli 		if (np) {
185dcfcf2c2SXiubo Li 			/* The eukrea,asoc-tlv320 driver was explicitly
18666f23290SDenis Carikli 			 * requested (through the device tree).
18766f23290SDenis Carikli 			 */
18866f23290SDenis Carikli 			dev_err(&pdev->dev,
18966f23290SDenis Carikli 				"Missing or invalid audmux DT node.\n");
19066f23290SDenis Carikli 			return -ENODEV;
19166f23290SDenis Carikli 		} else {
19266f23290SDenis Carikli 			/* Return happy.
19366f23290SDenis Carikli 			 * We might run on a totally different machine.
19466f23290SDenis Carikli 			 */
195a23dc694SShawn Guo 			return 0;
196a23dc694SShawn Guo 		}
19766f23290SDenis Carikli 	}
198a23dc694SShawn Guo 
199da75c924SFabio Estevam 	ret = snd_soc_register_card(&eukrea_tlv320);
20066f23290SDenis Carikli err:
201da75c924SFabio Estevam 	if (ret)
202da75c924SFabio Estevam 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
20366f23290SDenis Carikli 	of_node_put(ssi_np);
204a23dc694SShawn Guo 
205a23dc694SShawn Guo 	return ret;
206a23dc694SShawn Guo }
207a23dc694SShawn Guo 
eukrea_tlv320_remove(struct platform_device * pdev)208*01192ad2SUwe Kleine-König static void eukrea_tlv320_remove(struct platform_device *pdev)
209a23dc694SShawn Guo {
210da75c924SFabio Estevam 	snd_soc_unregister_card(&eukrea_tlv320);
211a23dc694SShawn Guo }
212a23dc694SShawn Guo 
21366f23290SDenis Carikli static const struct of_device_id imx_tlv320_dt_ids[] = {
21466f23290SDenis Carikli 	{ .compatible = "eukrea,asoc-tlv320"},
21566f23290SDenis Carikli 	{ /* sentinel */ }
21666f23290SDenis Carikli };
21766f23290SDenis Carikli MODULE_DEVICE_TABLE(of, imx_tlv320_dt_ids);
21866f23290SDenis Carikli 
219da75c924SFabio Estevam static struct platform_driver eukrea_tlv320_driver = {
220da75c924SFabio Estevam 	.driver = {
221da75c924SFabio Estevam 		.name = "eukrea_tlv320",
22266f23290SDenis Carikli 		.of_match_table = imx_tlv320_dt_ids,
223da75c924SFabio Estevam 	},
224da75c924SFabio Estevam 	.probe = eukrea_tlv320_probe,
225*01192ad2SUwe Kleine-König 	.remove_new = eukrea_tlv320_remove,
226154bab12SDenis Carikli };
227da75c924SFabio Estevam 
228da75c924SFabio Estevam module_platform_driver(eukrea_tlv320_driver);
229a23dc694SShawn Guo 
230a23dc694SShawn Guo MODULE_AUTHOR("Eric Bénard <eric@eukrea.com>");
231a23dc694SShawn Guo MODULE_DESCRIPTION("CPUIMX ALSA SoC driver");
232a23dc694SShawn Guo MODULE_LICENSE("GPL");
233da75c924SFabio Estevam MODULE_ALIAS("platform:eukrea_tlv320");
234