xref: /linux/sound/soc/mxs/mxs-sgtl5000.c (revision f2ee442115c9b6219083c019939a9cc0c9abb2f8)
1 /*
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>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/soc.h>
24 #include <sound/jack.h>
25 #include <sound/soc-dapm.h>
26 #include <asm/mach-types.h>
27 
28 #include "../codecs/sgtl5000.h"
29 #include "mxs-saif.h"
30 
31 static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream,
32 	struct snd_pcm_hw_params *params)
33 {
34 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
35 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
36 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
37 	unsigned int rate = params_rate(params);
38 	u32 dai_format, mclk;
39 	int ret;
40 
41 	/* sgtl5000 does not support 512*rate when in 96000 fs */
42 	switch (rate) {
43 	case 96000:
44 		mclk = 256 * rate;
45 		break;
46 	default:
47 		mclk = 512 * rate;
48 		break;
49 	}
50 
51 	/* Sgtl5000 sysclk should be >= 8MHz and <= 27M */
52 	if (mclk < 8000000 || mclk > 27000000)
53 		return -EINVAL;
54 
55 	/* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
56 	ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
57 	if (ret)
58 		return ret;
59 
60 	/* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
61 	ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
62 	if (ret)
63 		return ret;
64 
65 	/* set codec to slave mode */
66 	dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
67 			SND_SOC_DAIFMT_CBS_CFS;
68 
69 	/* set codec DAI configuration */
70 	ret = snd_soc_dai_set_fmt(codec_dai, dai_format);
71 	if (ret)
72 		return ret;
73 
74 	/* set cpu DAI configuration */
75 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
76 	if (ret)
77 		return ret;
78 
79 	return 0;
80 }
81 
82 static struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
83 	.hw_params = mxs_sgtl5000_hw_params,
84 };
85 
86 static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
87 	{
88 		.name		= "HiFi Tx",
89 		.stream_name	= "HiFi Playback",
90 		.codec_dai_name	= "sgtl5000",
91 		.codec_name	= "sgtl5000.0-000a",
92 		.cpu_dai_name	= "mxs-saif.0",
93 		.platform_name	= "mxs-pcm-audio.0",
94 		.ops		= &mxs_sgtl5000_hifi_ops,
95 	}, {
96 		.name		= "HiFi Rx",
97 		.stream_name	= "HiFi Capture",
98 		.codec_dai_name	= "sgtl5000",
99 		.codec_name	= "sgtl5000.0-000a",
100 		.cpu_dai_name	= "mxs-saif.1",
101 		.platform_name	= "mxs-pcm-audio.1",
102 		.ops		= &mxs_sgtl5000_hifi_ops,
103 	},
104 };
105 
106 static struct snd_soc_card mxs_sgtl5000 = {
107 	.name		= "mxs_sgtl5000",
108 	.dai_link	= mxs_sgtl5000_dai,
109 	.num_links	= ARRAY_SIZE(mxs_sgtl5000_dai),
110 };
111 
112 static int __devinit mxs_sgtl5000_probe(struct platform_device *pdev)
113 {
114 	struct snd_soc_card *card = &mxs_sgtl5000;
115 	int ret;
116 
117 	/*
118 	 * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
119 	 * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
120 	 * should be >= 8MHz and <= 27M.
121 	 */
122 	ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
123 	if (ret)
124 		return ret;
125 
126 	card->dev = &pdev->dev;
127 	platform_set_drvdata(pdev, card);
128 
129 	ret = snd_soc_register_card(card);
130 	if (ret) {
131 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
132 			ret);
133 		return ret;
134 	}
135 
136 	return 0;
137 }
138 
139 static int __devexit mxs_sgtl5000_remove(struct platform_device *pdev)
140 {
141 	struct snd_soc_card *card = platform_get_drvdata(pdev);
142 
143 	mxs_saif_put_mclk(0);
144 
145 	snd_soc_unregister_card(card);
146 
147 	return 0;
148 }
149 
150 static struct platform_driver mxs_sgtl5000_audio_driver = {
151 	.driver = {
152 		.name = "mxs-sgtl5000",
153 		.owner = THIS_MODULE,
154 	},
155 	.probe = mxs_sgtl5000_probe,
156 	.remove = __devexit_p(mxs_sgtl5000_remove),
157 };
158 
159 static int __init mxs_sgtl5000_init(void)
160 {
161 	return platform_driver_register(&mxs_sgtl5000_audio_driver);
162 }
163 module_init(mxs_sgtl5000_init);
164 
165 static void __exit mxs_sgtl5000_exit(void)
166 {
167 	platform_driver_unregister(&mxs_sgtl5000_audio_driver);
168 }
169 module_exit(mxs_sgtl5000_exit);
170 
171 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
172 MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
173 MODULE_LICENSE("GPL");
174