12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
268fb7407SGrazvydas Ignotas /*
368fb7407SGrazvydas Ignotas * omap3pandora.c -- SoC audio for Pandora Handheld Console
468fb7407SGrazvydas Ignotas *
568fb7407SGrazvydas Ignotas * Author: Gražvydas Ignotas <notasas@gmail.com>
668fb7407SGrazvydas Ignotas */
768fb7407SGrazvydas Ignotas
868fb7407SGrazvydas Ignotas #include <linux/clk.h>
968fb7407SGrazvydas Ignotas #include <linux/platform_device.h>
10319e6ac1SLinus Walleij #include <linux/gpio/consumer.h>
1168fb7407SGrazvydas Ignotas #include <linux/delay.h>
12c50749deSGrazvydas Ignotas #include <linux/regulator/consumer.h>
13da155d5bSPaul Gortmaker #include <linux/module.h>
1468fb7407SGrazvydas Ignotas
1568fb7407SGrazvydas Ignotas #include <sound/core.h>
1668fb7407SGrazvydas Ignotas #include <sound/pcm.h>
1768fb7407SGrazvydas Ignotas #include <sound/soc.h>
1868fb7407SGrazvydas Ignotas
1968fb7407SGrazvydas Ignotas #include <asm/mach-types.h>
202203747cSArnd Bergmann #include <linux/platform_data/asoc-ti-mcbsp.h>
2168fb7407SGrazvydas Ignotas
2268fb7407SGrazvydas Ignotas #include "omap-mcbsp.h"
2368fb7407SGrazvydas Ignotas
2468fb7407SGrazvydas Ignotas #define PREFIX "ASoC omap3pandora: "
2568fb7407SGrazvydas Ignotas
26c50749deSGrazvydas Ignotas static struct regulator *omap3pandora_dac_reg;
27319e6ac1SLinus Walleij static struct gpio_desc *dac_power_gpio;
28319e6ac1SLinus Walleij static struct gpio_desc *amp_power_gpio;
29c50749deSGrazvydas Ignotas
omap3pandora_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)304b94dba0SGrazvydas Ignotas static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
314b94dba0SGrazvydas Ignotas struct snd_pcm_hw_params *params)
3268fb7407SGrazvydas Ignotas {
331af52932SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
341af52932SKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
351af52932SKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
3668fb7407SGrazvydas Ignotas int ret;
3768fb7407SGrazvydas Ignotas
3868fb7407SGrazvydas Ignotas /* Set the codec system clock for DAC and ADC */
3968fb7407SGrazvydas Ignotas ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
4068fb7407SGrazvydas Ignotas SND_SOC_CLOCK_IN);
4168fb7407SGrazvydas Ignotas if (ret < 0) {
4268fb7407SGrazvydas Ignotas pr_err(PREFIX "can't set codec system clock\n");
4368fb7407SGrazvydas Ignotas return ret;
4468fb7407SGrazvydas Ignotas }
4568fb7407SGrazvydas Ignotas
4668fb7407SGrazvydas Ignotas /* Set McBSP clock to external */
479e5d86feSJarkko Nikula ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
489e5d86feSJarkko Nikula 256 * params_rate(params),
4968fb7407SGrazvydas Ignotas SND_SOC_CLOCK_IN);
5068fb7407SGrazvydas Ignotas if (ret < 0) {
5168fb7407SGrazvydas Ignotas pr_err(PREFIX "can't set cpu system clock\n");
5268fb7407SGrazvydas Ignotas return ret;
5368fb7407SGrazvydas Ignotas }
5468fb7407SGrazvydas Ignotas
5568fb7407SGrazvydas Ignotas ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
5668fb7407SGrazvydas Ignotas if (ret < 0) {
5768fb7407SGrazvydas Ignotas pr_err(PREFIX "can't set SRG clock divider\n");
5868fb7407SGrazvydas Ignotas return ret;
5968fb7407SGrazvydas Ignotas }
6068fb7407SGrazvydas Ignotas
6168fb7407SGrazvydas Ignotas return 0;
6268fb7407SGrazvydas Ignotas }
6368fb7407SGrazvydas Ignotas
omap3pandora_dac_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)64c50749deSGrazvydas Ignotas static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
65c50749deSGrazvydas Ignotas struct snd_kcontrol *k, int event)
66c50749deSGrazvydas Ignotas {
67dd194b48SMark Brown int ret;
68dd194b48SMark Brown
69c50749deSGrazvydas Ignotas /*
70c50749deSGrazvydas Ignotas * The PCM1773 DAC datasheet requires 1ms delay between switching
71c50749deSGrazvydas Ignotas * VCC power on/off and /PD pin high/low
72c50749deSGrazvydas Ignotas */
73c50749deSGrazvydas Ignotas if (SND_SOC_DAPM_EVENT_ON(event)) {
74dd194b48SMark Brown ret = regulator_enable(omap3pandora_dac_reg);
75dd194b48SMark Brown if (ret) {
76e37e0430SPeter Ujfalusi dev_err(w->dapm->dev, "Failed to power DAC: %d\n", ret);
77dd194b48SMark Brown return ret;
78dd194b48SMark Brown }
79c50749deSGrazvydas Ignotas mdelay(1);
80319e6ac1SLinus Walleij gpiod_set_value(dac_power_gpio, 1);
81c50749deSGrazvydas Ignotas } else {
82319e6ac1SLinus Walleij gpiod_set_value(dac_power_gpio, 0);
83c50749deSGrazvydas Ignotas mdelay(1);
84c50749deSGrazvydas Ignotas regulator_disable(omap3pandora_dac_reg);
85c50749deSGrazvydas Ignotas }
86c50749deSGrazvydas Ignotas
87c50749deSGrazvydas Ignotas return 0;
88c50749deSGrazvydas Ignotas }
89c50749deSGrazvydas Ignotas
omap3pandora_hp_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)9068fb7407SGrazvydas Ignotas static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
9168fb7407SGrazvydas Ignotas struct snd_kcontrol *k, int event)
9268fb7407SGrazvydas Ignotas {
93c50749deSGrazvydas Ignotas if (SND_SOC_DAPM_EVENT_ON(event))
94319e6ac1SLinus Walleij gpiod_set_value(amp_power_gpio, 1);
95c50749deSGrazvydas Ignotas else
96319e6ac1SLinus Walleij gpiod_set_value(amp_power_gpio, 0);
9768fb7407SGrazvydas Ignotas
9868fb7407SGrazvydas Ignotas return 0;
9968fb7407SGrazvydas Ignotas }
10068fb7407SGrazvydas Ignotas
10168fb7407SGrazvydas Ignotas /*
10268fb7407SGrazvydas Ignotas * Audio paths on Pandora board:
10368fb7407SGrazvydas Ignotas *
10468fb7407SGrazvydas Ignotas * |O| ---> PCM DAC +-> AMP -> Headphone Jack
10568fb7407SGrazvydas Ignotas * |M| A +--------> Line Out
10668fb7407SGrazvydas Ignotas * |A| <~~clk~~+
10768fb7407SGrazvydas Ignotas * |P| <--- TWL4030 <--------- Line In and MICs
10868fb7407SGrazvydas Ignotas */
109b2e69054SLars-Peter Clausen static const struct snd_soc_dapm_widget omap3pandora_dapm_widgets[] = {
110c50749deSGrazvydas Ignotas SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
111c50749deSGrazvydas Ignotas 0, 0, omap3pandora_dac_event,
112c50749deSGrazvydas Ignotas SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
11368fb7407SGrazvydas Ignotas SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
11468fb7407SGrazvydas Ignotas 0, 0, NULL, 0, omap3pandora_hp_event,
11568fb7407SGrazvydas Ignotas SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
11668fb7407SGrazvydas Ignotas SND_SOC_DAPM_HP("Headphone Jack", NULL),
11768fb7407SGrazvydas Ignotas SND_SOC_DAPM_LINE("Line Out", NULL),
11868fb7407SGrazvydas Ignotas
1198f008062SGrazvydas Ignotas SND_SOC_DAPM_MIC("Mic (internal)", NULL),
12068fb7407SGrazvydas Ignotas SND_SOC_DAPM_MIC("Mic (external)", NULL),
12168fb7407SGrazvydas Ignotas SND_SOC_DAPM_LINE("Line In", NULL),
12268fb7407SGrazvydas Ignotas };
12368fb7407SGrazvydas Ignotas
124b2e69054SLars-Peter Clausen static const struct snd_soc_dapm_route omap3pandora_map[] = {
1253b9447fbSGrazvydas Ignotas {"PCM DAC", NULL, "APLL Enable"},
12668fb7407SGrazvydas Ignotas {"Headphone Amplifier", NULL, "PCM DAC"},
12768fb7407SGrazvydas Ignotas {"Line Out", NULL, "PCM DAC"},
12868fb7407SGrazvydas Ignotas {"Headphone Jack", NULL, "Headphone Amplifier"},
12968fb7407SGrazvydas Ignotas
1308f008062SGrazvydas Ignotas {"AUXL", NULL, "Line In"},
1318f008062SGrazvydas Ignotas {"AUXR", NULL, "Line In"},
1328f008062SGrazvydas Ignotas
133e04d6e55SPeter Ujfalusi {"MAINMIC", NULL, "Mic (internal)"},
134e04d6e55SPeter Ujfalusi {"Mic (internal)", NULL, "Mic Bias 1"},
1358f008062SGrazvydas Ignotas
136e04d6e55SPeter Ujfalusi {"SUBMIC", NULL, "Mic (external)"},
137e04d6e55SPeter Ujfalusi {"Mic (external)", NULL, "Mic Bias 2"},
13868fb7407SGrazvydas Ignotas };
13968fb7407SGrazvydas Ignotas
omap3pandora_out_init(struct snd_soc_pcm_runtime * rtd)140f0fba2adSLiam Girdwood static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
14168fb7407SGrazvydas Ignotas {
142987731bbSLars-Peter Clausen struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
14368fb7407SGrazvydas Ignotas
1448f008062SGrazvydas Ignotas /* All TWL4030 output pins are floating */
145ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "EARPIECE");
146ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
147ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
148ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "HSOL");
149ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "HSOR");
150ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "CARKITL");
151ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "CARKITR");
152ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "HFL");
153ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "HFR");
154ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "VIBRA");
1558f008062SGrazvydas Ignotas
156b2e69054SLars-Peter Clausen return 0;
15768fb7407SGrazvydas Ignotas }
15868fb7407SGrazvydas Ignotas
omap3pandora_in_init(struct snd_soc_pcm_runtime * rtd)159f0fba2adSLiam Girdwood static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
16068fb7407SGrazvydas Ignotas {
161987731bbSLars-Peter Clausen struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
16268fb7407SGrazvydas Ignotas
1638f008062SGrazvydas Ignotas /* Not comnnected */
164ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "HSMIC");
165ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
166ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
167ce6120ccSLiam Girdwood snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
1687f185340SGrazvydas Ignotas
169b2e69054SLars-Peter Clausen return 0;
17068fb7407SGrazvydas Ignotas }
17168fb7407SGrazvydas Ignotas
1722261cf1cSBhumika Goyal static const struct snd_soc_ops omap3pandora_ops = {
1734b94dba0SGrazvydas Ignotas .hw_params = omap3pandora_hw_params,
17468fb7407SGrazvydas Ignotas };
17568fb7407SGrazvydas Ignotas
17668fb7407SGrazvydas Ignotas /* Digital audio interface glue - connects codec <--> CPU */
17777723f1cSKuninori Morimoto SND_SOC_DAILINK_DEFS(out,
17877723f1cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.2")),
17977723f1cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CODEC("twl4030-codec", "twl4030-hifi")),
18077723f1cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.2")));
18177723f1cSKuninori Morimoto
18277723f1cSKuninori Morimoto SND_SOC_DAILINK_DEFS(in,
18377723f1cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.4")),
18477723f1cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CODEC("twl4030-codec", "twl4030-hifi")),
18577723f1cSKuninori Morimoto DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.4")));
18677723f1cSKuninori Morimoto
18768fb7407SGrazvydas Ignotas static struct snd_soc_dai_link omap3pandora_dai[] = {
18868fb7407SGrazvydas Ignotas {
18968fb7407SGrazvydas Ignotas .name = "PCM1773",
19068fb7407SGrazvydas Ignotas .stream_name = "HiFi Out",
191cf9feff2SJarkko Nikula .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
192*941abe67SKuninori Morimoto SND_SOC_DAIFMT_CBC_CFC,
1934b94dba0SGrazvydas Ignotas .ops = &omap3pandora_ops,
19468fb7407SGrazvydas Ignotas .init = omap3pandora_out_init,
19577723f1cSKuninori Morimoto SND_SOC_DAILINK_REG(out),
19668fb7407SGrazvydas Ignotas }, {
19768fb7407SGrazvydas Ignotas .name = "TWL4030",
19868fb7407SGrazvydas Ignotas .stream_name = "Line/Mic In",
199cf9feff2SJarkko Nikula .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
200*941abe67SKuninori Morimoto SND_SOC_DAIFMT_CBC_CFC,
2014b94dba0SGrazvydas Ignotas .ops = &omap3pandora_ops,
20268fb7407SGrazvydas Ignotas .init = omap3pandora_in_init,
20377723f1cSKuninori Morimoto SND_SOC_DAILINK_REG(in),
20468fb7407SGrazvydas Ignotas }
20568fb7407SGrazvydas Ignotas };
20668fb7407SGrazvydas Ignotas
20768fb7407SGrazvydas Ignotas /* SoC card */
20868fb7407SGrazvydas Ignotas static struct snd_soc_card snd_soc_card_omap3pandora = {
20968fb7407SGrazvydas Ignotas .name = "omap3pandora",
210b425b884SAxel Lin .owner = THIS_MODULE,
21168fb7407SGrazvydas Ignotas .dai_link = omap3pandora_dai,
21268fb7407SGrazvydas Ignotas .num_links = ARRAY_SIZE(omap3pandora_dai),
213b2e69054SLars-Peter Clausen
214b2e69054SLars-Peter Clausen .dapm_widgets = omap3pandora_dapm_widgets,
215b2e69054SLars-Peter Clausen .num_dapm_widgets = ARRAY_SIZE(omap3pandora_dapm_widgets),
216b2e69054SLars-Peter Clausen .dapm_routes = omap3pandora_map,
217b2e69054SLars-Peter Clausen .num_dapm_routes = ARRAY_SIZE(omap3pandora_map),
21868fb7407SGrazvydas Ignotas };
21968fb7407SGrazvydas Ignotas
22068fb7407SGrazvydas Ignotas static struct platform_device *omap3pandora_snd_device;
22168fb7407SGrazvydas Ignotas
omap3pandora_soc_init(void)22268fb7407SGrazvydas Ignotas static int __init omap3pandora_soc_init(void)
22368fb7407SGrazvydas Ignotas {
22468fb7407SGrazvydas Ignotas int ret;
22568fb7407SGrazvydas Ignotas
2268f008062SGrazvydas Ignotas if (!machine_is_omap3_pandora())
22768fb7407SGrazvydas Ignotas return -ENODEV;
2288f008062SGrazvydas Ignotas
22968fb7407SGrazvydas Ignotas pr_info("OMAP3 Pandora SoC init\n");
23068fb7407SGrazvydas Ignotas
23168fb7407SGrazvydas Ignotas omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
23268fb7407SGrazvydas Ignotas if (omap3pandora_snd_device == NULL) {
23368fb7407SGrazvydas Ignotas pr_err(PREFIX "Platform device allocation failed\n");
234319e6ac1SLinus Walleij return -ENOMEM;
23568fb7407SGrazvydas Ignotas }
23668fb7407SGrazvydas Ignotas
237f0fba2adSLiam Girdwood platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
23868fb7407SGrazvydas Ignotas
23968fb7407SGrazvydas Ignotas ret = platform_device_add(omap3pandora_snd_device);
24068fb7407SGrazvydas Ignotas if (ret) {
24168fb7407SGrazvydas Ignotas pr_err(PREFIX "Unable to add platform device\n");
24268fb7407SGrazvydas Ignotas goto fail2;
24368fb7407SGrazvydas Ignotas }
24468fb7407SGrazvydas Ignotas
245319e6ac1SLinus Walleij dac_power_gpio = devm_gpiod_get(&omap3pandora_snd_device->dev,
246319e6ac1SLinus Walleij "dac", GPIOD_OUT_LOW);
247319e6ac1SLinus Walleij if (IS_ERR(dac_power_gpio)) {
248319e6ac1SLinus Walleij ret = PTR_ERR(dac_power_gpio);
249319e6ac1SLinus Walleij goto fail3;
250319e6ac1SLinus Walleij }
251319e6ac1SLinus Walleij
252319e6ac1SLinus Walleij amp_power_gpio = devm_gpiod_get(&omap3pandora_snd_device->dev,
253319e6ac1SLinus Walleij "amp", GPIOD_OUT_LOW);
254319e6ac1SLinus Walleij if (IS_ERR(amp_power_gpio)) {
255319e6ac1SLinus Walleij ret = PTR_ERR(amp_power_gpio);
256319e6ac1SLinus Walleij goto fail3;
257319e6ac1SLinus Walleij }
258319e6ac1SLinus Walleij
259c50749deSGrazvydas Ignotas omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
260c50749deSGrazvydas Ignotas if (IS_ERR(omap3pandora_dac_reg)) {
261c50749deSGrazvydas Ignotas pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
262c50749deSGrazvydas Ignotas dev_name(&omap3pandora_snd_device->dev),
263c50749deSGrazvydas Ignotas PTR_ERR(omap3pandora_dac_reg));
2645c12d201SAxel Lin ret = PTR_ERR(omap3pandora_dac_reg);
265c50749deSGrazvydas Ignotas goto fail3;
266c50749deSGrazvydas Ignotas }
267c50749deSGrazvydas Ignotas
26868fb7407SGrazvydas Ignotas return 0;
26968fb7407SGrazvydas Ignotas
270c50749deSGrazvydas Ignotas fail3:
271c50749deSGrazvydas Ignotas platform_device_del(omap3pandora_snd_device);
27268fb7407SGrazvydas Ignotas fail2:
27368fb7407SGrazvydas Ignotas platform_device_put(omap3pandora_snd_device);
274319e6ac1SLinus Walleij
27568fb7407SGrazvydas Ignotas return ret;
27668fb7407SGrazvydas Ignotas }
27768fb7407SGrazvydas Ignotas module_init(omap3pandora_soc_init);
27868fb7407SGrazvydas Ignotas
omap3pandora_soc_exit(void)27968fb7407SGrazvydas Ignotas static void __exit omap3pandora_soc_exit(void)
28068fb7407SGrazvydas Ignotas {
281c50749deSGrazvydas Ignotas regulator_put(omap3pandora_dac_reg);
28268fb7407SGrazvydas Ignotas platform_device_unregister(omap3pandora_snd_device);
28368fb7407SGrazvydas Ignotas }
28468fb7407SGrazvydas Ignotas module_exit(omap3pandora_soc_exit);
28568fb7407SGrazvydas Ignotas
28668fb7407SGrazvydas Ignotas MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
28768fb7407SGrazvydas Ignotas MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
28868fb7407SGrazvydas Ignotas MODULE_LICENSE("GPL");
289