2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright 2012 Linaro Ltd.
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
13 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/of_i2c.h>
17 #include <linux/clk.h>
18 #include <sound/soc.h>
20 #include "../codecs/sgtl5000.h"
21 #include "imx-audmux.h"
23 #define DAI_NAME_SIZE 32
25 struct imx_sgtl5000_data {
26 struct snd_soc_dai_link dai;
27 struct snd_soc_card card;
28 char codec_dai_name[DAI_NAME_SIZE];
29 char platform_name[DAI_NAME_SIZE];
30 struct clk *codec_clk;
31 unsigned int clk_frequency;
34 static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime *rtd)
36 struct imx_sgtl5000_data *data = container_of(rtd->card,
37 struct imx_sgtl5000_data, card);
38 struct device *dev = rtd->card->dev;
41 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, SGTL5000_SYSCLK,
42 data->clk_frequency, SND_SOC_CLOCK_IN);
44 dev_err(dev, "could not set codec driver clock params\n");
51 static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets[] = {
52 SND_SOC_DAPM_MIC("Mic Jack", NULL),
53 SND_SOC_DAPM_LINE("Line In Jack", NULL),
54 SND_SOC_DAPM_HP("Headphone Jack", NULL),
55 SND_SOC_DAPM_SPK("Line Out Jack", NULL),
56 SND_SOC_DAPM_SPK("Ext Spk", NULL),
59 static int imx_sgtl5000_probe(struct platform_device *pdev)
61 struct device_node *np = pdev->dev.of_node;
62 struct device_node *ssi_np, *codec_np;
63 struct platform_device *ssi_pdev;
64 struct i2c_client *codec_dev;
65 struct imx_sgtl5000_data *data;
66 int int_port, ext_port;
69 ret = of_property_read_u32(np, "mux-int-port", &int_port);
71 dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
74 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
76 dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
81 * The port numbering in the hardware manual starts at 1, while
82 * the audmux API expects it starts at 0.
86 ret = imx_audmux_v2_configure_port(int_port,
87 IMX_AUDMUX_V2_PTCR_SYN |
88 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
89 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
90 IMX_AUDMUX_V2_PTCR_TFSDIR |
91 IMX_AUDMUX_V2_PTCR_TCLKDIR,
92 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
94 dev_err(&pdev->dev, "audmux internal port setup failed\n");
97 ret = imx_audmux_v2_configure_port(ext_port,
98 IMX_AUDMUX_V2_PTCR_SYN,
99 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
101 dev_err(&pdev->dev, "audmux external port setup failed\n");
105 ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
106 codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
107 if (!ssi_np || !codec_np) {
108 dev_err(&pdev->dev, "phandle missing or invalid\n");
113 ssi_pdev = of_find_device_by_node(ssi_np);
115 dev_err(&pdev->dev, "failed to find SSI platform device\n");
119 codec_dev = of_find_i2c_device_by_node(codec_np);
121 dev_err(&pdev->dev, "failed to find codec platform device\n");
125 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
131 data->codec_clk = devm_clk_get(&codec_dev->dev, NULL);
132 if (IS_ERR(data->codec_clk))
135 data->clk_frequency = clk_get_rate(data->codec_clk);
137 data->dai.name = "HiFi";
138 data->dai.stream_name = "HiFi";
139 data->dai.codec_dai_name = "sgtl5000";
140 data->dai.codec_of_node = codec_np;
141 data->dai.cpu_of_node = ssi_np;
142 data->dai.platform_of_node = ssi_np;
143 data->dai.init = &imx_sgtl5000_dai_init;
144 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
145 SND_SOC_DAIFMT_CBM_CFM;
147 data->card.dev = &pdev->dev;
148 ret = snd_soc_of_parse_card_name(&data->card, "model");
151 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
154 data->card.num_links = 1;
155 data->card.owner = THIS_MODULE;
156 data->card.dai_link = &data->dai;
157 data->card.dapm_widgets = imx_sgtl5000_dapm_widgets;
158 data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets);
160 ret = snd_soc_register_card(&data->card);
162 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
166 platform_set_drvdata(pdev, data);
168 of_node_put(codec_np);
176 of_node_put(codec_np);
181 static int imx_sgtl5000_remove(struct platform_device *pdev)
183 struct imx_sgtl5000_data *data = platform_get_drvdata(pdev);
185 snd_soc_unregister_card(&data->card);
190 static const struct of_device_id imx_sgtl5000_dt_ids[] = {
191 { .compatible = "fsl,imx-audio-sgtl5000", },
194 MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids);
196 static struct platform_driver imx_sgtl5000_driver = {
198 .name = "imx-sgtl5000",
199 .owner = THIS_MODULE,
200 .of_match_table = imx_sgtl5000_dt_ids,
202 .probe = imx_sgtl5000_probe,
203 .remove = imx_sgtl5000_remove,
205 module_platform_driver(imx_sgtl5000_driver);
207 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
208 MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
209 MODULE_LICENSE("GPL v2");
210 MODULE_ALIAS("platform:imx-sgtl5000");