blob: 376ca351e0925cb2eaefbccfe0522a6983578cdb [file] [log] [blame]
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -05001/*
Peter Ujfalusi13f81d62011-12-07 15:54:50 +02002 * omap-abe-twl6040.c -- SoC audio for TI OMAP based boards with ABE and
3 * twl6040 codec
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -05004 *
Peter Ujfalusi8747a6b2011-12-07 15:31:26 +02005 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -05006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 *
21 */
22
23#include <linux/clk.h>
24#include <linux/platform_device.h>
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -050025#include <linux/mfd/twl6040.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040026#include <linux/module.h>
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -050027
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/soc.h>
Jorge Eduardo Candelaria96dc2272010-12-10 20:45:19 -060031#include <sound/jack.h>
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050032
33#include <asm/mach-types.h>
34#include <plat/hardware.h>
35#include <plat/mux.h>
36
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +020037#include "omap-dmic.h"
Misael Lopez Cruzf5f9d7b2011-07-05 19:50:45 +030038#include "omap-mcpdm.h"
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050039#include "omap-pcm.h"
40#include "../codecs/twl6040.h"
41
Peter Ujfalusi13f81d62011-12-07 15:54:50 +020042static int omap_abe_hw_params(struct snd_pcm_substream *substream,
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050043 struct snd_pcm_hw_params *params)
44{
45 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050047 int clk_id, freq;
48 int ret;
49
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030050 clk_id = twl6040_get_clk_id(rtd->codec);
51 if (clk_id == TWL6040_SYSCLK_SEL_HPPLL)
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050052 freq = 38400000;
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030053 else if (clk_id == TWL6040_SYSCLK_SEL_LPPLL)
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050054 freq = 32768;
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030055 else
56 return -EINVAL;
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050057
58 /* set the codec mclk */
59 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, freq,
60 SND_SOC_CLOCK_IN);
61 if (ret) {
62 printk(KERN_ERR "can't set codec system clock\n");
63 return ret;
64 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000065 return ret;
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050066}
67
Peter Ujfalusi13f81d62011-12-07 15:54:50 +020068static struct snd_soc_ops omap_abe_ops = {
69 .hw_params = omap_abe_hw_params,
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -050070};
71
Peter Ujfalusi13f81d62011-12-07 15:54:50 +020072static int omap_abe_dmic_hw_params(struct snd_pcm_substream *substream,
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +020073 struct snd_pcm_hw_params *params)
74{
75 struct snd_soc_pcm_runtime *rtd = substream->private_data;
76 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
77 int ret = 0;
78
79 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_SYSCLK_PAD_CLKS,
80 19200000, SND_SOC_CLOCK_IN);
81 if (ret < 0) {
82 printk(KERN_ERR "can't set DMIC cpu system clock\n");
83 return ret;
84 }
85 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_ABE_DMIC_CLK, 2400000,
86 SND_SOC_CLOCK_OUT);
87 if (ret < 0) {
88 printk(KERN_ERR "can't set DMIC output clock\n");
89 return ret;
90 }
91 return 0;
92}
93
Peter Ujfalusi13f81d62011-12-07 15:54:50 +020094static struct snd_soc_ops omap_abe_dmic_ops = {
95 .hw_params = omap_abe_dmic_hw_params,
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +020096};
97
Jorge Eduardo Candelaria96dc2272010-12-10 20:45:19 -060098/* Headset jack */
99static struct snd_soc_jack hs_jack;
100
101/*Headset jack detection DAPM pins */
102static struct snd_soc_jack_pin hs_jack_pins[] = {
103 {
104 .pin = "Headset Mic",
105 .mask = SND_JACK_MICROPHONE,
106 },
107 {
108 .pin = "Headset Stereophone",
109 .mask = SND_JACK_HEADPHONE,
110 },
111};
112
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500113/* SDP4430 machine DAPM */
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200114static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500115 SND_SOC_DAPM_MIC("Ext Mic", NULL),
116 SND_SOC_DAPM_SPK("Ext Spk", NULL),
117 SND_SOC_DAPM_MIC("Headset Mic", NULL),
118 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
Jorge Eduardo Candelaria7254e2b2010-05-18 12:44:17 -0500119 SND_SOC_DAPM_SPK("Earphone Spk", NULL),
Ujfalusi, Peter5382ffb2011-09-30 11:39:50 +0300120 SND_SOC_DAPM_INPUT("FM Stereo In"),
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500121};
122
123static const struct snd_soc_dapm_route audio_map[] = {
124 /* External Mics: MAINMIC, SUBMIC with bias*/
125 {"MAINMIC", NULL, "Main Mic Bias"},
126 {"SUBMIC", NULL, "Main Mic Bias"},
127 {"Main Mic Bias", NULL, "Ext Mic"},
128
129 /* External Speakers: HFL, HFR */
130 {"Ext Spk", NULL, "HFL"},
131 {"Ext Spk", NULL, "HFR"},
132
133 /* Headset Mic: HSMIC with bias */
134 {"HSMIC", NULL, "Headset Mic Bias"},
135 {"Headset Mic Bias", NULL, "Headset Mic"},
136
137 /* Headset Stereophone (Headphone): HSOL, HSOR */
138 {"Headset Stereophone", NULL, "HSOL"},
139 {"Headset Stereophone", NULL, "HSOR"},
Jorge Eduardo Candelaria7254e2b2010-05-18 12:44:17 -0500140
141 /* Earphone speaker */
142 {"Earphone Spk", NULL, "EP"},
Jorge Eduardo Candelaria23ac3b62010-12-08 10:55:05 -0600143
144 /* Aux/FM Stereo In: AFML, AFMR */
Ujfalusi, Peter5382ffb2011-09-30 11:39:50 +0300145 {"AFML", NULL, "FM Stereo In"},
146 {"AFMR", NULL, "FM Stereo In"},
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500147};
148
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200149static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500150{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000151 struct snd_soc_codec *codec = rtd->codec;
Peter Ujfalusi7bf3d922011-09-26 16:05:59 +0300152 int ret, hs_trim;
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500153
Peter Ujfalusi7bf3d922011-09-26 16:05:59 +0300154 /*
155 * Configure McPDM offset cancellation based on the HSOTRIM value from
156 * twl6040.
157 */
158 hs_trim = twl6040_get_trim_value(codec, TWL6040_TRIM_HSOTRIM);
159 omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
160 TWL6040_HSF_TRIM_RIGHT(hs_trim));
161
Jorge Eduardo Candelaria96dc2272010-12-10 20:45:19 -0600162 /* Headset jack detection */
163 ret = snd_soc_jack_new(codec, "Headset Jack",
164 SND_JACK_HEADSET, &hs_jack);
165 if (ret)
166 return ret;
167
168 ret = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pins),
169 hs_jack_pins);
170
171 if (machine_is_omap_4430sdp())
172 twl6040_hs_jack_detect(codec, &hs_jack, SND_JACK_HEADSET);
173 else
174 snd_soc_jack_report(&hs_jack, SND_JACK_HEADSET, SND_JACK_HEADSET);
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500175
176 return ret;
177}
178
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200179static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +0200180 SND_SOC_DAPM_MIC("Digital Mic", NULL),
181};
182
183static const struct snd_soc_dapm_route dmic_audio_map[] = {
184 {"DMic", NULL, "Digital Mic1 Bias"},
185 {"Digital Mic1 Bias", NULL, "Digital Mic"},
186};
187
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200188static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +0200189{
190 struct snd_soc_codec *codec = rtd->codec;
191 struct snd_soc_dapm_context *dapm = &codec->dapm;
192 int ret;
193
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200194 ret = snd_soc_dapm_new_controls(dapm, dmic_dapm_widgets,
195 ARRAY_SIZE(dmic_dapm_widgets));
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +0200196 if (ret)
197 return ret;
198
199 return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
200 ARRAY_SIZE(dmic_audio_map));
201}
202
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500203/* Digital audio interface glue - connects codec <--> CPU */
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +0200204static struct snd_soc_dai_link sdp4430_dai[] = {
205 {
206 .name = "TWL6040",
207 .stream_name = "TWL6040",
208 .cpu_dai_name = "omap-mcpdm",
209 .codec_dai_name = "twl6040-legacy",
210 .platform_name = "omap-pcm-audio",
211 .codec_name = "twl6040-codec",
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200212 .init = omap_abe_twl6040_init,
213 .ops = &omap_abe_ops,
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +0200214 },
215 {
216 .name = "DMIC",
217 .stream_name = "DMIC Capture",
218 .cpu_dai_name = "omap-dmic",
219 .codec_dai_name = "dmic-hifi",
220 .platform_name = "omap-pcm-audio",
221 .codec_name = "dmic-codec",
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200222 .init = omap_abe_dmic_init,
223 .ops = &omap_abe_dmic_ops,
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +0200224 },
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500225};
226
227/* Audio machine driver */
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200228static struct snd_soc_card omap_abe_card = {
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500229 .name = "SDP4430",
Axel Linb425b882011-12-22 11:08:59 +0800230 .owner = THIS_MODULE,
Peter Ujfalusi6524c8e2011-11-28 15:45:43 +0200231 .dai_link = sdp4430_dai,
232 .num_links = ARRAY_SIZE(sdp4430_dai),
Peter Ujfalusifc8e5b42011-10-10 15:34:15 +0300233
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200234 .dapm_widgets = twl6040_dapm_widgets,
235 .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
Peter Ujfalusifc8e5b42011-10-10 15:34:15 +0300236 .dapm_routes = audio_map,
237 .num_dapm_routes = ARRAY_SIZE(audio_map),
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500238};
239
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200240static struct platform_device *omap_abe_snd_device;
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500241
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200242static int __init omap_abe_soc_init(void)
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500243{
244 int ret;
245
Jarkko Nikulaec588ae2010-10-06 16:47:26 +0300246 if (!machine_is_omap_4430sdp())
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500247 return -ENODEV;
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500248 printk(KERN_INFO "SDP4430 SoC init\n");
249
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200250 omap_abe_snd_device = platform_device_alloc("soc-audio", -1);
251 if (!omap_abe_snd_device) {
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500252 printk(KERN_ERR "Platform device allocation failed\n");
253 return -ENOMEM;
254 }
255
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200256 platform_set_drvdata(omap_abe_snd_device, &omap_abe_card);
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500257
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200258 ret = platform_device_add(omap_abe_snd_device);
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500259 if (ret)
260 goto err;
261
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500262 return 0;
263
264err:
265 printk(KERN_ERR "Unable to add platform device\n");
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200266 platform_device_put(omap_abe_snd_device);
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500267 return ret;
268}
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200269module_init(omap_abe_soc_init);
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500270
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200271static void __exit omap_abe_soc_exit(void)
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500272{
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200273 platform_device_unregister(omap_abe_snd_device);
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500274}
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200275module_exit(omap_abe_soc_exit);
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500276
Peter Ujfalusi8747a6b2011-12-07 15:31:26 +0200277MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
Peter Ujfalusi13f81d62011-12-07 15:54:50 +0200278MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
Misael Lopez Cruz5e64d6a2010-05-17 19:53:10 -0500279MODULE_LICENSE("GPL");
280