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 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/moduleparam.h> |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 23 | #include <linux/i2c.h> |
| 24 | #include <linux/gpio.h> |
| 25 | #include <linux/clk.h> |
| 26 | |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 27 | #include <sound/soc.h> |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 28 | #include <sound/uda1380.h> |
| 29 | #include <sound/jack.h> |
| 30 | |
| 31 | #include <plat/regs-iis.h> |
| 32 | |
| 33 | #include <mach/regs-clock.h> |
| 34 | |
Vasily Khoruzhick | dedc3cf | 2010-09-07 17:04:15 +0300 | [diff] [blame] | 35 | #include <asm/mach-types.h> |
| 36 | |
Jassi Brar | 4b640cf | 2010-11-22 15:35:57 +0900 | [diff] [blame] | 37 | #include "dma.h" |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 38 | #include "s3c24xx-i2s.h" |
| 39 | #include "../codecs/uda1380.h" |
| 40 | |
| 41 | static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd); |
| 42 | static int rx1950_startup(struct snd_pcm_substream *substream); |
| 43 | static int rx1950_hw_params(struct snd_pcm_substream *substream, |
| 44 | struct snd_pcm_hw_params *params); |
| 45 | static int rx1950_spk_power(struct snd_soc_dapm_widget *w, |
| 46 | struct snd_kcontrol *kcontrol, int event); |
| 47 | |
| 48 | static unsigned int rates[] = { |
| 49 | 16000, |
| 50 | 44100, |
| 51 | 48000, |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static struct snd_pcm_hw_constraint_list hw_rates = { |
| 55 | .count = ARRAY_SIZE(rates), |
| 56 | .list = rates, |
| 57 | .mask = 0, |
| 58 | }; |
| 59 | |
| 60 | static struct snd_soc_jack hp_jack; |
| 61 | |
| 62 | static struct snd_soc_jack_pin hp_jack_pins[] = { |
| 63 | { |
| 64 | .pin = "Headphone Jack", |
| 65 | .mask = SND_JACK_HEADPHONE, |
| 66 | }, |
| 67 | { |
| 68 | .pin = "Speaker", |
| 69 | .mask = SND_JACK_HEADPHONE, |
| 70 | .invert = 1, |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | static struct snd_soc_jack_gpio hp_jack_gpios[] = { |
| 75 | [0] = { |
| 76 | .gpio = S3C2410_GPG(12), |
| 77 | .name = "hp-gpio", |
| 78 | .report = SND_JACK_HEADPHONE, |
| 79 | .invert = 1, |
| 80 | .debounce_time = 200, |
| 81 | }, |
| 82 | }; |
| 83 | |
| 84 | static struct snd_soc_ops rx1950_ops = { |
| 85 | .startup = rx1950_startup, |
| 86 | .hw_params = rx1950_hw_params, |
| 87 | }; |
| 88 | |
| 89 | /* s3c24xx digital audio interface glue - connects codec <--> CPU */ |
| 90 | static struct snd_soc_dai_link rx1950_uda1380_dai[] = { |
| 91 | { |
| 92 | .name = "uda1380", |
| 93 | .stream_name = "UDA1380 Duplex", |
| 94 | .cpu_dai_name = "s3c24xx-iis", |
| 95 | .codec_dai_name = "uda1380-hifi", |
| 96 | .init = rx1950_uda1380_init, |
Jassi Brar | 58bb407 | 2010-11-22 15:35:50 +0900 | [diff] [blame] | 97 | .platform_name = "samsung-audio", |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 98 | .codec_name = "uda1380-codec.0-001a", |
| 99 | .ops = &rx1950_ops, |
| 100 | }, |
| 101 | }; |
| 102 | |
| 103 | static struct snd_soc_card rx1950_asoc = { |
| 104 | .name = "rx1950", |
| 105 | .dai_link = rx1950_uda1380_dai, |
| 106 | .num_links = ARRAY_SIZE(rx1950_uda1380_dai), |
| 107 | }; |
| 108 | |
| 109 | /* rx1950 machine dapm widgets */ |
| 110 | static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = { |
| 111 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 112 | SND_SOC_DAPM_MIC("Mic Jack", NULL), |
| 113 | SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power), |
| 114 | }; |
| 115 | |
| 116 | /* rx1950 machine audio_map */ |
| 117 | static const struct snd_soc_dapm_route audio_map[] = { |
| 118 | /* headphone connected to VOUTLHP, VOUTRHP */ |
| 119 | {"Headphone Jack", NULL, "VOUTLHP"}, |
| 120 | {"Headphone Jack", NULL, "VOUTRHP"}, |
| 121 | |
| 122 | /* ext speaker connected to VOUTL, VOUTR */ |
| 123 | {"Speaker", NULL, "VOUTL"}, |
| 124 | {"Speaker", NULL, "VOUTR"}, |
| 125 | |
| 126 | /* mic is connected to VINM */ |
| 127 | {"VINM", NULL, "Mic Jack"}, |
| 128 | }; |
| 129 | |
| 130 | static struct platform_device *s3c24xx_snd_device; |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 131 | |
| 132 | static int rx1950_startup(struct snd_pcm_substream *substream) |
| 133 | { |
| 134 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 135 | |
| 136 | runtime->hw.rate_min = hw_rates.list[0]; |
| 137 | runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1]; |
| 138 | runtime->hw.rates = SNDRV_PCM_RATE_KNOT; |
| 139 | |
| 140 | return snd_pcm_hw_constraint_list(runtime, 0, |
| 141 | SNDRV_PCM_HW_PARAM_RATE, |
| 142 | &hw_rates); |
| 143 | } |
| 144 | |
| 145 | static int rx1950_spk_power(struct snd_soc_dapm_widget *w, |
| 146 | struct snd_kcontrol *kcontrol, int event) |
| 147 | { |
| 148 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 149 | gpio_set_value(S3C2410_GPA(1), 1); |
| 150 | else |
| 151 | gpio_set_value(S3C2410_GPA(1), 0); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int rx1950_hw_params(struct snd_pcm_substream *substream, |
| 157 | struct snd_pcm_hw_params *params) |
| 158 | { |
| 159 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 160 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 161 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 162 | int div; |
| 163 | int ret; |
| 164 | unsigned int rate = params_rate(params); |
| 165 | int clk_source, fs_mode; |
| 166 | |
| 167 | switch (rate) { |
| 168 | case 16000: |
| 169 | case 48000: |
| 170 | clk_source = S3C24XX_CLKSRC_PCLK; |
Vasily Khoruzhick | 1fdc7dd | 2010-09-07 17:04:18 +0300 | [diff] [blame] | 171 | fs_mode = S3C2410_IISMOD_256FS; |
| 172 | div = s3c24xx_i2s_get_clockrate() / (256 * rate); |
| 173 | if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate)) |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 174 | div++; |
| 175 | break; |
| 176 | case 44100: |
| 177 | case 88200: |
| 178 | clk_source = S3C24XX_CLKSRC_MPLL; |
Vasily Khoruzhick | ccb3b84 | 2010-11-13 14:53:41 +0200 | [diff] [blame] | 179 | fs_mode = S3C2410_IISMOD_384FS; |
| 180 | div = 1; |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 181 | break; |
| 182 | default: |
| 183 | printk(KERN_ERR "%s: rate %d is not supported\n", |
| 184 | __func__, rate); |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | /* set codec DAI configuration */ |
| 189 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S | |
| 190 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
| 191 | if (ret < 0) |
| 192 | return ret; |
| 193 | |
| 194 | /* set cpu DAI configuration */ |
| 195 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | |
| 196 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
| 197 | if (ret < 0) |
| 198 | return ret; |
| 199 | |
| 200 | /* select clock source */ |
| 201 | ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate, |
| 202 | SND_SOC_CLOCK_OUT); |
| 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | |
| 206 | /* set MCLK division for sample rate */ |
| 207 | ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK, |
Vasily Khoruzhick | ccb3b84 | 2010-11-13 14:53:41 +0200 | [diff] [blame] | 208 | fs_mode); |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 209 | if (ret < 0) |
| 210 | return ret; |
| 211 | |
| 212 | /* set BCLK division for sample rate */ |
| 213 | ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK, |
| 214 | S3C2410_IISMOD_32FS); |
| 215 | if (ret < 0) |
| 216 | return ret; |
| 217 | |
| 218 | /* set prescaler division for sample rate */ |
| 219 | ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER, |
| 220 | S3C24XX_PRESCALE(div, div)); |
| 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd) |
| 228 | { |
| 229 | struct snd_soc_codec *codec = rtd->codec; |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 230 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 231 | int err; |
| 232 | |
| 233 | /* Add rx1950 specific widgets */ |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 234 | err = snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets, |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 235 | ARRAY_SIZE(uda1380_dapm_widgets)); |
| 236 | |
| 237 | if (err) |
| 238 | return err; |
| 239 | |
| 240 | /* Set up rx1950 specific audio path audio_mapnects */ |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 241 | err = snd_soc_dapm_add_routes(dapm, audio_map, |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 242 | ARRAY_SIZE(audio_map)); |
| 243 | |
| 244 | if (err) |
| 245 | return err; |
| 246 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 247 | snd_soc_dapm_enable_pin(dapm, "Headphone Jack"); |
| 248 | snd_soc_dapm_enable_pin(dapm, "Speaker"); |
Vasily Khoruzhick | b60fc60 | 2011-01-06 22:15:49 +0200 | [diff] [blame] | 249 | snd_soc_dapm_enable_pin(dapm, "Mic Jack"); |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 250 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 251 | snd_soc_dapm_sync(dapm); |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 252 | |
| 253 | snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE, |
| 254 | &hp_jack); |
| 255 | |
| 256 | snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins), |
| 257 | hp_jack_pins); |
| 258 | |
| 259 | snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios), |
| 260 | hp_jack_gpios); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int __init rx1950_init(void) |
| 266 | { |
| 267 | int ret; |
| 268 | |
Vasily Khoruzhick | dedc3cf | 2010-09-07 17:04:15 +0300 | [diff] [blame] | 269 | if (!machine_is_rx1950()) |
| 270 | return -ENODEV; |
| 271 | |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 272 | /* configure some gpios */ |
| 273 | ret = gpio_request(S3C2410_GPA(1), "speaker-power"); |
| 274 | if (ret) |
| 275 | goto err_gpio; |
| 276 | |
| 277 | ret = gpio_direction_output(S3C2410_GPA(1), 0); |
| 278 | if (ret) |
| 279 | goto err_gpio_conf; |
| 280 | |
| 281 | s3c24xx_snd_device = platform_device_alloc("soc-audio", -1); |
| 282 | if (!s3c24xx_snd_device) { |
| 283 | ret = -ENOMEM; |
| 284 | goto err_plat_alloc; |
| 285 | } |
| 286 | |
| 287 | platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc); |
| 288 | ret = platform_device_add(s3c24xx_snd_device); |
| 289 | |
| 290 | if (ret) { |
| 291 | platform_device_put(s3c24xx_snd_device); |
| 292 | goto err_plat_add; |
| 293 | } |
| 294 | |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 295 | return 0; |
| 296 | |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 297 | err_plat_add: |
| 298 | err_plat_alloc: |
| 299 | err_gpio_conf: |
| 300 | gpio_free(S3C2410_GPA(1)); |
| 301 | |
| 302 | err_gpio: |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static void __exit rx1950_exit(void) |
| 307 | { |
| 308 | platform_device_unregister(s3c24xx_snd_device); |
| 309 | snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios), |
| 310 | hp_jack_gpios); |
Vasily Khoruzhick | 81d9780 | 2010-08-30 11:28:08 +0300 | [diff] [blame] | 311 | gpio_free(S3C2410_GPA(1)); |
| 312 | } |
| 313 | |
| 314 | module_init(rx1950_init); |
| 315 | module_exit(rx1950_exit); |
| 316 | |
| 317 | /* Module information */ |
| 318 | MODULE_AUTHOR("Vasily Khoruzhick"); |
| 319 | MODULE_DESCRIPTION("ALSA SoC RX1950"); |
| 320 | MODULE_LICENSE("GPL"); |