Path: blob/master/sound/soc/samsung/rx1950_uda1380.c
10817 views
/*1* rx1950.c -- ALSA Soc Audio Layer2*3* Copyright (c) 2010 Vasily Khoruzhick <[email protected]>4*5* Based on smdk2440.c and magician.c6*7* Authors: Graeme Gregory [email protected]8* Philipp Zabel <[email protected]>9* Denis Grigoriev <[email protected]>10* Vasily Khoruzhick <[email protected]>11*12* This program is free software; you can redistribute it and/or modify it13* under the terms of the GNU General Public License as published by the14* Free Software Foundation; either version 2 of the License, or (at your15* option) any later version.16*17*/1819#include <linux/gpio.h>2021#include <sound/soc.h>22#include <sound/jack.h>2324#include <plat/regs-iis.h>25#include <asm/mach-types.h>2627#include "s3c24xx-i2s.h"2829static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);30static int rx1950_startup(struct snd_pcm_substream *substream);31static int rx1950_hw_params(struct snd_pcm_substream *substream,32struct snd_pcm_hw_params *params);33static int rx1950_spk_power(struct snd_soc_dapm_widget *w,34struct snd_kcontrol *kcontrol, int event);3536static unsigned int rates[] = {3716000,3844100,3948000,40};4142static struct snd_pcm_hw_constraint_list hw_rates = {43.count = ARRAY_SIZE(rates),44.list = rates,45.mask = 0,46};4748static struct snd_soc_jack hp_jack;4950static struct snd_soc_jack_pin hp_jack_pins[] = {51{52.pin = "Headphone Jack",53.mask = SND_JACK_HEADPHONE,54},55{56.pin = "Speaker",57.mask = SND_JACK_HEADPHONE,58.invert = 1,59},60};6162static struct snd_soc_jack_gpio hp_jack_gpios[] = {63[0] = {64.gpio = S3C2410_GPG(12),65.name = "hp-gpio",66.report = SND_JACK_HEADPHONE,67.invert = 1,68.debounce_time = 200,69},70};7172static struct snd_soc_ops rx1950_ops = {73.startup = rx1950_startup,74.hw_params = rx1950_hw_params,75};7677/* s3c24xx digital audio interface glue - connects codec <--> CPU */78static struct snd_soc_dai_link rx1950_uda1380_dai[] = {79{80.name = "uda1380",81.stream_name = "UDA1380 Duplex",82.cpu_dai_name = "s3c24xx-iis",83.codec_dai_name = "uda1380-hifi",84.init = rx1950_uda1380_init,85.platform_name = "samsung-audio",86.codec_name = "uda1380-codec.0-001a",87.ops = &rx1950_ops,88},89};9091static struct snd_soc_card rx1950_asoc = {92.name = "rx1950",93.dai_link = rx1950_uda1380_dai,94.num_links = ARRAY_SIZE(rx1950_uda1380_dai),95};9697/* rx1950 machine dapm widgets */98static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {99SND_SOC_DAPM_HP("Headphone Jack", NULL),100SND_SOC_DAPM_MIC("Mic Jack", NULL),101SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),102};103104/* rx1950 machine audio_map */105static const struct snd_soc_dapm_route audio_map[] = {106/* headphone connected to VOUTLHP, VOUTRHP */107{"Headphone Jack", NULL, "VOUTLHP"},108{"Headphone Jack", NULL, "VOUTRHP"},109110/* ext speaker connected to VOUTL, VOUTR */111{"Speaker", NULL, "VOUTL"},112{"Speaker", NULL, "VOUTR"},113114/* mic is connected to VINM */115{"VINM", NULL, "Mic Jack"},116};117118static struct platform_device *s3c24xx_snd_device;119120static int rx1950_startup(struct snd_pcm_substream *substream)121{122struct snd_pcm_runtime *runtime = substream->runtime;123124runtime->hw.rate_min = hw_rates.list[0];125runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];126runtime->hw.rates = SNDRV_PCM_RATE_KNOT;127128return snd_pcm_hw_constraint_list(runtime, 0,129SNDRV_PCM_HW_PARAM_RATE,130&hw_rates);131}132133static int rx1950_spk_power(struct snd_soc_dapm_widget *w,134struct snd_kcontrol *kcontrol, int event)135{136if (SND_SOC_DAPM_EVENT_ON(event))137gpio_set_value(S3C2410_GPA(1), 1);138else139gpio_set_value(S3C2410_GPA(1), 0);140141return 0;142}143144static int rx1950_hw_params(struct snd_pcm_substream *substream,145struct snd_pcm_hw_params *params)146{147struct snd_soc_pcm_runtime *rtd = substream->private_data;148struct snd_soc_dai *cpu_dai = rtd->cpu_dai;149struct snd_soc_dai *codec_dai = rtd->codec_dai;150int div;151int ret;152unsigned int rate = params_rate(params);153int clk_source, fs_mode;154155switch (rate) {156case 16000:157case 48000:158clk_source = S3C24XX_CLKSRC_PCLK;159fs_mode = S3C2410_IISMOD_256FS;160div = s3c24xx_i2s_get_clockrate() / (256 * rate);161if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))162div++;163break;164case 44100:165case 88200:166clk_source = S3C24XX_CLKSRC_MPLL;167fs_mode = S3C2410_IISMOD_384FS;168div = 1;169break;170default:171printk(KERN_ERR "%s: rate %d is not supported\n",172__func__, rate);173return -EINVAL;174}175176/* set codec DAI configuration */177ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |178SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);179if (ret < 0)180return ret;181182/* set cpu DAI configuration */183ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |184SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);185if (ret < 0)186return ret;187188/* select clock source */189ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,190SND_SOC_CLOCK_OUT);191if (ret < 0)192return ret;193194/* set MCLK division for sample rate */195ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,196fs_mode);197if (ret < 0)198return ret;199200/* set BCLK division for sample rate */201ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,202S3C2410_IISMOD_32FS);203if (ret < 0)204return ret;205206/* set prescaler division for sample rate */207ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,208S3C24XX_PRESCALE(div, div));209if (ret < 0)210return ret;211212return 0;213}214215static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)216{217struct snd_soc_codec *codec = rtd->codec;218struct snd_soc_dapm_context *dapm = &codec->dapm;219int err;220221/* Add rx1950 specific widgets */222err = snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,223ARRAY_SIZE(uda1380_dapm_widgets));224225if (err)226return err;227228/* Set up rx1950 specific audio path audio_mapnects */229err = snd_soc_dapm_add_routes(dapm, audio_map,230ARRAY_SIZE(audio_map));231232if (err)233return err;234235snd_soc_dapm_enable_pin(dapm, "Headphone Jack");236snd_soc_dapm_enable_pin(dapm, "Speaker");237snd_soc_dapm_enable_pin(dapm, "Mic Jack");238239snd_soc_dapm_sync(dapm);240241snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,242&hp_jack);243244snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),245hp_jack_pins);246247snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),248hp_jack_gpios);249250return 0;251}252253static int __init rx1950_init(void)254{255int ret;256257if (!machine_is_rx1950())258return -ENODEV;259260/* configure some gpios */261ret = gpio_request(S3C2410_GPA(1), "speaker-power");262if (ret)263goto err_gpio;264265ret = gpio_direction_output(S3C2410_GPA(1), 0);266if (ret)267goto err_gpio_conf;268269s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);270if (!s3c24xx_snd_device) {271ret = -ENOMEM;272goto err_plat_alloc;273}274275platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);276ret = platform_device_add(s3c24xx_snd_device);277278if (ret) {279platform_device_put(s3c24xx_snd_device);280goto err_plat_add;281}282283return 0;284285err_plat_add:286err_plat_alloc:287err_gpio_conf:288gpio_free(S3C2410_GPA(1));289290err_gpio:291return ret;292}293294static void __exit rx1950_exit(void)295{296platform_device_unregister(s3c24xx_snd_device);297snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),298hp_jack_gpios);299gpio_free(S3C2410_GPA(1));300}301302module_init(rx1950_init);303module_exit(rx1950_exit);304305/* Module information */306MODULE_AUTHOR("Vasily Khoruzhick");307MODULE_DESCRIPTION("ALSA SoC RX1950");308MODULE_LICENSE("GPL");309310311