/*
 * smdk2416_wm8731.c  --  SoC audio for Neo1973
 *
 * Copyright 2007 Wolfson Microelectronics PLC.
 * Author: Graeme Gregory
 *         graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
 *
 *  Copyright (C) 2007, Ryu Euiyoul <ryu.real@gmail.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 *  Revision history
 *    20th Jan 2007   Initial version.
 *    05th Feb 2007   Rename all to Neo1973
 *
 */

/*
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
//#include <sound/driver.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>

#include <asm/mach-types.h>
#include <asm/hardware/scoop.h>
#include <plat/regs-iis.h>

#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <plat/audio.h>
#include <asm/io.h>
#include <asm/arch/spi-gpio.h>
#include <asm/arch/regs-s3c2416-clock.h>

#include "../codecs/wm8731.h"
#include "s3c-pcm.h"
#include "s3c-i2s.h"

/* define the scenarios */


#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>

#include <sound/soc.h>

#include <asm/mach-types.h>
#include <plat/regs-iis.h>
#include <mach/gta02.h>

#include "../codecs/wm8731.h"
#include "s3c24xx-i2s.h"

#define SMDK6400_AUDIO_OFF		0
#define SMDK6400_CAPTURE_MIC1		3
#define SMDK6400_STEREO_TO_HEADPHONES	2
#define SMDK6400_CAPTURE_LINE_IN	1

#undef CONFIG_SND_DEBUG

#ifdef CONFIG_SND_DEBUG
#define s3cdbg(x...) printk(x)
#else
#define s3cdbg(x...)
#endif

static struct platform_device *smdk2416_snd_device;

static int smdk2416_hifi_hw_params(struct snd_pcm_substream *substream,	struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
	struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
	int ret = 0;
	unsigned int prescaler=0;
	int rate=params_rate(params);
	u32  cdclk, clkiis;

	switch (params_rate(params)) {
	case 8000:
	case 16000:
	case 32000:
	case 48000:
	case 64000:
	case 96000:
		cdclk=12288000;
		break;
	case 11025:
	case 22050:
	case 44100:
	case 88200:
		cdclk=11289600;
		break;
	default:
		printk("Unsupported rate = %d\n", params_rate(params));
		break;
	}
	
	clkiis = s3c24xx_i2s_get_clockrate();
	
	prescaler= ((clkiis+128*rate)/(512*rate))-1;
	if(prescaler<0)
		prescaler=0;	
		/* set codec DAI configuration */
	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
					     SND_SOC_DAIFMT_NB_NF |
					     SND_SOC_DAIFMT_CBS_CFS);
	if (ret < 0)
		return ret;
	/* set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
					   SND_SOC_DAIFMT_NB_NF |
					   SND_SOC_DAIFMT_CBS_CFS);
	if (ret < 0)
		return ret;
	/* set the codec system clock for DAC and ADC */
	ret = snd_soc_dai_set_sysclk(codec_dai, 0, cdclk,
				     SND_SOC_CLOCK_IN);
	if (ret < 0)
		return ret;
	ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,S3C2410_IISMOD_32FS);
	if (ret < 0)
		return ret;
	ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK, S3C2410_IISMOD_256FS);
	if (ret < 0)
		return ret;
	ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER, ((prescaler) << 0x8));
	if (ret < 0)
		return ret;

	
#if 0
	/* set the codec register set for capture and play */
	ret = codec_dai->dai_ops.set_tdm_slot(codec_dai, substream->stream, 2);
	if (ret < 0)
		return ret;
#endif

	return 0;

}

static int smdk2416_hifi_hw_free(struct snd_pcm_substream *substream)
{
	return 0;
}

/*
 * Neo1973 WM8753 HiFi DAI opserations.
 */
static struct snd_soc_ops smdk2416_hifi_ops = {
	.hw_params = smdk2416_hifi_hw_params,
	.hw_free = smdk2416_hifi_hw_free,
};


/*
 * This is an example machine initialisation for a wm8731 connected to a
 * smdk2416. It is missing logic to detect hp/mic insertions and logic
 * to re-route the audio in such an event.
 */
static int smdk2416_wm8731_init(struct snd_soc_codec *codec)
{	
	return 0;
}



static struct snd_soc_dai_link smdk2416_dai[] = {
{ /* Hifi Playback - for similatious use with voice below */
	.name = "WM8731",
	.stream_name = "WM8731 HiFi",
	.platform_name = "samsung-audio",
	.cpu_dai_name = "s3c24xx-iis",
	.codec_dai_name = "wm8731-hifi",
	.codec_name = "wm8731",
	.init = smdk2416_wm8731_init,
	.ops = &smdk2416_hifi_ops,
},
};

static struct snd_soc_card SMDK2416wm8731 = {
	.name = "SMDK2416wm8731",
	.owner = THIS_MODULE,
	.dai_link = smdk2416_dai,
	.num_links = ARRAY_SIZE(smdk2416_dai),
};

static int __init smdk2416_init(void)
{
	int ret;

	smdk2416_snd_device = platform_device_alloc("soc-audio", -1);
	if (!smdk2416_snd_device)
		return -ENOMEM;

	platform_set_drvdata(smdk2416_snd_device, &SMDK2416wm8731);
	//smdk2416_snd_devdata.dev = &smdk2416_snd_device->dev;
	ret = platform_device_add(smdk2416_snd_device);

	if (ret){
		printk("failed add smdk2416_snd_device\n");
		platform_device_put(smdk2416_snd_device);
	}
	
	return ret;
}

static void __exit smdk2416_exit(void)
{
	platform_device_unregister(smdk2416_snd_device);
}

module_init(smdk2416_init);
module_exit(smdk2416_exit);

/* Module information */
MODULE_AUTHOR("Ryu Euiyoul");
MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973");
MODULE_LICENSE("GPL");
