1 // SPDX-License-Identifier: GPL-2.0+
3 // h1940_uda1380.c - ALSA SoC Audio Layer
5 // Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
6 // Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
8 // Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
10 #include <linux/types.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
14 #include <sound/soc.h>
15 #include <sound/jack.h>
18 #include "s3c24xx-i2s.h"
20 static const unsigned int rates[] = {
26 static const struct snd_pcm_hw_constraint_list hw_rates = {
27 .count = ARRAY_SIZE(rates),
31 static struct gpio_desc *gpiod_speaker_power;
33 static struct snd_soc_jack hp_jack;
35 static struct snd_soc_jack_pin hp_jack_pins[] = {
37 .pin = "Headphone Jack",
38 .mask = SND_JACK_HEADPHONE,
42 .mask = SND_JACK_HEADPHONE,
47 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
50 .report = SND_JACK_HEADPHONE,
56 static int h1940_startup(struct snd_pcm_substream *substream)
58 struct snd_pcm_runtime *runtime = substream->runtime;
60 return snd_pcm_hw_constraint_list(runtime, 0,
61 SNDRV_PCM_HW_PARAM_RATE,
65 static int h1940_hw_params(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params *params)
68 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
69 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
72 unsigned int rate = params_rate(params);
78 div = s3c24xx_i2s_get_clockrate() / (384 * rate);
79 if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
83 dev_err(rtd->dev, "%s: rate %d is not supported\n",
88 /* select clock source */
89 ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
94 /* set MCLK division for sample rate */
95 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
96 S3C2410_IISMOD_384FS);
100 /* set BCLK division for sample rate */
101 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
102 S3C2410_IISMOD_32FS);
106 /* set prescaler division for sample rate */
107 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
108 S3C24XX_PRESCALE(div, div));
115 static const struct snd_soc_ops h1940_ops = {
116 .startup = h1940_startup,
117 .hw_params = h1940_hw_params,
120 static int h1940_spk_power(struct snd_soc_dapm_widget *w,
121 struct snd_kcontrol *kcontrol, int event)
123 if (SND_SOC_DAPM_EVENT_ON(event))
124 gpiod_set_value(gpiod_speaker_power, 1);
126 gpiod_set_value(gpiod_speaker_power, 0);
131 /* h1940 machine dapm widgets */
132 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
133 SND_SOC_DAPM_HP("Headphone Jack", NULL),
134 SND_SOC_DAPM_MIC("Mic Jack", NULL),
135 SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
138 /* h1940 machine audio_map */
139 static const struct snd_soc_dapm_route audio_map[] = {
140 /* headphone connected to VOUTLHP, VOUTRHP */
141 {"Headphone Jack", NULL, "VOUTLHP"},
142 {"Headphone Jack", NULL, "VOUTRHP"},
144 /* ext speaker connected to VOUTL, VOUTR */
145 {"Speaker", NULL, "VOUTL"},
146 {"Speaker", NULL, "VOUTR"},
148 /* mic is connected to VINM */
149 {"VINM", NULL, "Mic Jack"},
152 static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
154 snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
156 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
158 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
164 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
165 SND_SOC_DAILINK_DEFS(uda1380,
166 DAILINK_COMP_ARRAY(COMP_CPU("s3c24xx-iis")),
167 DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-001a", "uda1380-hifi")),
168 DAILINK_COMP_ARRAY(COMP_PLATFORM("s3c24xx-iis")));
170 static struct snd_soc_dai_link h1940_uda1380_dai[] = {
173 .stream_name = "UDA1380 Duplex",
174 .init = h1940_uda1380_init,
175 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
176 SND_SOC_DAIFMT_CBS_CFS,
178 SND_SOC_DAILINK_REG(uda1380),
182 static struct snd_soc_card h1940_asoc = {
184 .owner = THIS_MODULE,
185 .dai_link = h1940_uda1380_dai,
186 .num_links = ARRAY_SIZE(h1940_uda1380_dai),
188 .dapm_widgets = uda1380_dapm_widgets,
189 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
190 .dapm_routes = audio_map,
191 .num_dapm_routes = ARRAY_SIZE(audio_map),
194 static int h1940_probe(struct platform_device *pdev)
196 struct device *dev = &pdev->dev;
198 h1940_asoc.dev = dev;
199 hp_jack_gpios[0].gpiod_dev = dev;
200 gpiod_speaker_power = devm_gpiod_get(&pdev->dev, "speaker-power",
203 if (IS_ERR(gpiod_speaker_power)) {
204 dev_err(dev, "Could not get gpio\n");
205 return PTR_ERR(gpiod_speaker_power);
208 return devm_snd_soc_register_card(dev, &h1940_asoc);
211 static struct platform_driver h1940_audio_driver = {
213 .name = "h1940-audio",
214 .pm = &snd_soc_pm_ops,
216 .probe = h1940_probe,
218 module_platform_driver(h1940_audio_driver);
220 /* Module information */
221 MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
222 MODULE_DESCRIPTION("ALSA SoC H1940");
223 MODULE_LICENSE("GPL");
224 MODULE_ALIAS("platform:h1940-audio");