18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// eukrea-tlv320.c  --  SoC audio for eukrea_cpuimxXX in I2S mode
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright 2010 Eric Bénard, Eukréa Electromatique <eric@eukrea.com>
68c2ecf20Sopenharmony_ci//
78c2ecf20Sopenharmony_ci// based on sound/soc/s3c24xx/s3c24xx_simtec_tlv320aic23.c
88c2ecf20Sopenharmony_ci// which is Copyright 2009 Simtec Electronics
98c2ecf20Sopenharmony_ci// and on sound/soc/imx/phycore-ac97.c which is
108c2ecf20Sopenharmony_ci// Copyright 2009 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/errno.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
178c2ecf20Sopenharmony_ci#include <linux/device.h>
188c2ecf20Sopenharmony_ci#include <linux/i2c.h>
198c2ecf20Sopenharmony_ci#include <sound/core.h>
208c2ecf20Sopenharmony_ci#include <sound/pcm.h>
218c2ecf20Sopenharmony_ci#include <sound/soc.h>
228c2ecf20Sopenharmony_ci#include <asm/mach-types.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "../codecs/tlv320aic23.h"
258c2ecf20Sopenharmony_ci#include "imx-ssi.h"
268c2ecf20Sopenharmony_ci#include "imx-audmux.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define CODEC_CLOCK 12000000
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int eukrea_tlv320_hw_params(struct snd_pcm_substream *substream,
318c2ecf20Sopenharmony_ci			    struct snd_pcm_hw_params *params)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
348c2ecf20Sopenharmony_ci	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
358c2ecf20Sopenharmony_ci	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
368c2ecf20Sopenharmony_ci	int ret;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	ret = snd_soc_dai_set_sysclk(codec_dai, 0,
398c2ecf20Sopenharmony_ci				     CODEC_CLOCK, SND_SOC_CLOCK_OUT);
408c2ecf20Sopenharmony_ci	if (ret) {
418c2ecf20Sopenharmony_ci		dev_err(cpu_dai->dev,
428c2ecf20Sopenharmony_ci			"Failed to set the codec sysclk.\n");
438c2ecf20Sopenharmony_ci		return ret;
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 0);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	ret = snd_soc_dai_set_sysclk(cpu_dai, IMX_SSP_SYS_CLK, 0,
498c2ecf20Sopenharmony_ci				SND_SOC_CLOCK_IN);
508c2ecf20Sopenharmony_ci	/* fsl_ssi lacks the set_sysclk ops */
518c2ecf20Sopenharmony_ci	if (ret && ret != -EINVAL) {
528c2ecf20Sopenharmony_ci		dev_err(cpu_dai->dev,
538c2ecf20Sopenharmony_ci			"Can't set the IMX_SSP_SYS_CLK CPU system clock.\n");
548c2ecf20Sopenharmony_ci		return ret;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return 0;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic const struct snd_soc_ops eukrea_tlv320_snd_ops = {
618c2ecf20Sopenharmony_ci	.hw_params	= eukrea_tlv320_hw_params,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEFS(hifi,
658c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_EMPTY()),
668c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic23-hifi")),
678c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_EMPTY()));
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic struct snd_soc_dai_link eukrea_tlv320_dai = {
708c2ecf20Sopenharmony_ci	.name		= "tlv320aic23",
718c2ecf20Sopenharmony_ci	.stream_name	= "TLV320AIC23",
728c2ecf20Sopenharmony_ci	.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
738c2ecf20Sopenharmony_ci			  SND_SOC_DAIFMT_CBM_CFM,
748c2ecf20Sopenharmony_ci	.ops		= &eukrea_tlv320_snd_ops,
758c2ecf20Sopenharmony_ci	SND_SOC_DAILINK_REG(hifi),
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic struct snd_soc_card eukrea_tlv320 = {
798c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
808c2ecf20Sopenharmony_ci	.dai_link	= &eukrea_tlv320_dai,
818c2ecf20Sopenharmony_ci	.num_links	= 1,
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int eukrea_tlv320_probe(struct platform_device *pdev)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	int ret;
878c2ecf20Sopenharmony_ci	int int_port = 0, ext_port;
888c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
898c2ecf20Sopenharmony_ci	struct device_node *ssi_np = NULL, *codec_np = NULL, *tmp_np = NULL;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	eukrea_tlv320.dev = &pdev->dev;
928c2ecf20Sopenharmony_ci	if (np) {
938c2ecf20Sopenharmony_ci		ret = snd_soc_of_parse_card_name(&eukrea_tlv320,
948c2ecf20Sopenharmony_ci						 "eukrea,model");
958c2ecf20Sopenharmony_ci		if (ret) {
968c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
978c2ecf20Sopenharmony_ci				"eukrea,model node missing or invalid.\n");
988c2ecf20Sopenharmony_ci			goto err;
998c2ecf20Sopenharmony_ci		}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		ssi_np = of_parse_phandle(pdev->dev.of_node,
1028c2ecf20Sopenharmony_ci					  "ssi-controller", 0);
1038c2ecf20Sopenharmony_ci		if (!ssi_np) {
1048c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
1058c2ecf20Sopenharmony_ci				"ssi-controller missing or invalid.\n");
1068c2ecf20Sopenharmony_ci			ret = -ENODEV;
1078c2ecf20Sopenharmony_ci			goto err;
1088c2ecf20Sopenharmony_ci		}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		codec_np = of_parse_phandle(ssi_np, "codec-handle", 0);
1118c2ecf20Sopenharmony_ci		if (codec_np)
1128c2ecf20Sopenharmony_ci			eukrea_tlv320_dai.codecs->of_node = codec_np;
1138c2ecf20Sopenharmony_ci		else
1148c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "codec-handle node missing or invalid.\n");
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		ret = of_property_read_u32(np, "fsl,mux-int-port", &int_port);
1178c2ecf20Sopenharmony_ci		if (ret) {
1188c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
1198c2ecf20Sopenharmony_ci				"fsl,mux-int-port node missing or invalid.\n");
1208c2ecf20Sopenharmony_ci			goto err;
1218c2ecf20Sopenharmony_ci		}
1228c2ecf20Sopenharmony_ci		ret = of_property_read_u32(np, "fsl,mux-ext-port", &ext_port);
1238c2ecf20Sopenharmony_ci		if (ret) {
1248c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
1258c2ecf20Sopenharmony_ci				"fsl,mux-ext-port node missing or invalid.\n");
1268c2ecf20Sopenharmony_ci			goto err;
1278c2ecf20Sopenharmony_ci		}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		/*
1308c2ecf20Sopenharmony_ci		 * The port numbering in the hardware manual starts at 1, while
1318c2ecf20Sopenharmony_ci		 * the audmux API expects it starts at 0.
1328c2ecf20Sopenharmony_ci		 */
1338c2ecf20Sopenharmony_ci		int_port--;
1348c2ecf20Sopenharmony_ci		ext_port--;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci		eukrea_tlv320_dai.cpus->of_node = ssi_np;
1378c2ecf20Sopenharmony_ci		eukrea_tlv320_dai.platforms->of_node = ssi_np;
1388c2ecf20Sopenharmony_ci	} else {
1398c2ecf20Sopenharmony_ci		eukrea_tlv320_dai.cpus->dai_name = "imx-ssi.0";
1408c2ecf20Sopenharmony_ci		eukrea_tlv320_dai.platforms->name = "imx-ssi.0";
1418c2ecf20Sopenharmony_ci		eukrea_tlv320_dai.codecs->name = "tlv320aic23-codec.0-001a";
1428c2ecf20Sopenharmony_ci		eukrea_tlv320.name = "cpuimx-audio";
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (machine_is_eukrea_cpuimx27() ||
1468c2ecf20Sopenharmony_ci	    (tmp_np = of_find_compatible_node(NULL, NULL, "fsl,imx21-audmux"))) {
1478c2ecf20Sopenharmony_ci		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR1_SSI0,
1488c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_SYN |
1498c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_TFSDIR |
1508c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_TCLKDIR |
1518c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_RFSDIR |
1528c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_RCLKDIR |
1538c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_TFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
1548c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_RFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
1558c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4)
1568c2ecf20Sopenharmony_ci		);
1578c2ecf20Sopenharmony_ci		imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR3_SSI_PINS_4,
1588c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_SYN |
1598c2ecf20Sopenharmony_ci			IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR1_SSI0)
1608c2ecf20Sopenharmony_ci		);
1618c2ecf20Sopenharmony_ci		of_node_put(tmp_np);
1628c2ecf20Sopenharmony_ci	} else if (machine_is_eukrea_cpuimx25sd() ||
1638c2ecf20Sopenharmony_ci		   machine_is_eukrea_cpuimx35sd() ||
1648c2ecf20Sopenharmony_ci		   machine_is_eukrea_cpuimx51sd() ||
1658c2ecf20Sopenharmony_ci		   (tmp_np = of_find_compatible_node(NULL, NULL, "fsl,imx31-audmux"))) {
1668c2ecf20Sopenharmony_ci		if (!np)
1678c2ecf20Sopenharmony_ci			ext_port = machine_is_eukrea_cpuimx25sd() ?
1688c2ecf20Sopenharmony_ci				4 : 3;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		imx_audmux_v2_configure_port(int_port,
1718c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PTCR_SYN |
1728c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PTCR_TFSDIR |
1738c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
1748c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PTCR_TCLKDIR |
1758c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PTCR_TCSEL(ext_port),
1768c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)
1778c2ecf20Sopenharmony_ci		);
1788c2ecf20Sopenharmony_ci		imx_audmux_v2_configure_port(ext_port,
1798c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PTCR_SYN,
1808c2ecf20Sopenharmony_ci			IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)
1818c2ecf20Sopenharmony_ci		);
1828c2ecf20Sopenharmony_ci		of_node_put(tmp_np);
1838c2ecf20Sopenharmony_ci	} else {
1848c2ecf20Sopenharmony_ci		if (np) {
1858c2ecf20Sopenharmony_ci			/* The eukrea,asoc-tlv320 driver was explicitly
1868c2ecf20Sopenharmony_ci			 * requested (through the device tree).
1878c2ecf20Sopenharmony_ci			 */
1888c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
1898c2ecf20Sopenharmony_ci				"Missing or invalid audmux DT node.\n");
1908c2ecf20Sopenharmony_ci			return -ENODEV;
1918c2ecf20Sopenharmony_ci		} else {
1928c2ecf20Sopenharmony_ci			/* Return happy.
1938c2ecf20Sopenharmony_ci			 * We might run on a totally different machine.
1948c2ecf20Sopenharmony_ci			 */
1958c2ecf20Sopenharmony_ci			return 0;
1968c2ecf20Sopenharmony_ci		}
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	ret = snd_soc_register_card(&eukrea_tlv320);
2008c2ecf20Sopenharmony_cierr:
2018c2ecf20Sopenharmony_ci	if (ret)
2028c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
2038c2ecf20Sopenharmony_ci	of_node_put(ssi_np);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return ret;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int eukrea_tlv320_remove(struct platform_device *pdev)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	snd_soc_unregister_card(&eukrea_tlv320);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic const struct of_device_id imx_tlv320_dt_ids[] = {
2168c2ecf20Sopenharmony_ci	{ .compatible = "eukrea,asoc-tlv320"},
2178c2ecf20Sopenharmony_ci	{ /* sentinel */ }
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, imx_tlv320_dt_ids);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic struct platform_driver eukrea_tlv320_driver = {
2228c2ecf20Sopenharmony_ci	.driver = {
2238c2ecf20Sopenharmony_ci		.name = "eukrea_tlv320",
2248c2ecf20Sopenharmony_ci		.of_match_table = imx_tlv320_dt_ids,
2258c2ecf20Sopenharmony_ci	},
2268c2ecf20Sopenharmony_ci	.probe = eukrea_tlv320_probe,
2278c2ecf20Sopenharmony_ci	.remove = eukrea_tlv320_remove,
2288c2ecf20Sopenharmony_ci};
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cimodule_platform_driver(eukrea_tlv320_driver);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciMODULE_AUTHOR("Eric Bénard <eric@eukrea.com>");
2338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CPUIMX ALSA SoC driver");
2348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2358c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:eukrea_tlv320");
236