blob: a064ca7d78c352e267ec11bd2e7cfcef0200f8c5 [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);
34static int rx1950_startup(struct snd_pcm_substream *substream);
35static int rx1950_hw_params(struct snd_pcm_substream *substream,
36 struct snd_pcm_hw_params *params);
37static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
38 struct snd_kcontrol *kcontrol, int event);
39
Lars-Peter Clausen7a5b8ba2015-10-22 10:43:22 +020040static const unsigned int rates[] = {
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030041 16000,
42 44100,
43 48000,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030044};
45
Lars-Peter Clausen7a5b8ba2015-10-22 10:43:22 +020046static const struct snd_pcm_hw_constraint_list hw_rates = {
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030047 .count = ARRAY_SIZE(rates),
48 .list = rates,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030049};
50
51static struct snd_soc_jack hp_jack;
52
53static struct snd_soc_jack_pin hp_jack_pins[] = {
54 {
55 .pin = "Headphone Jack",
56 .mask = SND_JACK_HEADPHONE,
57 },
58 {
59 .pin = "Speaker",
60 .mask = SND_JACK_HEADPHONE,
61 .invert = 1,
62 },
63};
64
65static struct snd_soc_jack_gpio hp_jack_gpios[] = {
66 [0] = {
67 .gpio = S3C2410_GPG(12),
68 .name = "hp-gpio",
69 .report = SND_JACK_HEADPHONE,
70 .invert = 1,
71 .debounce_time = 200,
72 },
73};
74
75static struct snd_soc_ops rx1950_ops = {
76 .startup = rx1950_startup,
77 .hw_params = rx1950_hw_params,
78};
79
80/* s3c24xx digital audio interface glue - connects codec <--> CPU */
81static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
82 {
83 .name = "uda1380",
84 .stream_name = "UDA1380 Duplex",
85 .cpu_dai_name = "s3c24xx-iis",
86 .codec_dai_name = "uda1380-hifi",
87 .init = rx1950_uda1380_init,
Padmavathi Vennaa08485d82012-12-07 13:59:21 +053088 .platform_name = "s3c24xx-iis",
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030089 .codec_name = "uda1380-codec.0-001a",
Lars-Peter Clausen5f0aced2015-01-01 17:16:23 +010090 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
91 SND_SOC_DAIFMT_CBS_CFS,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030092 .ops = &rx1950_ops,
93 },
94};
95
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030096/* rx1950 machine dapm widgets */
97static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
98 SND_SOC_DAPM_HP("Headphone Jack", NULL),
99 SND_SOC_DAPM_MIC("Mic Jack", NULL),
100 SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
101};
102
103/* rx1950 machine audio_map */
104static const struct snd_soc_dapm_route audio_map[] = {
105 /* headphone connected to VOUTLHP, VOUTRHP */
106 {"Headphone Jack", NULL, "VOUTLHP"},
107 {"Headphone Jack", NULL, "VOUTRHP"},
108
109 /* ext speaker connected to VOUTL, VOUTR */
110 {"Speaker", NULL, "VOUTL"},
111 {"Speaker", NULL, "VOUTR"},
112
113 /* mic is connected to VINM */
114 {"VINM", NULL, "Mic Jack"},
115};
116
Mark Brown8ae23222011-10-08 13:30:35 +0100117static struct snd_soc_card rx1950_asoc = {
118 .name = "rx1950",
Axel Lin095d79d2011-12-22 10:53:15 +0800119 .owner = THIS_MODULE,
Mark Brown8ae23222011-10-08 13:30:35 +0100120 .dai_link = rx1950_uda1380_dai,
121 .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
122
123 .dapm_widgets = uda1380_dapm_widgets,
124 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
125 .dapm_routes = audio_map,
126 .num_dapm_routes = ARRAY_SIZE(audio_map),
127};
128
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300129static struct platform_device *s3c24xx_snd_device;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300130
131static int rx1950_startup(struct snd_pcm_substream *substream)
132{
133 struct snd_pcm_runtime *runtime = substream->runtime;
134
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300135 return snd_pcm_hw_constraint_list(runtime, 0,
136 SNDRV_PCM_HW_PARAM_RATE,
137 &hw_rates);
138}
139
140static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
141 struct snd_kcontrol *kcontrol, int event)
142{
143 if (SND_SOC_DAPM_EVENT_ON(event))
144 gpio_set_value(S3C2410_GPA(1), 1);
145 else
146 gpio_set_value(S3C2410_GPA(1), 0);
147
148 return 0;
149}
150
151static int rx1950_hw_params(struct snd_pcm_substream *substream,
152 struct snd_pcm_hw_params *params)
153{
154 struct snd_soc_pcm_runtime *rtd = substream->private_data;
155 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300156 int div;
157 int ret;
158 unsigned int rate = params_rate(params);
159 int clk_source, fs_mode;
160
161 switch (rate) {
162 case 16000:
163 case 48000:
164 clk_source = S3C24XX_CLKSRC_PCLK;
Vasily Khoruzhick1fdc7dd2010-09-07 17:04:18 +0300165 fs_mode = S3C2410_IISMOD_256FS;
166 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
167 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300168 div++;
169 break;
170 case 44100:
171 case 88200:
172 clk_source = S3C24XX_CLKSRC_MPLL;
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200173 fs_mode = S3C2410_IISMOD_384FS;
174 div = 1;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300175 break;
176 default:
177 printk(KERN_ERR "%s: rate %d is not supported\n",
178 __func__, rate);
179 return -EINVAL;
180 }
181
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300182 /* select clock source */
183 ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
184 SND_SOC_CLOCK_OUT);
185 if (ret < 0)
186 return ret;
187
188 /* set MCLK division for sample rate */
189 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200190 fs_mode);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300191 if (ret < 0)
192 return ret;
193
194 /* set BCLK division for sample rate */
195 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
196 S3C2410_IISMOD_32FS);
197 if (ret < 0)
198 return ret;
199
200 /* set prescaler division for sample rate */
201 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
202 S3C24XX_PRESCALE(div, div));
203 if (ret < 0)
204 return ret;
205
206 return 0;
207}
208
209static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
210{
Lars-Peter Clausene9c9a722015-03-04 10:33:36 +0100211 snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
212 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300213
214 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
215 hp_jack_gpios);
216
217 return 0;
218}
219
220static int __init rx1950_init(void)
221{
222 int ret;
223
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +0300224 if (!machine_is_rx1950())
225 return -ENODEV;
226
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300227 /* configure some gpios */
228 ret = gpio_request(S3C2410_GPA(1), "speaker-power");
229 if (ret)
230 goto err_gpio;
231
232 ret = gpio_direction_output(S3C2410_GPA(1), 0);
233 if (ret)
234 goto err_gpio_conf;
235
236 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
237 if (!s3c24xx_snd_device) {
238 ret = -ENOMEM;
239 goto err_plat_alloc;
240 }
241
242 platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
243 ret = platform_device_add(s3c24xx_snd_device);
244
245 if (ret) {
246 platform_device_put(s3c24xx_snd_device);
247 goto err_plat_add;
248 }
249
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300250 return 0;
251
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300252err_plat_add:
253err_plat_alloc:
254err_gpio_conf:
255 gpio_free(S3C2410_GPA(1));
256
257err_gpio:
258 return ret;
259}
260
261static void __exit rx1950_exit(void)
262{
263 platform_device_unregister(s3c24xx_snd_device);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300264 gpio_free(S3C2410_GPA(1));
265}
266
267module_init(rx1950_init);
268module_exit(rx1950_exit);
269
270/* Module information */
271MODULE_AUTHOR("Vasily Khoruzhick");
272MODULE_DESCRIPTION("ALSA SoC RX1950");
273MODULE_LICENSE("GPL");