blob: 35e37c457f1fd311ee982169010df6507939924d [file] [log] [blame]
Vasily Khoruzhick81d97802010-08-30 11:28:08 +03001/*
2 * rx1950.c -- ALSA Soc Audio Layer
3 *
4 * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
5 *
6 * Based on smdk2440.c and magician.c
7 *
8 * Authors: Graeme Gregory graeme.gregory@wolfsonmicro.com
9 * Philipp Zabel <philipp.zabel@gmail.com>
10 * Denis Grigoriev <dgreenday@gmail.com>
11 * Vasily Khoruzhick <anarsoul@gmail.com>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 */
19
Vasily Khoruzhickb8487922011-08-12 17:53:00 +030020#include <linux/types.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030021#include <linux/gpio.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040022#include <linux/module.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030023
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030024#include <sound/soc.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030025#include <sound/jack.h>
26
Sachin Kamatabffae62014-01-22 17:30:38 +053027#include <mach/gpio-samsung.h>
Arnd Bergmann5d229ce52013-04-11 19:08:42 +020028#include "regs-iis.h"
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +030029#include <asm/mach-types.h>
30
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030031#include "s3c24xx-i2s.h"
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030032
33static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
Takashi Iwai16088cb2014-06-03 12:31:46 +020034static int rx1950_uda1380_card_remove(struct snd_soc_card *card);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030035static int rx1950_startup(struct snd_pcm_substream *substream);
36static int rx1950_hw_params(struct snd_pcm_substream *substream,
37 struct snd_pcm_hw_params *params);
38static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
39 struct snd_kcontrol *kcontrol, int event);
40
41static unsigned int rates[] = {
42 16000,
43 44100,
44 48000,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030045};
46
47static struct snd_pcm_hw_constraint_list hw_rates = {
48 .count = ARRAY_SIZE(rates),
49 .list = rates,
50 .mask = 0,
51};
52
53static struct snd_soc_jack hp_jack;
54
55static struct snd_soc_jack_pin hp_jack_pins[] = {
56 {
57 .pin = "Headphone Jack",
58 .mask = SND_JACK_HEADPHONE,
59 },
60 {
61 .pin = "Speaker",
62 .mask = SND_JACK_HEADPHONE,
63 .invert = 1,
64 },
65};
66
67static struct snd_soc_jack_gpio hp_jack_gpios[] = {
68 [0] = {
69 .gpio = S3C2410_GPG(12),
70 .name = "hp-gpio",
71 .report = SND_JACK_HEADPHONE,
72 .invert = 1,
73 .debounce_time = 200,
74 },
75};
76
77static struct snd_soc_ops rx1950_ops = {
78 .startup = rx1950_startup,
79 .hw_params = rx1950_hw_params,
80};
81
82/* s3c24xx digital audio interface glue - connects codec <--> CPU */
83static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
84 {
85 .name = "uda1380",
86 .stream_name = "UDA1380 Duplex",
87 .cpu_dai_name = "s3c24xx-iis",
88 .codec_dai_name = "uda1380-hifi",
89 .init = rx1950_uda1380_init,
Padmavathi Vennaa08485d2012-12-07 13:59:21 +053090 .platform_name = "s3c24xx-iis",
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030091 .codec_name = "uda1380-codec.0-001a",
Lars-Peter Clausen5f0aced2015-01-01 17:16:23 +010092 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
93 SND_SOC_DAIFMT_CBS_CFS,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030094 .ops = &rx1950_ops,
95 },
96};
97
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030098/* rx1950 machine dapm widgets */
99static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
100 SND_SOC_DAPM_HP("Headphone Jack", NULL),
101 SND_SOC_DAPM_MIC("Mic Jack", NULL),
102 SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
103};
104
105/* rx1950 machine audio_map */
106static const struct snd_soc_dapm_route audio_map[] = {
107 /* headphone connected to VOUTLHP, VOUTRHP */
108 {"Headphone Jack", NULL, "VOUTLHP"},
109 {"Headphone Jack", NULL, "VOUTRHP"},
110
111 /* ext speaker connected to VOUTL, VOUTR */
112 {"Speaker", NULL, "VOUTL"},
113 {"Speaker", NULL, "VOUTR"},
114
115 /* mic is connected to VINM */
116 {"VINM", NULL, "Mic Jack"},
117};
118
Mark Brown8ae23222011-10-08 13:30:35 +0100119static struct snd_soc_card rx1950_asoc = {
120 .name = "rx1950",
Axel Lin095d79d2011-12-22 10:53:15 +0800121 .owner = THIS_MODULE,
Stephen Warrene1d4d3c2014-05-30 12:42:57 -0600122 .remove = rx1950_uda1380_card_remove,
Mark Brown8ae23222011-10-08 13:30:35 +0100123 .dai_link = rx1950_uda1380_dai,
124 .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
125
126 .dapm_widgets = uda1380_dapm_widgets,
127 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
128 .dapm_routes = audio_map,
129 .num_dapm_routes = ARRAY_SIZE(audio_map),
130};
131
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300132static struct platform_device *s3c24xx_snd_device;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300133
134static int rx1950_startup(struct snd_pcm_substream *substream)
135{
136 struct snd_pcm_runtime *runtime = substream->runtime;
137
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300138 return snd_pcm_hw_constraint_list(runtime, 0,
139 SNDRV_PCM_HW_PARAM_RATE,
140 &hw_rates);
141}
142
143static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
144 struct snd_kcontrol *kcontrol, int event)
145{
146 if (SND_SOC_DAPM_EVENT_ON(event))
147 gpio_set_value(S3C2410_GPA(1), 1);
148 else
149 gpio_set_value(S3C2410_GPA(1), 0);
150
151 return 0;
152}
153
154static int rx1950_hw_params(struct snd_pcm_substream *substream,
155 struct snd_pcm_hw_params *params)
156{
157 struct snd_soc_pcm_runtime *rtd = substream->private_data;
158 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300159 int div;
160 int ret;
161 unsigned int rate = params_rate(params);
162 int clk_source, fs_mode;
163
164 switch (rate) {
165 case 16000:
166 case 48000:
167 clk_source = S3C24XX_CLKSRC_PCLK;
Vasily Khoruzhick1fdc7dd2010-09-07 17:04:18 +0300168 fs_mode = S3C2410_IISMOD_256FS;
169 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
170 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300171 div++;
172 break;
173 case 44100:
174 case 88200:
175 clk_source = S3C24XX_CLKSRC_MPLL;
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200176 fs_mode = S3C2410_IISMOD_384FS;
177 div = 1;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300178 break;
179 default:
180 printk(KERN_ERR "%s: rate %d is not supported\n",
181 __func__, rate);
182 return -EINVAL;
183 }
184
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300185 /* select clock source */
186 ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
187 SND_SOC_CLOCK_OUT);
188 if (ret < 0)
189 return ret;
190
191 /* set MCLK division for sample rate */
192 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200193 fs_mode);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300194 if (ret < 0)
195 return ret;
196
197 /* set BCLK division for sample rate */
198 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
199 S3C2410_IISMOD_32FS);
200 if (ret < 0)
201 return ret;
202
203 /* set prescaler division for sample rate */
204 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
205 S3C24XX_PRESCALE(div, div));
206 if (ret < 0)
207 return ret;
208
209 return 0;
210}
211
212static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
213{
Lars-Peter Clausene9c9a722015-03-04 10:33:36 +0100214 snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
215 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300216
217 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
218 hp_jack_gpios);
219
220 return 0;
221}
222
Takashi Iwai16088cb2014-06-03 12:31:46 +0200223static int rx1950_uda1380_card_remove(struct snd_soc_card *card)
Stephen Warrene1d4d3c2014-05-30 12:42:57 -0600224{
225 snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
226 hp_jack_gpios);
227
228 return 0;
229}
230
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300231static int __init rx1950_init(void)
232{
233 int ret;
234
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +0300235 if (!machine_is_rx1950())
236 return -ENODEV;
237
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300238 /* configure some gpios */
239 ret = gpio_request(S3C2410_GPA(1), "speaker-power");
240 if (ret)
241 goto err_gpio;
242
243 ret = gpio_direction_output(S3C2410_GPA(1), 0);
244 if (ret)
245 goto err_gpio_conf;
246
247 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
248 if (!s3c24xx_snd_device) {
249 ret = -ENOMEM;
250 goto err_plat_alloc;
251 }
252
253 platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
254 ret = platform_device_add(s3c24xx_snd_device);
255
256 if (ret) {
257 platform_device_put(s3c24xx_snd_device);
258 goto err_plat_add;
259 }
260
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300261 return 0;
262
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300263err_plat_add:
264err_plat_alloc:
265err_gpio_conf:
266 gpio_free(S3C2410_GPA(1));
267
268err_gpio:
269 return ret;
270}
271
272static void __exit rx1950_exit(void)
273{
274 platform_device_unregister(s3c24xx_snd_device);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300275 gpio_free(S3C2410_GPA(1));
276}
277
278module_init(rx1950_init);
279module_exit(rx1950_exit);
280
281/* Module information */
282MODULE_AUTHOR("Vasily Khoruzhick");
283MODULE_DESCRIPTION("ALSA SoC RX1950");
284MODULE_LICENSE("GPL");