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