blob: b1d9b5ebeeeb77d5028a1bd183b4828e7737d78d [file] [log] [blame]
Dong Aishengfcb5e472011-07-21 12:36:57 +08001/*
2 * Copyright 2011 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/module.h>
20#include <linux/device.h>
Shawn Guoe9681942012-05-11 22:24:18 +080021#include <linux/of.h>
22#include <linux/of_device.h>
Dong Aishengfcb5e472011-07-21 12:36:57 +080023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/soc.h>
26#include <sound/jack.h>
27#include <sound/soc-dapm.h>
28#include <asm/mach-types.h>
29
30#include "../codecs/sgtl5000.h"
31#include "mxs-saif.h"
32
33static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream,
34 struct snd_pcm_hw_params *params)
35{
36 struct snd_soc_pcm_runtime *rtd = substream->private_data;
37 struct snd_soc_dai *codec_dai = rtd->codec_dai;
38 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
39 unsigned int rate = params_rate(params);
40 u32 dai_format, mclk;
41 int ret;
42
43 /* sgtl5000 does not support 512*rate when in 96000 fs */
44 switch (rate) {
45 case 96000:
46 mclk = 256 * rate;
47 break;
48 default:
49 mclk = 512 * rate;
50 break;
51 }
52
53 /* Sgtl5000 sysclk should be >= 8MHz and <= 27M */
54 if (mclk < 8000000 || mclk > 27000000)
55 return -EINVAL;
56
57 /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
58 ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
59 if (ret)
60 return ret;
61
62 /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
63 ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
64 if (ret)
65 return ret;
66
67 /* set codec to slave mode */
68 dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
69 SND_SOC_DAIFMT_CBS_CFS;
70
71 /* set codec DAI configuration */
72 ret = snd_soc_dai_set_fmt(codec_dai, dai_format);
73 if (ret)
74 return ret;
75
76 /* set cpu DAI configuration */
77 ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
78 if (ret)
79 return ret;
80
81 return 0;
82}
83
84static struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
85 .hw_params = mxs_sgtl5000_hw_params,
86};
87
88static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
89 {
Dong Aisheng78a262c2011-08-22 00:02:46 +080090 .name = "HiFi Tx",
Dong Aishengfcb5e472011-07-21 12:36:57 +080091 .stream_name = "HiFi Playback",
92 .codec_dai_name = "sgtl5000",
93 .codec_name = "sgtl5000.0-000a",
94 .cpu_dai_name = "mxs-saif.0",
Shawn Guo4da3fe72012-05-11 22:24:16 +080095 .platform_name = "mxs-saif.0",
Dong Aishengfcb5e472011-07-21 12:36:57 +080096 .ops = &mxs_sgtl5000_hifi_ops,
Dong Aisheng78a262c2011-08-22 00:02:46 +080097 }, {
98 .name = "HiFi Rx",
99 .stream_name = "HiFi Capture",
100 .codec_dai_name = "sgtl5000",
101 .codec_name = "sgtl5000.0-000a",
102 .cpu_dai_name = "mxs-saif.1",
Shawn Guo4da3fe72012-05-11 22:24:16 +0800103 .platform_name = "mxs-saif.1",
Dong Aisheng78a262c2011-08-22 00:02:46 +0800104 .ops = &mxs_sgtl5000_hifi_ops,
Dong Aishengfcb5e472011-07-21 12:36:57 +0800105 },
106};
107
108static struct snd_soc_card mxs_sgtl5000 = {
109 .name = "mxs_sgtl5000",
Lothar Waßmanna5b68342011-12-28 20:06:21 +0800110 .owner = THIS_MODULE,
Dong Aishengfcb5e472011-07-21 12:36:57 +0800111 .dai_link = mxs_sgtl5000_dai,
112 .num_links = ARRAY_SIZE(mxs_sgtl5000_dai),
113};
114
Bill Pembertonfd582732012-12-07 09:26:27 -0500115static int mxs_sgtl5000_probe_dt(struct platform_device *pdev)
Shawn Guoe9681942012-05-11 22:24:18 +0800116{
117 struct device_node *np = pdev->dev.of_node;
118 struct device_node *saif_np[2], *codec_np;
119 int i, ret = 0;
120
121 if (!np)
122 return 1; /* no device tree */
123
124 saif_np[0] = of_parse_phandle(np, "saif-controllers", 0);
125 saif_np[1] = of_parse_phandle(np, "saif-controllers", 1);
126 codec_np = of_parse_phandle(np, "audio-codec", 0);
127 if (!saif_np[0] || !saif_np[1] || !codec_np) {
128 dev_err(&pdev->dev, "phandle missing or invalid\n");
129 return -EINVAL;
130 }
131
132 for (i = 0; i < 2; i++) {
133 mxs_sgtl5000_dai[i].codec_name = NULL;
134 mxs_sgtl5000_dai[i].codec_of_node = codec_np;
135 mxs_sgtl5000_dai[i].cpu_dai_name = NULL;
Stephen Warrenbc926572012-05-25 18:22:11 -0600136 mxs_sgtl5000_dai[i].cpu_of_node = saif_np[i];
Shawn Guoe9681942012-05-11 22:24:18 +0800137 mxs_sgtl5000_dai[i].platform_name = NULL;
138 mxs_sgtl5000_dai[i].platform_of_node = saif_np[i];
139 }
140
141 of_node_put(codec_np);
142 of_node_put(saif_np[0]);
143 of_node_put(saif_np[1]);
144
145 return ret;
146}
147
Bill Pembertonfd582732012-12-07 09:26:27 -0500148static int mxs_sgtl5000_probe(struct platform_device *pdev)
Dong Aishengfcb5e472011-07-21 12:36:57 +0800149{
150 struct snd_soc_card *card = &mxs_sgtl5000;
151 int ret;
152
Shawn Guoe9681942012-05-11 22:24:18 +0800153 ret = mxs_sgtl5000_probe_dt(pdev);
154 if (ret < 0)
155 return ret;
156
Dong Aishengfcb5e472011-07-21 12:36:57 +0800157 /*
158 * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
159 * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
160 * should be >= 8MHz and <= 27M.
161 */
162 ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
163 if (ret)
164 return ret;
165
166 card->dev = &pdev->dev;
167 platform_set_drvdata(pdev, card);
168
169 ret = snd_soc_register_card(card);
170 if (ret) {
171 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
172 ret);
173 return ret;
174 }
175
176 return 0;
177}
178
Bill Pembertonfd582732012-12-07 09:26:27 -0500179static int mxs_sgtl5000_remove(struct platform_device *pdev)
Dong Aishengfcb5e472011-07-21 12:36:57 +0800180{
181 struct snd_soc_card *card = platform_get_drvdata(pdev);
182
183 mxs_saif_put_mclk(0);
184
185 snd_soc_unregister_card(card);
186
187 return 0;
188}
189
Shawn Guoe9681942012-05-11 22:24:18 +0800190static const struct of_device_id mxs_sgtl5000_dt_ids[] = {
191 { .compatible = "fsl,mxs-audio-sgtl5000", },
192 { /* sentinel */ }
193};
194MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids);
195
Dong Aishengfcb5e472011-07-21 12:36:57 +0800196static struct platform_driver mxs_sgtl5000_audio_driver = {
197 .driver = {
198 .name = "mxs-sgtl5000",
199 .owner = THIS_MODULE,
Shawn Guoe9681942012-05-11 22:24:18 +0800200 .of_match_table = mxs_sgtl5000_dt_ids,
Dong Aishengfcb5e472011-07-21 12:36:57 +0800201 },
202 .probe = mxs_sgtl5000_probe,
Bill Pembertonfd582732012-12-07 09:26:27 -0500203 .remove = mxs_sgtl5000_remove,
Dong Aishengfcb5e472011-07-21 12:36:57 +0800204};
205
Axel Lin85aa0962011-11-24 14:21:29 +0800206module_platform_driver(mxs_sgtl5000_audio_driver);
Dong Aishengfcb5e472011-07-21 12:36:57 +0800207
208MODULE_AUTHOR("Freescale Semiconductor, Inc.");
209MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
210MODULE_LICENSE("GPL");
Lothar Waßmann9fd369b2011-12-09 14:38:12 +0100211MODULE_ALIAS("platform:mxs-sgtl5000");