1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright 2012 Freescale Semiconductor, Inc.
4 // Copyright 2012 Linaro Ltd.
6 #include <linux/module.h>
8 #include <linux/of_platform.h>
10 #include <linux/clk.h>
11 #include <sound/soc.h>
13 #include "../codecs/sgtl5000.h"
14 #include "imx-audmux.h"
16 #define DAI_NAME_SIZE 32
18 struct imx_sgtl5000_data
{
19 struct snd_soc_dai_link dai
;
20 struct snd_soc_card card
;
21 char codec_dai_name
[DAI_NAME_SIZE
];
22 char platform_name
[DAI_NAME_SIZE
];
23 struct clk
*codec_clk
;
24 unsigned int clk_frequency
;
27 static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime
*rtd
)
29 struct imx_sgtl5000_data
*data
= snd_soc_card_get_drvdata(rtd
->card
);
30 struct device
*dev
= rtd
->card
->dev
;
33 ret
= snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd
, 0), SGTL5000_SYSCLK
,
34 data
->clk_frequency
, SND_SOC_CLOCK_IN
);
36 dev_err(dev
, "could not set codec driver clock params\n");
43 static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets
[] = {
44 SND_SOC_DAPM_MIC("Mic Jack", NULL
),
45 SND_SOC_DAPM_LINE("Line In Jack", NULL
),
46 SND_SOC_DAPM_HP("Headphone Jack", NULL
),
47 SND_SOC_DAPM_SPK("Line Out Jack", NULL
),
48 SND_SOC_DAPM_SPK("Ext Spk", NULL
),
51 static int imx_sgtl5000_probe(struct platform_device
*pdev
)
53 struct device_node
*np
= pdev
->dev
.of_node
;
54 struct device_node
*ssi_np
, *codec_np
;
55 struct platform_device
*ssi_pdev
;
56 struct i2c_client
*codec_dev
;
57 struct imx_sgtl5000_data
*data
= NULL
;
58 struct snd_soc_dai_link_component
*comp
;
59 int int_port
, ext_port
;
62 ret
= of_property_read_u32(np
, "mux-int-port", &int_port
);
64 dev_err(&pdev
->dev
, "mux-int-port missing or invalid\n");
67 ret
= of_property_read_u32(np
, "mux-ext-port", &ext_port
);
69 dev_err(&pdev
->dev
, "mux-ext-port missing or invalid\n");
74 * The port numbering in the hardware manual starts at 1, while
75 * the audmux API expects it starts at 0.
79 ret
= imx_audmux_v2_configure_port(int_port
,
80 IMX_AUDMUX_V2_PTCR_SYN
|
81 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port
) |
82 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port
) |
83 IMX_AUDMUX_V2_PTCR_TFSDIR
|
84 IMX_AUDMUX_V2_PTCR_TCLKDIR
,
85 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port
));
87 dev_err(&pdev
->dev
, "audmux internal port setup failed\n");
90 ret
= imx_audmux_v2_configure_port(ext_port
,
91 IMX_AUDMUX_V2_PTCR_SYN
,
92 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port
));
94 dev_err(&pdev
->dev
, "audmux external port setup failed\n");
98 ssi_np
= of_parse_phandle(pdev
->dev
.of_node
, "ssi-controller", 0);
99 codec_np
= of_parse_phandle(pdev
->dev
.of_node
, "audio-codec", 0);
100 if (!ssi_np
|| !codec_np
) {
101 dev_err(&pdev
->dev
, "phandle missing or invalid\n");
106 ssi_pdev
= of_find_device_by_node(ssi_np
);
108 dev_dbg(&pdev
->dev
, "failed to find SSI platform device\n");
112 put_device(&ssi_pdev
->dev
);
113 codec_dev
= of_find_i2c_device_by_node(codec_np
);
115 dev_dbg(&pdev
->dev
, "failed to find codec platform device\n");
120 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
126 comp
= devm_kzalloc(&pdev
->dev
, 3 * sizeof(*comp
), GFP_KERNEL
);
132 data
->codec_clk
= clk_get(&codec_dev
->dev
, NULL
);
133 if (IS_ERR(data
->codec_clk
)) {
134 ret
= PTR_ERR(data
->codec_clk
);
138 data
->clk_frequency
= clk_get_rate(data
->codec_clk
);
140 data
->dai
.cpus
= &comp
[0];
141 data
->dai
.codecs
= &comp
[1];
142 data
->dai
.platforms
= &comp
[2];
144 data
->dai
.num_cpus
= 1;
145 data
->dai
.num_codecs
= 1;
146 data
->dai
.num_platforms
= 1;
148 data
->dai
.name
= "HiFi";
149 data
->dai
.stream_name
= "HiFi";
150 data
->dai
.codecs
->dai_name
= "sgtl5000";
151 data
->dai
.codecs
->of_node
= codec_np
;
152 data
->dai
.cpus
->of_node
= ssi_np
;
153 data
->dai
.platforms
->of_node
= ssi_np
;
154 data
->dai
.init
= &imx_sgtl5000_dai_init
;
155 data
->dai
.dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
156 SND_SOC_DAIFMT_CBM_CFM
;
158 data
->card
.dev
= &pdev
->dev
;
159 ret
= snd_soc_of_parse_card_name(&data
->card
, "model");
162 ret
= snd_soc_of_parse_audio_routing(&data
->card
, "audio-routing");
165 data
->card
.num_links
= 1;
166 data
->card
.owner
= THIS_MODULE
;
167 data
->card
.dai_link
= &data
->dai
;
168 data
->card
.dapm_widgets
= imx_sgtl5000_dapm_widgets
;
169 data
->card
.num_dapm_widgets
= ARRAY_SIZE(imx_sgtl5000_dapm_widgets
);
171 platform_set_drvdata(pdev
, &data
->card
);
172 snd_soc_card_set_drvdata(&data
->card
, data
);
174 ret
= devm_snd_soc_register_card(&pdev
->dev
, &data
->card
);
176 if (ret
!= -EPROBE_DEFER
)
177 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n",
183 of_node_put(codec_np
);
188 if (data
&& !IS_ERR(data
->codec_clk
))
189 clk_put(data
->codec_clk
);
191 of_node_put(codec_np
);
196 static int imx_sgtl5000_remove(struct platform_device
*pdev
)
198 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
199 struct imx_sgtl5000_data
*data
= snd_soc_card_get_drvdata(card
);
201 clk_put(data
->codec_clk
);
206 static const struct of_device_id imx_sgtl5000_dt_ids
[] = {
207 { .compatible
= "fsl,imx-audio-sgtl5000", },
210 MODULE_DEVICE_TABLE(of
, imx_sgtl5000_dt_ids
);
212 static struct platform_driver imx_sgtl5000_driver
= {
214 .name
= "imx-sgtl5000",
215 .pm
= &snd_soc_pm_ops
,
216 .of_match_table
= imx_sgtl5000_dt_ids
,
218 .probe
= imx_sgtl5000_probe
,
219 .remove
= imx_sgtl5000_remove
,
221 module_platform_driver(imx_sgtl5000_driver
);
223 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
224 MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
225 MODULE_LICENSE("GPL v2");
226 MODULE_ALIAS("platform:imx-sgtl5000");