blob: e1a7441ec1337dcba454fd6a8c2e91b732cba371 [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");
106 return -EINVAL;
107 }
108
109 ssi_pdev = of_find_device_by_node(ssi_np);
110 if (!ssi_pdev) {
111 dev_err(&pdev->dev, "failed to find SSI platform device\n");
112 return -EINVAL;
113 }
114
115 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
116 if (!data)
117 return -ENOMEM;
118
119 ret = of_property_read_u32(codec_np, "clock-frequency",
120 &data->clk_frequency);
121 if (ret) {
122 dev_err(&pdev->dev, "clock-frequency missing or invalid\n");
123 return ret;
124 }
125
126 data->dai.name = "HiFi";
127 data->dai.stream_name = "HiFi";
128 data->dai.codec_dai_name = "sgtl5000";
129 data->dai.codec_of_node = codec_np;
130 data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
131 data->dai.platform_name = "imx-pcm-audio";
132 data->dai.init = &imx_sgtl5000_dai_init;
133 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
134 SND_SOC_DAIFMT_CBM_CFM;
135
136 data->card.dev = &pdev->dev;
137 ret = snd_soc_of_parse_card_name(&data->card, "model");
138 if (ret)
139 return ret;
Shawn Guo172b4c5c2012-03-30 00:13:03 +0800140 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
141 if (ret)
142 return ret;
Shawn Guoc4483032012-03-16 16:56:44 +0800143 data->card.num_links = 1;
144 data->card.dai_link = &data->dai;
Shawn Guo172b4c5c2012-03-30 00:13:03 +0800145 data->card.dapm_widgets = imx_sgtl5000_dapm_widgets;
146 data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets);
Shawn Guoc4483032012-03-16 16:56:44 +0800147
148 ret = snd_soc_register_card(&data->card);
149 if (ret) {
150 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
151 return ret;
152 }
153
154 platform_set_drvdata(pdev, data);
155 of_node_put(ssi_np);
156 of_node_put(codec_np);
157
158 return 0;
159}
160
161static int __devexit imx_sgtl5000_remove(struct platform_device *pdev)
162{
163 struct imx_sgtl5000_data *data = platform_get_drvdata(pdev);
164
165 snd_soc_unregister_card(&data->card);
166
167 return 0;
168}
169
170static const struct of_device_id imx_sgtl5000_dt_ids[] = {
171 { .compatible = "fsl,imx-audio-sgtl5000", },
172 { /* sentinel */ }
173};
174MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids);
175
176static struct platform_driver imx_sgtl5000_driver = {
177 .driver = {
178 .name = "imx-sgtl5000",
179 .owner = THIS_MODULE,
180 .of_match_table = imx_sgtl5000_dt_ids,
181 },
182 .probe = imx_sgtl5000_probe,
183 .remove = __devexit_p(imx_sgtl5000_remove),
184};
185module_platform_driver(imx_sgtl5000_driver);
186
187MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
188MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
189MODULE_LICENSE("GPL v2");
190MODULE_ALIAS("platform:imx-sgtl5000");