blob: 076bec606d78aabbdecd4e398522fcc03bb2e197 [file] [log] [blame]
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +02001/*
2 * omap3pandora.c -- SoC audio for Pandora Handheld Console
3 *
4 * Author: GraÅžvydas Ignotas <notasas@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
22#include <linux/clk.h>
23#include <linux/platform_device.h>
24#include <linux/gpio.h>
25#include <linux/delay.h>
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020026#include <linux/regulator/consumer.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020028
29#include <sound/core.h>
30#include <sound/pcm.h>
31#include <sound/soc.h>
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020032
33#include <asm/mach-types.h>
Arnd Bergmann22037472012-08-24 15:21:06 +020034#include <linux/platform_data/asoc-ti-mcbsp.h>
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020035
36#include "omap-mcbsp.h"
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020037
38#define OMAP3_PANDORA_DAC_POWER_GPIO 118
39#define OMAP3_PANDORA_AMP_POWER_GPIO 14
40
41#define PREFIX "ASoC omap3pandora: "
42
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020043static struct regulator *omap3pandora_dac_reg;
44
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +030045static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
46 struct snd_pcm_hw_params *params)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020047{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020048 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000049 struct snd_soc_dai *codec_dai = rtd->codec_dai;
50 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020051 int ret;
52
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020053 /* Set the codec system clock for DAC and ADC */
54 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
55 SND_SOC_CLOCK_IN);
56 if (ret < 0) {
57 pr_err(PREFIX "can't set codec system clock\n");
58 return ret;
59 }
60
61 /* Set McBSP clock to external */
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020062 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
63 256 * params_rate(params),
64 SND_SOC_CLOCK_IN);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020065 if (ret < 0) {
66 pr_err(PREFIX "can't set cpu system clock\n");
67 return ret;
68 }
69
70 ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
71 if (ret < 0) {
72 pr_err(PREFIX "can't set SRG clock divider\n");
73 return ret;
74 }
75
76 return 0;
77}
78
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020079static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
80 struct snd_kcontrol *k, int event)
81{
Mark Browndd194b42013-03-02 15:47:55 +080082 int ret;
83
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020084 /*
85 * The PCM1773 DAC datasheet requires 1ms delay between switching
86 * VCC power on/off and /PD pin high/low
87 */
88 if (SND_SOC_DAPM_EVENT_ON(event)) {
Mark Browndd194b42013-03-02 15:47:55 +080089 ret = regulator_enable(omap3pandora_dac_reg);
90 if (ret) {
Peter Ujfalusie37e0432013-03-05 15:24:16 +010091 dev_err(w->dapm->dev, "Failed to power DAC: %d\n", ret);
Mark Browndd194b42013-03-02 15:47:55 +080092 return ret;
93 }
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020094 mdelay(1);
95 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
96 } else {
97 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
98 mdelay(1);
99 regulator_disable(omap3pandora_dac_reg);
100 }
101
102 return 0;
103}
104
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200105static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
106 struct snd_kcontrol *k, int event)
107{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200108 if (SND_SOC_DAPM_EVENT_ON(event))
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200109 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200110 else
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200111 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200112
113 return 0;
114}
115
116/*
117 * Audio paths on Pandora board:
118 *
119 * |O| ---> PCM DAC +-> AMP -> Headphone Jack
120 * |M| A +--------> Line Out
121 * |A| <~~clk~~+
122 * |P| <--- TWL4030 <--------- Line In and MICs
123 */
Lars-Peter Clausenb2e69052014-03-12 15:27:34 +0100124static const struct snd_soc_dapm_widget omap3pandora_dapm_widgets[] = {
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200125 SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
126 0, 0, omap3pandora_dac_event,
127 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200128 SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
129 0, 0, NULL, 0, omap3pandora_hp_event,
130 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
131 SND_SOC_DAPM_HP("Headphone Jack", NULL),
132 SND_SOC_DAPM_LINE("Line Out", NULL),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200133
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200134 SND_SOC_DAPM_MIC("Mic (internal)", NULL),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200135 SND_SOC_DAPM_MIC("Mic (external)", NULL),
136 SND_SOC_DAPM_LINE("Line In", NULL),
137};
138
Lars-Peter Clausenb2e69052014-03-12 15:27:34 +0100139static const struct snd_soc_dapm_route omap3pandora_map[] = {
Grazvydas Ignotas3b9447f2010-02-05 00:55:33 +0200140 {"PCM DAC", NULL, "APLL Enable"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200141 {"Headphone Amplifier", NULL, "PCM DAC"},
142 {"Line Out", NULL, "PCM DAC"},
143 {"Headphone Jack", NULL, "Headphone Amplifier"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200144
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200145 {"AUXL", NULL, "Line In"},
146 {"AUXR", NULL, "Line In"},
147
Peter Ujfalusie04d6e52012-12-31 11:51:45 +0100148 {"MAINMIC", NULL, "Mic (internal)"},
149 {"Mic (internal)", NULL, "Mic Bias 1"},
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200150
Peter Ujfalusie04d6e52012-12-31 11:51:45 +0100151 {"SUBMIC", NULL, "Mic (external)"},
152 {"Mic (external)", NULL, "Mic Bias 2"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200153};
154
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000155static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200156{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000157 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200158 struct snd_soc_dapm_context *dapm = &codec->dapm;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200159
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200160 /* All TWL4030 output pins are floating */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200161 snd_soc_dapm_nc_pin(dapm, "EARPIECE");
162 snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
163 snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
164 snd_soc_dapm_nc_pin(dapm, "HSOL");
165 snd_soc_dapm_nc_pin(dapm, "HSOR");
166 snd_soc_dapm_nc_pin(dapm, "CARKITL");
167 snd_soc_dapm_nc_pin(dapm, "CARKITR");
168 snd_soc_dapm_nc_pin(dapm, "HFL");
169 snd_soc_dapm_nc_pin(dapm, "HFR");
170 snd_soc_dapm_nc_pin(dapm, "VIBRA");
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200171
Lars-Peter Clausenb2e69052014-03-12 15:27:34 +0100172 return 0;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200173}
174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000175static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200176{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200178 struct snd_soc_dapm_context *dapm = &codec->dapm;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200179
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200180 /* Not comnnected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200181 snd_soc_dapm_nc_pin(dapm, "HSMIC");
182 snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
183 snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
184 snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
Grazvydas Ignotas7f185342008-12-23 12:04:48 +0200185
Lars-Peter Clausenb2e69052014-03-12 15:27:34 +0100186 return 0;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200187}
188
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300189static struct snd_soc_ops omap3pandora_ops = {
190 .hw_params = omap3pandora_hw_params,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200191};
192
193/* Digital audio interface glue - connects codec <--> CPU */
194static struct snd_soc_dai_link omap3pandora_dai[] = {
195 {
196 .name = "PCM1773",
197 .stream_name = "HiFi Out",
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200198 .cpu_dai_name = "omap-mcbsp.2",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000199 .codec_dai_name = "twl4030-hifi",
Peter Ujfalusibffbe632014-04-16 15:46:26 +0300200 .platform_name = "omap-mcbsp.2",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000201 .codec_name = "twl4030-codec",
Jarkko Nikulacf9feff2011-09-30 16:07:45 +0300202 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
203 SND_SOC_DAIFMT_CBS_CFS,
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300204 .ops = &omap3pandora_ops,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200205 .init = omap3pandora_out_init,
206 }, {
207 .name = "TWL4030",
208 .stream_name = "Line/Mic In",
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200209 .cpu_dai_name = "omap-mcbsp.4",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000210 .codec_dai_name = "twl4030-hifi",
Peter Ujfalusibffbe632014-04-16 15:46:26 +0300211 .platform_name = "omap-mcbsp.4",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000212 .codec_name = "twl4030-codec",
Jarkko Nikulacf9feff2011-09-30 16:07:45 +0300213 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
214 SND_SOC_DAIFMT_CBS_CFS,
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300215 .ops = &omap3pandora_ops,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200216 .init = omap3pandora_in_init,
217 }
218};
219
220/* SoC card */
221static struct snd_soc_card snd_soc_card_omap3pandora = {
222 .name = "omap3pandora",
Axel Linb425b882011-12-22 11:08:59 +0800223 .owner = THIS_MODULE,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200224 .dai_link = omap3pandora_dai,
225 .num_links = ARRAY_SIZE(omap3pandora_dai),
Lars-Peter Clausenb2e69052014-03-12 15:27:34 +0100226
227 .dapm_widgets = omap3pandora_dapm_widgets,
228 .num_dapm_widgets = ARRAY_SIZE(omap3pandora_dapm_widgets),
229 .dapm_routes = omap3pandora_map,
230 .num_dapm_routes = ARRAY_SIZE(omap3pandora_map),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200231};
232
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200233static struct platform_device *omap3pandora_snd_device;
234
235static int __init omap3pandora_soc_init(void)
236{
237 int ret;
238
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200239 if (!machine_is_omap3_pandora())
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200240 return -ENODEV;
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200241
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200242 pr_info("OMAP3 Pandora SoC init\n");
243
244 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
245 if (ret) {
246 pr_err(PREFIX "Failed to get DAC power GPIO\n");
247 return ret;
248 }
249
250 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
251 if (ret) {
252 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
253 goto fail0;
254 }
255
256 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
257 if (ret) {
258 pr_err(PREFIX "Failed to get amp power GPIO\n");
259 goto fail0;
260 }
261
262 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
263 if (ret) {
264 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
265 goto fail1;
266 }
267
268 omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
269 if (omap3pandora_snd_device == NULL) {
270 pr_err(PREFIX "Platform device allocation failed\n");
271 ret = -ENOMEM;
272 goto fail1;
273 }
274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000275 platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200276
277 ret = platform_device_add(omap3pandora_snd_device);
278 if (ret) {
279 pr_err(PREFIX "Unable to add platform device\n");
280 goto fail2;
281 }
282
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200283 omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
284 if (IS_ERR(omap3pandora_dac_reg)) {
285 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
286 dev_name(&omap3pandora_snd_device->dev),
287 PTR_ERR(omap3pandora_dac_reg));
Axel Lin5c12d202010-11-24 15:20:48 +0800288 ret = PTR_ERR(omap3pandora_dac_reg);
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200289 goto fail3;
290 }
291
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200292 return 0;
293
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200294fail3:
295 platform_device_del(omap3pandora_snd_device);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200296fail2:
297 platform_device_put(omap3pandora_snd_device);
298fail1:
299 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
300fail0:
301 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
302 return ret;
303}
304module_init(omap3pandora_soc_init);
305
306static void __exit omap3pandora_soc_exit(void)
307{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200308 regulator_put(omap3pandora_dac_reg);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200309 platform_device_unregister(omap3pandora_snd_device);
310 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
311 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
312}
313module_exit(omap3pandora_soc_exit);
314
315MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
316MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
317MODULE_LICENSE("GPL");