]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/fsl/imx-sgtl5000.c
ASoC: fsl: make audmux optional for i.MX6UL support
[karo-tx-linux.git] / sound / soc / fsl / imx-sgtl5000.c
1 /*
2  * Copyright 2012 Freescale Semiconductor, Inc.
3  * Copyright 2012 Linaro Ltd.
4  *
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:
8  *
9  * http://www.opensource.org/licenses/gpl-license.html
10  * http://www.gnu.org/copyleft/gpl.html
11  */
12
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_platform.h>
16 #include <linux/i2c.h>
17 #include <linux/clk.h>
18 #include <sound/soc.h>
19
20 #include "../codecs/sgtl5000.h"
21 #include "imx-audmux.h"
22
23 #define DAI_NAME_SIZE   32
24
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;
32 };
33
34 static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime *rtd)
35 {
36         struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(rtd->card);
37         struct device *dev = rtd->card->dev;
38         int ret;
39
40         ret = snd_soc_dai_set_sysclk(rtd->codec_dai, SGTL5000_SYSCLK,
41                                      data->clk_frequency, SND_SOC_CLOCK_IN);
42         if (ret) {
43                 dev_err(dev, "could not set codec driver clock params\n");
44                 return ret;
45         }
46
47         return 0;
48 }
49
50 static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets[] = {
51         SND_SOC_DAPM_MIC("Mic Jack", NULL),
52         SND_SOC_DAPM_LINE("Line In Jack", NULL),
53         SND_SOC_DAPM_HP("Headphone Jack", NULL),
54         SND_SOC_DAPM_SPK("Line Out Jack", NULL),
55         SND_SOC_DAPM_SPK("Ext Spk", NULL),
56 };
57
58 static int imx_sgtl5000_probe(struct platform_device *pdev)
59 {
60         struct device_node *np = pdev->dev.of_node;
61         struct device_node *ssi_np, *codec_np;
62         struct platform_device *ssi_pdev;
63         struct i2c_client *codec_dev;
64         struct imx_sgtl5000_data *data = NULL;
65         int int_port, ext_port;
66         int ret;
67
68         if (!of_property_read_bool(np, "fsl,no-audmux")) {
69                 ret = of_property_read_u32(np, "mux-int-port", &int_port);
70                 if (ret) {
71                         dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
72                         return ret;
73                 }
74                 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
75                 if (ret) {
76                         dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
77                         return ret;
78                 }
79
80                 /*
81                  * The port numbering in the hardware manual starts at 1, while
82                  * the audmux API expects it starts at 0.
83                  */
84                 int_port--;
85                 ext_port--;
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));
93                 if (ret) {
94                         dev_err(&pdev->dev, "audmux internal port setup failed\n");
95                         return ret;
96                 }
97                 ret = imx_audmux_v2_configure_port(ext_port,
98                                 IMX_AUDMUX_V2_PTCR_SYN,
99                                 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
100                 if (ret) {
101                         dev_err(&pdev->dev, "audmux external port setup failed\n");
102                         return ret;
103                 }
104         }
105
106         ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
107         codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
108         if (!ssi_np || !codec_np) {
109                 dev_err(&pdev->dev, "phandle missing or invalid\n");
110                 ret = -EINVAL;
111                 goto fail;
112         }
113
114         ssi_pdev = of_find_device_by_node(ssi_np);
115         if (!ssi_pdev) {
116                 dev_err(&pdev->dev, "failed to find SSI platform device\n");
117                 ret = -EPROBE_DEFER;
118                 goto fail;
119         }
120         codec_dev = of_find_i2c_device_by_node(codec_np);
121         if (!codec_dev) {
122                 dev_err(&pdev->dev, "failed to find codec platform device\n");
123                 return -EPROBE_DEFER;
124         }
125
126         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
127         if (!data) {
128                 ret = -ENOMEM;
129                 goto fail;
130         }
131
132         data->codec_clk = clk_get(&codec_dev->dev, NULL);
133         if (IS_ERR(data->codec_clk)) {
134                 ret = PTR_ERR(data->codec_clk);
135                 goto fail;
136         }
137
138         data->clk_frequency = clk_get_rate(data->codec_clk);
139
140         data->dai.name = "HiFi";
141         data->dai.stream_name = "HiFi";
142         data->dai.codec_dai_name = "sgtl5000";
143         data->dai.codec_of_node = codec_np;
144         data->dai.cpu_of_node = ssi_np;
145         data->dai.platform_of_node = ssi_np;
146         data->dai.init = &imx_sgtl5000_dai_init;
147         data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
148                             SND_SOC_DAIFMT_CBM_CFM;
149
150         data->card.dev = &pdev->dev;
151         ret = snd_soc_of_parse_card_name(&data->card, "model");
152         if (ret)
153                 goto fail;
154         ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
155         if (ret)
156                 goto fail;
157         data->card.num_links = 1;
158         data->card.owner = THIS_MODULE;
159         data->card.dai_link = &data->dai;
160         data->card.dapm_widgets = imx_sgtl5000_dapm_widgets;
161         data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets);
162
163         platform_set_drvdata(pdev, &data->card);
164         snd_soc_card_set_drvdata(&data->card, data);
165
166         ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
167         if (ret) {
168                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
169                 goto fail;
170         }
171
172         of_node_put(ssi_np);
173         of_node_put(codec_np);
174
175         return 0;
176
177 fail:
178         if (data && !IS_ERR(data->codec_clk))
179                 clk_put(data->codec_clk);
180         of_node_put(ssi_np);
181         of_node_put(codec_np);
182
183         return ret;
184 }
185
186 static int imx_sgtl5000_remove(struct platform_device *pdev)
187 {
188         struct snd_soc_card *card = platform_get_drvdata(pdev);
189         struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(card);
190
191         clk_put(data->codec_clk);
192
193         return 0;
194 }
195
196 static const struct of_device_id imx_sgtl5000_dt_ids[] = {
197         { .compatible = "fsl,imx-audio-sgtl5000", },
198         { /* sentinel */ }
199 };
200 MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids);
201
202 static struct platform_driver imx_sgtl5000_driver = {
203         .driver = {
204                 .name = "imx-sgtl5000",
205                 .pm = &snd_soc_pm_ops,
206                 .of_match_table = imx_sgtl5000_dt_ids,
207         },
208         .probe = imx_sgtl5000_probe,
209         .remove = imx_sgtl5000_remove,
210 };
211 module_platform_driver(imx_sgtl5000_driver);
212
213 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
214 MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
215 MODULE_LICENSE("GPL v2");
216 MODULE_ALIAS("platform:imx-sgtl5000");