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