1 // SPDX-License-Identifier: GPL-2.0+
3 // rx1950.c - ALSA SoC Audio Layer
5 // Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
7 // Based on smdk2440.c and magician.c
9 // Authors: Graeme Gregory graeme.gregory@wolfsonmicro.com
10 // Philipp Zabel <philipp.zabel@gmail.com>
11 // Denis Grigoriev <dgreenday@gmail.com>
12 // Vasily Khoruzhick <anarsoul@gmail.com>
14 #include <linux/types.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
18 #include <sound/soc.h>
19 #include <sound/jack.h>
22 #include "s3c24xx-i2s.h"
24 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
25 static int rx1950_startup(struct snd_pcm_substream *substream);
26 static int rx1950_hw_params(struct snd_pcm_substream *substream,
27 struct snd_pcm_hw_params *params);
28 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
29 struct snd_kcontrol *kcontrol, int event);
31 static const unsigned int rates[] = {
37 static const struct snd_pcm_hw_constraint_list hw_rates = {
38 .count = ARRAY_SIZE(rates),
42 static struct snd_soc_jack hp_jack;
44 static struct snd_soc_jack_pin hp_jack_pins[] = {
46 .pin = "Headphone Jack",
47 .mask = SND_JACK_HEADPHONE,
51 .mask = SND_JACK_HEADPHONE,
56 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
59 .report = SND_JACK_HEADPHONE,
65 static const struct snd_soc_ops rx1950_ops = {
66 .startup = rx1950_startup,
67 .hw_params = rx1950_hw_params,
70 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
71 SND_SOC_DAILINK_DEFS(uda1380,
72 DAILINK_COMP_ARRAY(COMP_CPU("s3c24xx-iis")),
73 DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-001a",
75 DAILINK_COMP_ARRAY(COMP_PLATFORM("s3c24xx-iis")));
77 static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
80 .stream_name = "UDA1380 Duplex",
81 .init = rx1950_uda1380_init,
82 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
83 SND_SOC_DAIFMT_CBS_CFS,
85 SND_SOC_DAILINK_REG(uda1380),
89 /* rx1950 machine dapm widgets */
90 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
91 SND_SOC_DAPM_HP("Headphone Jack", NULL),
92 SND_SOC_DAPM_MIC("Mic Jack", NULL),
93 SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
96 /* rx1950 machine audio_map */
97 static const struct snd_soc_dapm_route audio_map[] = {
98 /* headphone connected to VOUTLHP, VOUTRHP */
99 {"Headphone Jack", NULL, "VOUTLHP"},
100 {"Headphone Jack", NULL, "VOUTRHP"},
102 /* ext speaker connected to VOUTL, VOUTR */
103 {"Speaker", NULL, "VOUTL"},
104 {"Speaker", NULL, "VOUTR"},
106 /* mic is connected to VINM */
107 {"VINM", NULL, "Mic Jack"},
110 static struct snd_soc_card rx1950_asoc = {
112 .owner = THIS_MODULE,
113 .dai_link = rx1950_uda1380_dai,
114 .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
116 .dapm_widgets = uda1380_dapm_widgets,
117 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
118 .dapm_routes = audio_map,
119 .num_dapm_routes = ARRAY_SIZE(audio_map),
122 static int rx1950_startup(struct snd_pcm_substream *substream)
124 struct snd_pcm_runtime *runtime = substream->runtime;
126 return snd_pcm_hw_constraint_list(runtime, 0,
127 SNDRV_PCM_HW_PARAM_RATE,
131 struct gpio_desc *gpiod_speaker_power;
133 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
134 struct snd_kcontrol *kcontrol, int event)
136 if (SND_SOC_DAPM_EVENT_ON(event))
137 gpiod_set_value(gpiod_speaker_power, 1);
139 gpiod_set_value(gpiod_speaker_power, 0);
144 static int rx1950_hw_params(struct snd_pcm_substream *substream,
145 struct snd_pcm_hw_params *params)
147 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
148 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
151 unsigned int rate = params_rate(params);
152 int clk_source, fs_mode;
157 clk_source = S3C24XX_CLKSRC_PCLK;
158 fs_mode = S3C2410_IISMOD_256FS;
159 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
160 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
165 clk_source = S3C24XX_CLKSRC_MPLL;
166 fs_mode = S3C2410_IISMOD_384FS;
170 printk(KERN_ERR "%s: rate %d is not supported\n",
175 /* select clock source */
176 ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
181 /* set MCLK division for sample rate */
182 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
187 /* set BCLK division for sample rate */
188 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
189 S3C2410_IISMOD_32FS);
193 /* set prescaler division for sample rate */
194 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
195 S3C24XX_PRESCALE(div, div));
202 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
204 snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
206 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
208 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
214 static int rx1950_probe(struct platform_device *pdev)
216 struct device *dev = &pdev->dev;
218 /* configure some gpios */
219 gpiod_speaker_power = devm_gpiod_get(dev, "speaker-power", GPIOD_OUT_LOW);
220 if (IS_ERR(gpiod_speaker_power)) {
221 dev_err(dev, "cannot get gpio\n");
222 return PTR_ERR(gpiod_speaker_power);
225 hp_jack_gpios[0].gpiod_dev = dev;
226 rx1950_asoc.dev = dev;
228 return devm_snd_soc_register_card(dev, &rx1950_asoc);
231 struct platform_driver rx1950_audio = {
233 .name = "rx1950-audio",
234 .pm = &snd_soc_pm_ops,
236 .probe = rx1950_probe,
239 module_platform_driver(rx1950_audio);
241 /* Module information */
242 MODULE_AUTHOR("Vasily Khoruzhick");
243 MODULE_DESCRIPTION("ALSA SoC RX1950");
244 MODULE_LICENSE("GPL");
245 MODULE_ALIAS("platform:rx1950-audio");