Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * poodle.c  --  SoC audio for Poodle
0004  *
0005  * Copyright 2005 Wolfson Microelectronics PLC.
0006  * Copyright 2005 Openedhand Ltd.
0007  *
0008  * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
0009  *          Richard Purdie <richard@openedhand.com>
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/moduleparam.h>
0014 #include <linux/timer.h>
0015 #include <linux/i2c.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/platform_device.h>
0018 #include <sound/core.h>
0019 #include <sound/pcm.h>
0020 #include <sound/soc.h>
0021 
0022 #include <asm/mach-types.h>
0023 #include <asm/hardware/locomo.h>
0024 #include <linux/platform_data/asoc-pxa.h>
0025 #include <linux/platform_data/asoc-poodle.h>
0026 
0027 #include "../codecs/wm8731.h"
0028 #include "pxa2xx-i2s.h"
0029 
0030 #define POODLE_HP        1
0031 #define POODLE_HP_OFF    0
0032 #define POODLE_SPK_ON    1
0033 #define POODLE_SPK_OFF   0
0034 
0035  /* audio clock in Hz - rounded from 12.235MHz */
0036 #define POODLE_AUDIO_CLOCK 12288000
0037 
0038 static int poodle_jack_func;
0039 static int poodle_spk_func;
0040 
0041 static struct poodle_audio_platform_data *poodle_pdata;
0042 
0043 static void poodle_ext_control(struct snd_soc_dapm_context *dapm)
0044 {
0045     /* set up jack connection */
0046     if (poodle_jack_func == POODLE_HP) {
0047         /* set = unmute headphone */
0048         locomo_gpio_write(poodle_pdata->locomo_dev,
0049             poodle_pdata->gpio_mute_l, 1);
0050         locomo_gpio_write(poodle_pdata->locomo_dev,
0051             poodle_pdata->gpio_mute_r, 1);
0052         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
0053     } else {
0054         locomo_gpio_write(poodle_pdata->locomo_dev,
0055             poodle_pdata->gpio_mute_l, 0);
0056         locomo_gpio_write(poodle_pdata->locomo_dev,
0057             poodle_pdata->gpio_mute_r, 0);
0058         snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
0059     }
0060 
0061     /* set the endpoints to their new connection states */
0062     if (poodle_spk_func == POODLE_SPK_ON)
0063         snd_soc_dapm_enable_pin(dapm, "Ext Spk");
0064     else
0065         snd_soc_dapm_disable_pin(dapm, "Ext Spk");
0066 
0067     /* signal a DAPM event */
0068     snd_soc_dapm_sync(dapm);
0069 }
0070 
0071 static int poodle_startup(struct snd_pcm_substream *substream)
0072 {
0073     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0074 
0075     /* check the jack status at stream startup */
0076     poodle_ext_control(&rtd->card->dapm);
0077 
0078     return 0;
0079 }
0080 
0081 /* we need to unmute the HP at shutdown as the mute burns power on poodle */
0082 static void poodle_shutdown(struct snd_pcm_substream *substream)
0083 {
0084     /* set = unmute headphone */
0085     locomo_gpio_write(poodle_pdata->locomo_dev,
0086         poodle_pdata->gpio_mute_l, 1);
0087     locomo_gpio_write(poodle_pdata->locomo_dev,
0088         poodle_pdata->gpio_mute_r, 1);
0089 }
0090 
0091 static int poodle_hw_params(struct snd_pcm_substream *substream,
0092     struct snd_pcm_hw_params *params)
0093 {
0094     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0095     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0096     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0097     unsigned int clk = 0;
0098     int ret = 0;
0099 
0100     switch (params_rate(params)) {
0101     case 8000:
0102     case 16000:
0103     case 48000:
0104     case 96000:
0105         clk = 12288000;
0106         break;
0107     case 11025:
0108     case 22050:
0109     case 44100:
0110         clk = 11289600;
0111         break;
0112     }
0113 
0114     /* set the codec system clock for DAC and ADC */
0115     ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
0116         SND_SOC_CLOCK_IN);
0117     if (ret < 0)
0118         return ret;
0119 
0120     /* set the I2S system clock as input (unused) */
0121     ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
0122         SND_SOC_CLOCK_IN);
0123     if (ret < 0)
0124         return ret;
0125 
0126     return 0;
0127 }
0128 
0129 static const struct snd_soc_ops poodle_ops = {
0130     .startup = poodle_startup,
0131     .hw_params = poodle_hw_params,
0132     .shutdown = poodle_shutdown,
0133 };
0134 
0135 static int poodle_get_jack(struct snd_kcontrol *kcontrol,
0136     struct snd_ctl_elem_value *ucontrol)
0137 {
0138     ucontrol->value.enumerated.item[0] = poodle_jack_func;
0139     return 0;
0140 }
0141 
0142 static int poodle_set_jack(struct snd_kcontrol *kcontrol,
0143     struct snd_ctl_elem_value *ucontrol)
0144 {
0145     struct snd_soc_card *card =  snd_kcontrol_chip(kcontrol);
0146 
0147     if (poodle_jack_func == ucontrol->value.enumerated.item[0])
0148         return 0;
0149 
0150     poodle_jack_func = ucontrol->value.enumerated.item[0];
0151     poodle_ext_control(&card->dapm);
0152     return 1;
0153 }
0154 
0155 static int poodle_get_spk(struct snd_kcontrol *kcontrol,
0156     struct snd_ctl_elem_value *ucontrol)
0157 {
0158     ucontrol->value.enumerated.item[0] = poodle_spk_func;
0159     return 0;
0160 }
0161 
0162 static int poodle_set_spk(struct snd_kcontrol *kcontrol,
0163     struct snd_ctl_elem_value *ucontrol)
0164 {
0165     struct snd_soc_card *card =  snd_kcontrol_chip(kcontrol);
0166 
0167     if (poodle_spk_func == ucontrol->value.enumerated.item[0])
0168         return 0;
0169 
0170     poodle_spk_func = ucontrol->value.enumerated.item[0];
0171     poodle_ext_control(&card->dapm);
0172     return 1;
0173 }
0174 
0175 static int poodle_amp_event(struct snd_soc_dapm_widget *w,
0176     struct snd_kcontrol *k, int event)
0177 {
0178     if (SND_SOC_DAPM_EVENT_ON(event))
0179         locomo_gpio_write(poodle_pdata->locomo_dev,
0180             poodle_pdata->gpio_amp_on, 0);
0181     else
0182         locomo_gpio_write(poodle_pdata->locomo_dev,
0183             poodle_pdata->gpio_amp_on, 1);
0184 
0185     return 0;
0186 }
0187 
0188 /* poodle machine dapm widgets */
0189 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
0190 SND_SOC_DAPM_HP("Headphone Jack", NULL),
0191 SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
0192 SND_SOC_DAPM_MIC("Microphone", NULL),
0193 };
0194 
0195 /* Corgi machine connections to the codec pins */
0196 static const struct snd_soc_dapm_route poodle_audio_map[] = {
0197 
0198     /* headphone connected to LHPOUT1, RHPOUT1 */
0199     {"Headphone Jack", NULL, "LHPOUT"},
0200     {"Headphone Jack", NULL, "RHPOUT"},
0201 
0202     /* speaker connected to LOUT, ROUT */
0203     {"Ext Spk", NULL, "ROUT"},
0204     {"Ext Spk", NULL, "LOUT"},
0205 
0206     {"MICIN", NULL, "Microphone"},
0207 };
0208 
0209 static const char * const jack_function[] = {"Off", "Headphone"};
0210 static const char * const spk_function[] = {"Off", "On"};
0211 static const struct soc_enum poodle_enum[] = {
0212     SOC_ENUM_SINGLE_EXT(2, jack_function),
0213     SOC_ENUM_SINGLE_EXT(2, spk_function),
0214 };
0215 
0216 static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
0217     SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
0218         poodle_set_jack),
0219     SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
0220         poodle_set_spk),
0221 };
0222 
0223 /* poodle digital audio interface glue - connects codec <--> CPU */
0224 SND_SOC_DAILINK_DEFS(wm8731,
0225     DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-i2s")),
0226     DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.0-001b", "wm8731-hifi")),
0227     DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
0228 
0229 static struct snd_soc_dai_link poodle_dai = {
0230     .name = "WM8731",
0231     .stream_name = "WM8731",
0232     .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0233            SND_SOC_DAIFMT_CBS_CFS,
0234     .ops = &poodle_ops,
0235     SND_SOC_DAILINK_REG(wm8731),
0236 };
0237 
0238 /* poodle audio machine driver */
0239 static struct snd_soc_card poodle = {
0240     .name = "Poodle",
0241     .dai_link = &poodle_dai,
0242     .num_links = 1,
0243     .owner = THIS_MODULE,
0244 
0245     .controls = wm8731_poodle_controls,
0246     .num_controls = ARRAY_SIZE(wm8731_poodle_controls),
0247     .dapm_widgets = wm8731_dapm_widgets,
0248     .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
0249     .dapm_routes = poodle_audio_map,
0250     .num_dapm_routes = ARRAY_SIZE(poodle_audio_map),
0251     .fully_routed = true,
0252 };
0253 
0254 static int poodle_probe(struct platform_device *pdev)
0255 {
0256     struct snd_soc_card *card = &poodle;
0257     int ret;
0258 
0259     poodle_pdata = pdev->dev.platform_data;
0260     locomo_gpio_set_dir(poodle_pdata->locomo_dev,
0261         poodle_pdata->gpio_amp_on, 0);
0262     /* should we mute HP at startup - burning power ?*/
0263     locomo_gpio_set_dir(poodle_pdata->locomo_dev,
0264         poodle_pdata->gpio_mute_l, 0);
0265     locomo_gpio_set_dir(poodle_pdata->locomo_dev,
0266         poodle_pdata->gpio_mute_r, 0);
0267 
0268     card->dev = &pdev->dev;
0269 
0270     ret = devm_snd_soc_register_card(&pdev->dev, card);
0271     if (ret)
0272         dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
0273             ret);
0274     return ret;
0275 }
0276 
0277 static struct platform_driver poodle_driver = {
0278     .driver     = {
0279         .name   = "poodle-audio",
0280         .pm     = &snd_soc_pm_ops,
0281     },
0282     .probe      = poodle_probe,
0283 };
0284 
0285 module_platform_driver(poodle_driver);
0286 
0287 /* Module information */
0288 MODULE_AUTHOR("Richard Purdie");
0289 MODULE_DESCRIPTION("ALSA SoC Poodle");
0290 MODULE_LICENSE("GPL");
0291 MODULE_ALIAS("platform:poodle-audio");