Path: blob/master/sound/soc/samsung/h1940_uda1380.c
10817 views
/*1* h1940-uda1380.c -- ALSA Soc Audio Layer2*3* Copyright (c) 2010 Arnaud Patard <[email protected]>4* Copyright (c) 2010 Vasily Khoruzhick <[email protected]>5*6* Based on version from Arnaud Patard <[email protected]>7*8* This program is free software; you can redistribute it and/or modify it9* under the terms of the GNU General Public License as published by the10* Free Software Foundation; either version 2 of the License, or (at your11* option) any later version.12*13*/1415#include <linux/gpio.h>1617#include <sound/soc.h>18#include <sound/jack.h>1920#include <plat/regs-iis.h>21#include <mach/h1940-latch.h>22#include <asm/mach-types.h>2324#include "s3c24xx-i2s.h"2526static unsigned int rates[] = {2711025,2822050,2944100,30};3132static struct snd_pcm_hw_constraint_list hw_rates = {33.count = ARRAY_SIZE(rates),34.list = rates,35.mask = 0,36};3738static struct snd_soc_jack hp_jack;3940static struct snd_soc_jack_pin hp_jack_pins[] = {41{42.pin = "Headphone Jack",43.mask = SND_JACK_HEADPHONE,44},45{46.pin = "Speaker",47.mask = SND_JACK_HEADPHONE,48.invert = 1,49},50};5152static struct snd_soc_jack_gpio hp_jack_gpios[] = {53{54.gpio = S3C2410_GPG(4),55.name = "hp-gpio",56.report = SND_JACK_HEADPHONE,57.invert = 1,58.debounce_time = 200,59},60};6162static int h1940_startup(struct snd_pcm_substream *substream)63{64struct snd_pcm_runtime *runtime = substream->runtime;6566runtime->hw.rate_min = hw_rates.list[0];67runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];68runtime->hw.rates = SNDRV_PCM_RATE_KNOT;6970return snd_pcm_hw_constraint_list(runtime, 0,71SNDRV_PCM_HW_PARAM_RATE,72&hw_rates);73}7475static int h1940_hw_params(struct snd_pcm_substream *substream,76struct snd_pcm_hw_params *params)77{78struct snd_soc_pcm_runtime *rtd = substream->private_data;79struct snd_soc_dai *cpu_dai = rtd->cpu_dai;80struct snd_soc_dai *codec_dai = rtd->codec_dai;81int div;82int ret;83unsigned int rate = params_rate(params);8485switch (rate) {86case 11025:87case 22050:88case 44100:89div = s3c24xx_i2s_get_clockrate() / (384 * rate);90if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))91div++;92break;93default:94dev_err(&rtd->dev, "%s: rate %d is not supported\n",95__func__, rate);96return -EINVAL;97}9899/* set codec DAI configuration */100ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |101SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);102if (ret < 0)103return ret;104105/* set cpu DAI configuration */106ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |107SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);108if (ret < 0)109return ret;110111/* select clock source */112ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,113SND_SOC_CLOCK_OUT);114if (ret < 0)115return ret;116117/* set MCLK division for sample rate */118ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,119S3C2410_IISMOD_384FS);120if (ret < 0)121return ret;122123/* set BCLK division for sample rate */124ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,125S3C2410_IISMOD_32FS);126if (ret < 0)127return ret;128129/* set prescaler division for sample rate */130ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,131S3C24XX_PRESCALE(div, div));132if (ret < 0)133return ret;134135return 0;136}137138static struct snd_soc_ops h1940_ops = {139.startup = h1940_startup,140.hw_params = h1940_hw_params,141};142143static int h1940_spk_power(struct snd_soc_dapm_widget *w,144struct snd_kcontrol *kcontrol, int event)145{146if (SND_SOC_DAPM_EVENT_ON(event))147gpio_set_value(H1940_LATCH_AUDIO_POWER, 1);148else149gpio_set_value(H1940_LATCH_AUDIO_POWER, 0);150151return 0;152}153154/* h1940 machine dapm widgets */155static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {156SND_SOC_DAPM_HP("Headphone Jack", NULL),157SND_SOC_DAPM_MIC("Mic Jack", NULL),158SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),159};160161/* h1940 machine audio_map */162static const struct snd_soc_dapm_route audio_map[] = {163/* headphone connected to VOUTLHP, VOUTRHP */164{"Headphone Jack", NULL, "VOUTLHP"},165{"Headphone Jack", NULL, "VOUTRHP"},166167/* ext speaker connected to VOUTL, VOUTR */168{"Speaker", NULL, "VOUTL"},169{"Speaker", NULL, "VOUTR"},170171/* mic is connected to VINM */172{"VINM", NULL, "Mic Jack"},173};174175static struct platform_device *s3c24xx_snd_device;176177static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)178{179struct snd_soc_codec *codec = rtd->codec;180struct snd_soc_dapm_context *dapm = &codec->dapm;181int err;182183/* Add h1940 specific widgets */184err = snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,185ARRAY_SIZE(uda1380_dapm_widgets));186if (err)187return err;188189/* Set up h1940 specific audio path audio_mapnects */190err = snd_soc_dapm_add_routes(dapm, audio_map,191ARRAY_SIZE(audio_map));192if (err)193return err;194195snd_soc_dapm_enable_pin(dapm, "Headphone Jack");196snd_soc_dapm_enable_pin(dapm, "Speaker");197snd_soc_dapm_enable_pin(dapm, "Mic Jack");198199snd_soc_dapm_sync(dapm);200201snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,202&hp_jack);203204snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),205hp_jack_pins);206207snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),208hp_jack_gpios);209210return 0;211}212213/* s3c24xx digital audio interface glue - connects codec <--> CPU */214static struct snd_soc_dai_link h1940_uda1380_dai[] = {215{216.name = "uda1380",217.stream_name = "UDA1380 Duplex",218.cpu_dai_name = "s3c24xx-iis",219.codec_dai_name = "uda1380-hifi",220.init = h1940_uda1380_init,221.platform_name = "samsung-audio",222.codec_name = "uda1380-codec.0-001a",223.ops = &h1940_ops,224},225};226227static struct snd_soc_card h1940_asoc = {228.name = "h1940",229.dai_link = h1940_uda1380_dai,230.num_links = ARRAY_SIZE(h1940_uda1380_dai),231};232233static int __init h1940_init(void)234{235int ret;236237if (!machine_is_h1940())238return -ENODEV;239240/* configure some gpios */241ret = gpio_request(H1940_LATCH_AUDIO_POWER, "speaker-power");242if (ret)243goto err_out;244245ret = gpio_direction_output(H1940_LATCH_AUDIO_POWER, 0);246if (ret)247goto err_gpio;248249s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);250if (!s3c24xx_snd_device) {251ret = -ENOMEM;252goto err_gpio;253}254255platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);256ret = platform_device_add(s3c24xx_snd_device);257258if (ret)259goto err_plat;260261return 0;262263err_plat:264platform_device_put(s3c24xx_snd_device);265err_gpio:266gpio_free(H1940_LATCH_AUDIO_POWER);267268err_out:269return ret;270}271272static void __exit h1940_exit(void)273{274platform_device_unregister(s3c24xx_snd_device);275snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),276hp_jack_gpios);277gpio_free(H1940_LATCH_AUDIO_POWER);278}279280module_init(h1940_init);281module_exit(h1940_exit);282283/* Module information */284MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");285MODULE_DESCRIPTION("ALSA SoC H1940");286MODULE_LICENSE("GPL");287288289