blob: 73b935e25f9baa98aa913933e6931711ad2564b7 [file] [log] [blame]
Shawn Guoc4483032012-03-16 16:56:44 +08001/*
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 <sound/soc.h>
17
18#include "../codecs/sgtl5000.h"
19#include "imx-audmux.h"
20
21#define DAI_NAME_SIZE 32
22
23struct imx_sgtl5000_data {
24 struct snd_soc_dai_link dai;
25 struct snd_soc_card card;
26 char codec_dai_name[DAI_NAME_SIZE];
27 char platform_name[DAI_NAME_SIZE];
28 unsigned int clk_frequency;
29};
30
31static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime *rtd)
32{
33 struct imx_sgtl5000_data *data = container_of(rtd->card,
34 struct imx_sgtl5000_data, card);
35 struct device *dev = rtd->card->dev;
36 int ret;
37
38 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, SGTL5000_SYSCLK,
39 data->clk_frequency, SND_SOC_CLOCK_IN);
40 if (ret) {
41 dev_err(dev, "could not set codec driver clock params\n");
42 return ret;
43 }
44
45 return 0;
46}
47
Shawn Guo172b4c5c2012-03-30 00:13:03 +080048static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets[] = {
49 SND_SOC_DAPM_MIC("Mic Jack", NULL),
50 SND_SOC_DAPM_LINE("Line In Jack", NULL),
51 SND_SOC_DAPM_HP("Headphone Jack", NULL),
52 SND_SOC_DAPM_SPK("Line Out Jack", NULL),
53 SND_SOC_DAPM_SPK("Ext Spk", NULL),
54};
55
Shawn Guoc4483032012-03-16 16:56:44 +080056static int __devinit imx_sgtl5000_probe(struct platform_device *pdev)
57{
58 struct device_node *np = pdev->dev.of_node;
59 struct device_node *ssi_np, *codec_np;
60 struct platform_device *ssi_pdev;
61 struct imx_sgtl5000_data *data;
62 int int_port, ext_port;
63 int ret;
64
65 ret = of_property_read_u32(np, "mux-int-port", &int_port);
66 if (ret) {
67 dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
68 return ret;
69 }
70 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
71 if (ret) {
72 dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
73 return ret;
74 }
75
76 /*
77 * The port numbering in the hardware manual starts at 1, while
78 * the audmux API expects it starts at 0.
79 */
80 int_port--;
81 ext_port--;
82 ret = imx_audmux_v2_configure_port(int_port,
83 IMX_AUDMUX_V2_PTCR_SYN |
84 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
85 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
86 IMX_AUDMUX_V2_PTCR_TFSDIR |
87 IMX_AUDMUX_V2_PTCR_TCLKDIR,
88 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
89 if (ret) {
90 dev_err(&pdev->dev, "audmux internal port setup failed\n");
91 return ret;
92 }
93 imx_audmux_v2_configure_port(ext_port,
94 IMX_AUDMUX_V2_PTCR_SYN |
95 IMX_AUDMUX_V2_PTCR_TCSEL(int_port),
96 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
97 if (ret) {
98 dev_err(&pdev->dev, "audmux external port setup failed\n");
99 return ret;
100 }
101
102 ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
103 codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
104 if (!ssi_np || !codec_np) {
105 dev_err(&pdev->dev, "phandle missing or invalid\n");
Richard Zhao717071d2012-04-27 15:02:56 +0800106 ret = -EINVAL;
107 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800108 }
109
110 ssi_pdev = of_find_device_by_node(ssi_np);
111 if (!ssi_pdev) {
112 dev_err(&pdev->dev, "failed to find SSI platform device\n");
Richard Zhao717071d2012-04-27 15:02:56 +0800113 ret = -EINVAL;
114 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800115 }
116
117 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Richard Zhao717071d2012-04-27 15:02:56 +0800118 if (!data) {
119 ret = -ENOMEM;
120 goto fail;
121 }
Shawn Guoc4483032012-03-16 16:56:44 +0800122
123 ret = of_property_read_u32(codec_np, "clock-frequency",
124 &data->clk_frequency);
125 if (ret) {
126 dev_err(&pdev->dev, "clock-frequency missing or invalid\n");
Richard Zhao717071d2012-04-27 15:02:56 +0800127 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800128 }
129
130 data->dai.name = "HiFi";
131 data->dai.stream_name = "HiFi";
132 data->dai.codec_dai_name = "sgtl5000";
133 data->dai.codec_of_node = codec_np;
134 data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
135 data->dai.platform_name = "imx-pcm-audio";
136 data->dai.init = &imx_sgtl5000_dai_init;
137 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
138 SND_SOC_DAIFMT_CBM_CFM;
139
140 data->card.dev = &pdev->dev;
141 ret = snd_soc_of_parse_card_name(&data->card, "model");
142 if (ret)
Richard Zhao717071d2012-04-27 15:02:56 +0800143 goto fail;
Shawn Guo172b4c5c2012-03-30 00:13:03 +0800144 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
145 if (ret)
Richard Zhao717071d2012-04-27 15:02:56 +0800146 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800147 data->card.num_links = 1;
148 data->card.dai_link = &data->dai;
Shawn Guo172b4c5c2012-03-30 00:13:03 +0800149 data->card.dapm_widgets = imx_sgtl5000_dapm_widgets;
150 data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets);
Shawn Guoc4483032012-03-16 16:56:44 +0800151
152 ret = snd_soc_register_card(&data->card);
153 if (ret) {
154 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
Richard Zhao717071d2012-04-27 15:02:56 +0800155 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800156 }
157
158 platform_set_drvdata(pdev, data);
Richard Zhao717071d2012-04-27 15:02:56 +0800159fail:
160 if (ssi_np)
161 of_node_put(ssi_np);
162 if (codec_np)
163 of_node_put(codec_np);
Shawn Guoc4483032012-03-16 16:56:44 +0800164
Richard Zhao717071d2012-04-27 15:02:56 +0800165 return ret;
Shawn Guoc4483032012-03-16 16:56:44 +0800166}
167
168static int __devexit imx_sgtl5000_remove(struct platform_device *pdev)
169{
170 struct imx_sgtl5000_data *data = platform_get_drvdata(pdev);
171
172 snd_soc_unregister_card(&data->card);
173
174 return 0;
175}
176
177static const struct of_device_id imx_sgtl5000_dt_ids[] = {
178 { .compatible = "fsl,imx-audio-sgtl5000", },
179 { /* sentinel */ }
180};
181MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids);
182
183static struct platform_driver imx_sgtl5000_driver = {
184 .driver = {
185 .name = "imx-sgtl5000",
186 .owner = THIS_MODULE,
187 .of_match_table = imx_sgtl5000_dt_ids,
188 },
189 .probe = imx_sgtl5000_probe,
190 .remove = __devexit_p(imx_sgtl5000_remove),
191};
192module_platform_driver(imx_sgtl5000_driver);
193
194MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
195MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
196MODULE_LICENSE("GPL v2");
197MODULE_ALIAS("platform:imx-sgtl5000");