Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 1 | /* |
| 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 Khoruzhick | fd04975 | 2011-08-12 17:52:59 +0300 | [diff] [blame] | 16 | #include <linux/types.h> |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 17 | #include <linux/gpio.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 19 | |
| 20 | #include <sound/soc.h> |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 21 | #include <sound/jack.h> |
| 22 | |
Arnd Bergmann | 5d229ce5 | 2013-04-11 19:08:42 +0200 | [diff] [blame] | 23 | #include "regs-iis.h" |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 24 | #include <asm/mach-types.h> |
| 25 | |
Sachin Kamat | abffae6 | 2014-01-22 17:30:38 +0530 | [diff] [blame] | 26 | #include <mach/gpio-samsung.h> |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 27 | #include "s3c24xx-i2s.h" |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 28 | |
| 29 | static unsigned int rates[] = { |
| 30 | 11025, |
| 31 | 22050, |
| 32 | 44100, |
| 33 | }; |
| 34 | |
| 35 | static struct snd_pcm_hw_constraint_list hw_rates = { |
| 36 | .count = ARRAY_SIZE(rates), |
| 37 | .list = rates, |
| 38 | .mask = 0, |
| 39 | }; |
| 40 | |
| 41 | static struct snd_soc_jack hp_jack; |
| 42 | |
| 43 | static struct snd_soc_jack_pin hp_jack_pins[] = { |
| 44 | { |
| 45 | .pin = "Headphone Jack", |
| 46 | .mask = SND_JACK_HEADPHONE, |
| 47 | }, |
| 48 | { |
| 49 | .pin = "Speaker", |
| 50 | .mask = SND_JACK_HEADPHONE, |
| 51 | .invert = 1, |
| 52 | }, |
| 53 | }; |
| 54 | |
| 55 | static struct snd_soc_jack_gpio hp_jack_gpios[] = { |
| 56 | { |
| 57 | .gpio = S3C2410_GPG(4), |
| 58 | .name = "hp-gpio", |
| 59 | .report = SND_JACK_HEADPHONE, |
| 60 | .invert = 1, |
| 61 | .debounce_time = 200, |
| 62 | }, |
| 63 | }; |
| 64 | |
| 65 | static int h1940_startup(struct snd_pcm_substream *substream) |
| 66 | { |
| 67 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 68 | |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 69 | return snd_pcm_hw_constraint_list(runtime, 0, |
| 70 | SNDRV_PCM_HW_PARAM_RATE, |
| 71 | &hw_rates); |
| 72 | } |
| 73 | |
| 74 | static int h1940_hw_params(struct snd_pcm_substream *substream, |
| 75 | struct snd_pcm_hw_params *params) |
| 76 | { |
| 77 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 78 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 79 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 80 | int div; |
| 81 | int ret; |
| 82 | unsigned int rate = params_rate(params); |
| 83 | |
| 84 | switch (rate) { |
| 85 | case 11025: |
| 86 | case 22050: |
| 87 | case 44100: |
| 88 | div = s3c24xx_i2s_get_clockrate() / (384 * rate); |
| 89 | if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate)) |
| 90 | div++; |
| 91 | break; |
| 92 | default: |
Sachin Kamat | 2046d45 | 2014-01-22 17:30:41 +0530 | [diff] [blame] | 93 | dev_err(rtd->dev, "%s: rate %d is not supported\n", |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 94 | __func__, rate); |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
| 98 | /* set codec DAI configuration */ |
| 99 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S | |
| 100 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
| 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | |
| 104 | /* set cpu DAI configuration */ |
| 105 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | |
| 106 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | |
| 110 | /* select clock source */ |
| 111 | ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate, |
| 112 | SND_SOC_CLOCK_OUT); |
| 113 | if (ret < 0) |
| 114 | return ret; |
| 115 | |
| 116 | /* set MCLK division for sample rate */ |
| 117 | ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK, |
| 118 | S3C2410_IISMOD_384FS); |
| 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | |
| 122 | /* set BCLK division for sample rate */ |
| 123 | ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK, |
| 124 | S3C2410_IISMOD_32FS); |
| 125 | if (ret < 0) |
| 126 | return ret; |
| 127 | |
| 128 | /* set prescaler division for sample rate */ |
| 129 | ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER, |
| 130 | S3C24XX_PRESCALE(div, div)); |
| 131 | if (ret < 0) |
| 132 | return ret; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static struct snd_soc_ops h1940_ops = { |
| 138 | .startup = h1940_startup, |
| 139 | .hw_params = h1940_hw_params, |
| 140 | }; |
| 141 | |
| 142 | static int h1940_spk_power(struct snd_soc_dapm_widget *w, |
| 143 | struct snd_kcontrol *kcontrol, int event) |
| 144 | { |
| 145 | if (SND_SOC_DAPM_EVENT_ON(event)) |
Kukjin Kim | 232910d | 2013-01-02 10:18:58 -0800 | [diff] [blame] | 146 | gpio_set_value(S3C_GPIO_END + 9, 1); |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 147 | else |
Kukjin Kim | 232910d | 2013-01-02 10:18:58 -0800 | [diff] [blame] | 148 | gpio_set_value(S3C_GPIO_END + 9, 0); |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* h1940 machine dapm widgets */ |
| 154 | static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = { |
| 155 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 156 | SND_SOC_DAPM_MIC("Mic Jack", NULL), |
| 157 | SND_SOC_DAPM_SPK("Speaker", h1940_spk_power), |
| 158 | }; |
| 159 | |
| 160 | /* h1940 machine audio_map */ |
| 161 | static const struct snd_soc_dapm_route audio_map[] = { |
| 162 | /* headphone connected to VOUTLHP, VOUTRHP */ |
| 163 | {"Headphone Jack", NULL, "VOUTLHP"}, |
| 164 | {"Headphone Jack", NULL, "VOUTRHP"}, |
| 165 | |
| 166 | /* ext speaker connected to VOUTL, VOUTR */ |
| 167 | {"Speaker", NULL, "VOUTL"}, |
| 168 | {"Speaker", NULL, "VOUTR"}, |
| 169 | |
| 170 | /* mic is connected to VINM */ |
| 171 | {"VINM", NULL, "Mic Jack"}, |
| 172 | }; |
| 173 | |
| 174 | static struct platform_device *s3c24xx_snd_device; |
| 175 | |
| 176 | static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd) |
| 177 | { |
| 178 | struct snd_soc_codec *codec = rtd->codec; |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 179 | |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 180 | snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE, |
| 181 | &hp_jack); |
| 182 | |
| 183 | snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins), |
| 184 | hp_jack_pins); |
| 185 | |
| 186 | snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios), |
| 187 | hp_jack_gpios); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Takashi Iwai | 16088cb | 2014-06-03 12:31:46 +0200 | [diff] [blame] | 192 | static int h1940_uda1380_card_remove(struct snd_soc_card *card) |
Stephen Warren | e1d4d3c | 2014-05-30 12:42:57 -0600 | [diff] [blame] | 193 | { |
| 194 | snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios), |
| 195 | hp_jack_gpios); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 200 | /* s3c24xx digital audio interface glue - connects codec <--> CPU */ |
| 201 | static struct snd_soc_dai_link h1940_uda1380_dai[] = { |
| 202 | { |
| 203 | .name = "uda1380", |
| 204 | .stream_name = "UDA1380 Duplex", |
| 205 | .cpu_dai_name = "s3c24xx-iis", |
| 206 | .codec_dai_name = "uda1380-hifi", |
| 207 | .init = h1940_uda1380_init, |
Padmavathi Venna | a08485d | 2012-12-07 13:59:21 +0530 | [diff] [blame] | 208 | .platform_name = "s3c24xx-iis", |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 209 | .codec_name = "uda1380-codec.0-001a", |
| 210 | .ops = &h1940_ops, |
| 211 | }, |
| 212 | }; |
| 213 | |
| 214 | static struct snd_soc_card h1940_asoc = { |
| 215 | .name = "h1940", |
Axel Lin | 095d79d | 2011-12-22 10:53:15 +0800 | [diff] [blame] | 216 | .owner = THIS_MODULE, |
Stephen Warren | e1d4d3c | 2014-05-30 12:42:57 -0600 | [diff] [blame] | 217 | .remove = h1940_uda1380_card_remove, |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 218 | .dai_link = h1940_uda1380_dai, |
| 219 | .num_links = ARRAY_SIZE(h1940_uda1380_dai), |
Mark Brown | 6119d01 | 2011-10-08 13:30:06 +0100 | [diff] [blame] | 220 | |
| 221 | .dapm_widgets = uda1380_dapm_widgets, |
| 222 | .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets), |
| 223 | .dapm_routes = audio_map, |
| 224 | .num_dapm_routes = ARRAY_SIZE(audio_map), |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | static int __init h1940_init(void) |
| 228 | { |
| 229 | int ret; |
| 230 | |
| 231 | if (!machine_is_h1940()) |
| 232 | return -ENODEV; |
| 233 | |
| 234 | /* configure some gpios */ |
Kukjin Kim | 232910d | 2013-01-02 10:18:58 -0800 | [diff] [blame] | 235 | ret = gpio_request(S3C_GPIO_END + 9, "speaker-power"); |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 236 | if (ret) |
| 237 | goto err_out; |
| 238 | |
Kukjin Kim | 232910d | 2013-01-02 10:18:58 -0800 | [diff] [blame] | 239 | ret = gpio_direction_output(S3C_GPIO_END + 9, 0); |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 240 | if (ret) |
| 241 | goto err_gpio; |
| 242 | |
| 243 | s3c24xx_snd_device = platform_device_alloc("soc-audio", -1); |
| 244 | if (!s3c24xx_snd_device) { |
| 245 | ret = -ENOMEM; |
| 246 | goto err_gpio; |
| 247 | } |
| 248 | |
| 249 | platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc); |
| 250 | ret = platform_device_add(s3c24xx_snd_device); |
| 251 | |
| 252 | if (ret) |
| 253 | goto err_plat; |
| 254 | |
| 255 | return 0; |
| 256 | |
| 257 | err_plat: |
| 258 | platform_device_put(s3c24xx_snd_device); |
| 259 | err_gpio: |
Kukjin Kim | 232910d | 2013-01-02 10:18:58 -0800 | [diff] [blame] | 260 | gpio_free(S3C_GPIO_END + 9); |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 261 | |
| 262 | err_out: |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static void __exit h1940_exit(void) |
| 267 | { |
| 268 | platform_device_unregister(s3c24xx_snd_device); |
Kukjin Kim | 232910d | 2013-01-02 10:18:58 -0800 | [diff] [blame] | 269 | gpio_free(S3C_GPIO_END + 9); |
Vasily Khoruzhick | 1957668 | 2010-12-09 21:17:56 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | module_init(h1940_init); |
| 273 | module_exit(h1940_exit); |
| 274 | |
| 275 | /* Module information */ |
| 276 | MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick"); |
| 277 | MODULE_DESCRIPTION("ALSA SoC H1940"); |
| 278 | MODULE_LICENSE("GPL"); |