blob: 5f5825faeb2a4331177595eaabcd0600b9d2476e [file] [log] [blame]
Vasily Khoruzhick19576682010-12-09 21:17:56 +02001/*
2 * h1940-uda1380.c -- ALSA Soc Audio Layer
3 *
4 * Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
5 * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
6 *
7 * Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
Vasily Khoruzhickfd049752011-08-12 17:52:59 +030016#include <linux/types.h>
Vasily Khoruzhick19576682010-12-09 21:17:56 +020017#include <linux/gpio.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040018#include <linux/module.h>
Vasily Khoruzhick19576682010-12-09 21:17:56 +020019
20#include <sound/soc.h>
Vasily Khoruzhick19576682010-12-09 21:17:56 +020021#include <sound/jack.h>
22
Arnd Bergmann5d229ce52013-04-11 19:08:42 +020023#include "regs-iis.h"
Vasily Khoruzhick19576682010-12-09 21:17:56 +020024#include <asm/mach-types.h>
25
Sachin Kamatabffae62014-01-22 17:30:38 +053026#include <mach/gpio-samsung.h>
Vasily Khoruzhick19576682010-12-09 21:17:56 +020027#include "s3c24xx-i2s.h"
Vasily Khoruzhick19576682010-12-09 21:17:56 +020028
Lars-Peter Clausende687fb2015-10-22 10:43:21 +020029static const unsigned int rates[] = {
Vasily Khoruzhick19576682010-12-09 21:17:56 +020030 11025,
31 22050,
32 44100,
33};
34
Lars-Peter Clausende687fb2015-10-22 10:43:21 +020035static const struct snd_pcm_hw_constraint_list hw_rates = {
Vasily Khoruzhick19576682010-12-09 21:17:56 +020036 .count = ARRAY_SIZE(rates),
37 .list = rates,
Vasily Khoruzhick19576682010-12-09 21:17:56 +020038};
39
40static struct snd_soc_jack hp_jack;
41
42static struct snd_soc_jack_pin hp_jack_pins[] = {
43 {
44 .pin = "Headphone Jack",
45 .mask = SND_JACK_HEADPHONE,
46 },
47 {
48 .pin = "Speaker",
49 .mask = SND_JACK_HEADPHONE,
50 .invert = 1,
51 },
52};
53
54static struct snd_soc_jack_gpio hp_jack_gpios[] = {
55 {
56 .gpio = S3C2410_GPG(4),
57 .name = "hp-gpio",
58 .report = SND_JACK_HEADPHONE,
59 .invert = 1,
60 .debounce_time = 200,
61 },
62};
63
64static int h1940_startup(struct snd_pcm_substream *substream)
65{
66 struct snd_pcm_runtime *runtime = substream->runtime;
67
Vasily Khoruzhick19576682010-12-09 21:17:56 +020068 return snd_pcm_hw_constraint_list(runtime, 0,
69 SNDRV_PCM_HW_PARAM_RATE,
70 &hw_rates);
71}
72
73static int h1940_hw_params(struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params)
75{
76 struct snd_soc_pcm_runtime *rtd = substream->private_data;
77 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Vasily Khoruzhick19576682010-12-09 21:17:56 +020078 int div;
79 int ret;
80 unsigned int rate = params_rate(params);
81
82 switch (rate) {
83 case 11025:
84 case 22050:
85 case 44100:
86 div = s3c24xx_i2s_get_clockrate() / (384 * rate);
87 if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
88 div++;
89 break;
90 default:
Sachin Kamat2046d452014-01-22 17:30:41 +053091 dev_err(rtd->dev, "%s: rate %d is not supported\n",
Vasily Khoruzhick19576682010-12-09 21:17:56 +020092 __func__, rate);
93 return -EINVAL;
94 }
95
Vasily Khoruzhick19576682010-12-09 21:17:56 +020096 /* select clock source */
97 ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
98 SND_SOC_CLOCK_OUT);
99 if (ret < 0)
100 return ret;
101
102 /* set MCLK division for sample rate */
103 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
104 S3C2410_IISMOD_384FS);
105 if (ret < 0)
106 return ret;
107
108 /* set BCLK division for sample rate */
109 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
110 S3C2410_IISMOD_32FS);
111 if (ret < 0)
112 return ret;
113
114 /* set prescaler division for sample rate */
115 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
116 S3C24XX_PRESCALE(div, div));
117 if (ret < 0)
118 return ret;
119
120 return 0;
121}
122
123static struct snd_soc_ops h1940_ops = {
124 .startup = h1940_startup,
125 .hw_params = h1940_hw_params,
126};
127
128static int h1940_spk_power(struct snd_soc_dapm_widget *w,
129 struct snd_kcontrol *kcontrol, int event)
130{
131 if (SND_SOC_DAPM_EVENT_ON(event))
Kukjin Kim232910d2013-01-02 10:18:58 -0800132 gpio_set_value(S3C_GPIO_END + 9, 1);
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200133 else
Kukjin Kim232910d2013-01-02 10:18:58 -0800134 gpio_set_value(S3C_GPIO_END + 9, 0);
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200135
136 return 0;
137}
138
139/* h1940 machine dapm widgets */
140static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
141 SND_SOC_DAPM_HP("Headphone Jack", NULL),
142 SND_SOC_DAPM_MIC("Mic Jack", NULL),
143 SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
144};
145
146/* h1940 machine audio_map */
147static const struct snd_soc_dapm_route audio_map[] = {
148 /* headphone connected to VOUTLHP, VOUTRHP */
149 {"Headphone Jack", NULL, "VOUTLHP"},
150 {"Headphone Jack", NULL, "VOUTRHP"},
151
152 /* ext speaker connected to VOUTL, VOUTR */
153 {"Speaker", NULL, "VOUTL"},
154 {"Speaker", NULL, "VOUTR"},
155
156 /* mic is connected to VINM */
157 {"VINM", NULL, "Mic Jack"},
158};
159
160static struct platform_device *s3c24xx_snd_device;
161
162static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
163{
Lars-Peter Clausendfe11f22015-03-04 10:33:33 +0100164 snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
165 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200166
167 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
168 hp_jack_gpios);
169
170 return 0;
171}
172
Takashi Iwai16088cb2014-06-03 12:31:46 +0200173static int h1940_uda1380_card_remove(struct snd_soc_card *card)
Stephen Warrene1d4d3c2014-05-30 12:42:57 -0600174{
175 snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
176 hp_jack_gpios);
177
178 return 0;
179}
180
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200181/* s3c24xx digital audio interface glue - connects codec <--> CPU */
182static struct snd_soc_dai_link h1940_uda1380_dai[] = {
183 {
184 .name = "uda1380",
185 .stream_name = "UDA1380 Duplex",
186 .cpu_dai_name = "s3c24xx-iis",
187 .codec_dai_name = "uda1380-hifi",
188 .init = h1940_uda1380_init,
Padmavathi Vennaa08485d82012-12-07 13:59:21 +0530189 .platform_name = "s3c24xx-iis",
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200190 .codec_name = "uda1380-codec.0-001a",
Lars-Peter Clausen5cc10b92015-01-01 17:16:20 +0100191 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
192 SND_SOC_DAIFMT_CBS_CFS,
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200193 .ops = &h1940_ops,
194 },
195};
196
197static struct snd_soc_card h1940_asoc = {
198 .name = "h1940",
Axel Lin095d79d2011-12-22 10:53:15 +0800199 .owner = THIS_MODULE,
Stephen Warrene1d4d3c2014-05-30 12:42:57 -0600200 .remove = h1940_uda1380_card_remove,
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200201 .dai_link = h1940_uda1380_dai,
202 .num_links = ARRAY_SIZE(h1940_uda1380_dai),
Mark Brown6119d012011-10-08 13:30:06 +0100203
204 .dapm_widgets = uda1380_dapm_widgets,
205 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
206 .dapm_routes = audio_map,
207 .num_dapm_routes = ARRAY_SIZE(audio_map),
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200208};
209
210static int __init h1940_init(void)
211{
212 int ret;
213
214 if (!machine_is_h1940())
215 return -ENODEV;
216
217 /* configure some gpios */
Kukjin Kim232910d2013-01-02 10:18:58 -0800218 ret = gpio_request(S3C_GPIO_END + 9, "speaker-power");
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200219 if (ret)
220 goto err_out;
221
Kukjin Kim232910d2013-01-02 10:18:58 -0800222 ret = gpio_direction_output(S3C_GPIO_END + 9, 0);
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200223 if (ret)
224 goto err_gpio;
225
226 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
227 if (!s3c24xx_snd_device) {
228 ret = -ENOMEM;
229 goto err_gpio;
230 }
231
232 platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);
233 ret = platform_device_add(s3c24xx_snd_device);
234
235 if (ret)
236 goto err_plat;
237
238 return 0;
239
240err_plat:
241 platform_device_put(s3c24xx_snd_device);
242err_gpio:
Kukjin Kim232910d2013-01-02 10:18:58 -0800243 gpio_free(S3C_GPIO_END + 9);
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200244
245err_out:
246 return ret;
247}
248
249static void __exit h1940_exit(void)
250{
251 platform_device_unregister(s3c24xx_snd_device);
Kukjin Kim232910d2013-01-02 10:18:58 -0800252 gpio_free(S3C_GPIO_END + 9);
Vasily Khoruzhick19576682010-12-09 21:17:56 +0200253}
254
255module_init(h1940_init);
256module_exit(h1940_exit);
257
258/* Module information */
259MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
260MODULE_DESCRIPTION("ALSA SoC H1940");
261MODULE_LICENSE("GPL");