Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // rx1950.c - ALSA SoC Audio Layer
0004 //
0005 // Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
0006 //
0007 // Based on smdk2440.c and magician.c
0008 //
0009 // Authors: Graeme Gregory graeme.gregory@wolfsonmicro.com
0010 //          Philipp Zabel <philipp.zabel@gmail.com>
0011 //          Denis Grigoriev <dgreenday@gmail.com>
0012 //          Vasily Khoruzhick <anarsoul@gmail.com>
0013 
0014 #include <linux/types.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/module.h>
0017 
0018 #include <sound/soc.h>
0019 #include <sound/jack.h>
0020 
0021 #include "regs-iis.h"
0022 #include "s3c24xx-i2s.h"
0023 
0024 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
0025 static int rx1950_startup(struct snd_pcm_substream *substream);
0026 static int rx1950_hw_params(struct snd_pcm_substream *substream,
0027                 struct snd_pcm_hw_params *params);
0028 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
0029                 struct snd_kcontrol *kcontrol, int event);
0030 
0031 static const unsigned int rates[] = {
0032     16000,
0033     44100,
0034     48000,
0035 };
0036 
0037 static const struct snd_pcm_hw_constraint_list hw_rates = {
0038     .count = ARRAY_SIZE(rates),
0039     .list = rates,
0040 };
0041 
0042 static struct snd_soc_jack hp_jack;
0043 
0044 static struct snd_soc_jack_pin hp_jack_pins[] = {
0045     {
0046         .pin    = "Headphone Jack",
0047         .mask   = SND_JACK_HEADPHONE,
0048     },
0049     {
0050         .pin    = "Speaker",
0051         .mask   = SND_JACK_HEADPHONE,
0052         .invert = 1,
0053     },
0054 };
0055 
0056 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
0057     [0] = {
0058         .name           = "hp-gpio",
0059         .report         = SND_JACK_HEADPHONE,
0060         .invert         = 1,
0061         .debounce_time      = 200,
0062     },
0063 };
0064 
0065 static const struct snd_soc_ops rx1950_ops = {
0066     .startup    = rx1950_startup,
0067     .hw_params  = rx1950_hw_params,
0068 };
0069 
0070 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
0071 SND_SOC_DAILINK_DEFS(uda1380,
0072     DAILINK_COMP_ARRAY(COMP_CPU("s3c24xx-iis")),
0073     DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-001a",
0074                       "uda1380-hifi")),
0075     DAILINK_COMP_ARRAY(COMP_PLATFORM("s3c24xx-iis")));
0076 
0077 static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
0078     {
0079         .name       = "uda1380",
0080         .stream_name    = "UDA1380 Duplex",
0081         .init       = rx1950_uda1380_init,
0082         .dai_fmt    = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0083                   SND_SOC_DAIFMT_CBS_CFS,
0084         .ops        = &rx1950_ops,
0085         SND_SOC_DAILINK_REG(uda1380),
0086     },
0087 };
0088 
0089 /* rx1950 machine dapm widgets */
0090 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
0091     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0092     SND_SOC_DAPM_MIC("Mic Jack", NULL),
0093     SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
0094 };
0095 
0096 /* rx1950 machine audio_map */
0097 static const struct snd_soc_dapm_route audio_map[] = {
0098     /* headphone connected to VOUTLHP, VOUTRHP */
0099     {"Headphone Jack", NULL, "VOUTLHP"},
0100     {"Headphone Jack", NULL, "VOUTRHP"},
0101 
0102     /* ext speaker connected to VOUTL, VOUTR  */
0103     {"Speaker", NULL, "VOUTL"},
0104     {"Speaker", NULL, "VOUTR"},
0105 
0106     /* mic is connected to VINM */
0107     {"VINM", NULL, "Mic Jack"},
0108 };
0109 
0110 static struct snd_soc_card rx1950_asoc = {
0111     .name = "rx1950",
0112     .owner = THIS_MODULE,
0113     .dai_link = rx1950_uda1380_dai,
0114     .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
0115 
0116     .dapm_widgets = uda1380_dapm_widgets,
0117     .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
0118     .dapm_routes = audio_map,
0119     .num_dapm_routes = ARRAY_SIZE(audio_map),
0120 };
0121 
0122 static int rx1950_startup(struct snd_pcm_substream *substream)
0123 {
0124     struct snd_pcm_runtime *runtime = substream->runtime;
0125 
0126     return snd_pcm_hw_constraint_list(runtime, 0,
0127                     SNDRV_PCM_HW_PARAM_RATE,
0128                     &hw_rates);
0129 }
0130 
0131 static struct gpio_desc *gpiod_speaker_power;
0132 
0133 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
0134                 struct snd_kcontrol *kcontrol, int event)
0135 {
0136     if (SND_SOC_DAPM_EVENT_ON(event))
0137         gpiod_set_value(gpiod_speaker_power, 1);
0138     else
0139         gpiod_set_value(gpiod_speaker_power, 0);
0140 
0141     return 0;
0142 }
0143 
0144 static int rx1950_hw_params(struct snd_pcm_substream *substream,
0145                 struct snd_pcm_hw_params *params)
0146 {
0147     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0148     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0149     int div;
0150     int ret;
0151     unsigned int rate = params_rate(params);
0152     int clk_source, fs_mode;
0153 
0154     switch (rate) {
0155     case 16000:
0156     case 48000:
0157         clk_source = S3C24XX_CLKSRC_PCLK;
0158         fs_mode = S3C2410_IISMOD_256FS;
0159         div = s3c24xx_i2s_get_clockrate() / (256 * rate);
0160         if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
0161             div++;
0162         break;
0163     case 44100:
0164     case 88200:
0165         clk_source = S3C24XX_CLKSRC_MPLL;
0166         fs_mode = S3C2410_IISMOD_384FS;
0167         div = 1;
0168         break;
0169     default:
0170         printk(KERN_ERR "%s: rate %d is not supported\n",
0171             __func__, rate);
0172         return -EINVAL;
0173     }
0174 
0175     /* select clock source */
0176     ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
0177             SND_SOC_CLOCK_OUT);
0178     if (ret < 0)
0179         return ret;
0180 
0181     /* set MCLK division for sample rate */
0182     ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
0183         fs_mode);
0184     if (ret < 0)
0185         return ret;
0186 
0187     /* set BCLK division for sample rate */
0188     ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
0189         S3C2410_IISMOD_32FS);
0190     if (ret < 0)
0191         return ret;
0192 
0193     /* set prescaler division for sample rate */
0194     ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
0195         S3C24XX_PRESCALE(div, div));
0196     if (ret < 0)
0197         return ret;
0198 
0199     return 0;
0200 }
0201 
0202 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
0203 {
0204     snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
0205         SND_JACK_HEADPHONE,
0206         &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
0207 
0208     snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
0209         hp_jack_gpios);
0210 
0211     return 0;
0212 }
0213 
0214 static int rx1950_probe(struct platform_device *pdev)
0215 {
0216     struct device *dev = &pdev->dev;
0217 
0218     /* configure some gpios */
0219     gpiod_speaker_power = devm_gpiod_get(dev, "speaker-power", GPIOD_OUT_LOW);
0220     if (IS_ERR(gpiod_speaker_power)) {
0221         dev_err(dev, "cannot get gpio\n");
0222         return PTR_ERR(gpiod_speaker_power);
0223     }
0224 
0225     hp_jack_gpios[0].gpiod_dev = dev;
0226     rx1950_asoc.dev = dev;
0227 
0228     return devm_snd_soc_register_card(dev, &rx1950_asoc);
0229 }
0230 
0231 static struct platform_driver rx1950_audio = {
0232     .driver = {
0233         .name = "rx1950-audio",
0234         .pm = &snd_soc_pm_ops,
0235     },
0236     .probe = rx1950_probe,
0237 };
0238 
0239 module_platform_driver(rx1950_audio);
0240 
0241 /* Module information */
0242 MODULE_AUTHOR("Vasily Khoruzhick");
0243 MODULE_DESCRIPTION("ALSA SoC RX1950");
0244 MODULE_LICENSE("GPL");
0245 MODULE_ALIAS("platform:rx1950-audio");