123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/kernel.h>
- #include <linux/clk.h>
- #include <linux/timer.h>
- #include <linux/interrupt.h>
- #include <linux/platform_device.h>
- #include <linux/i2c.h>
- #include <linux/workqueue.h>
- #include <linux/switch.h>
- #include <sound/core.h>
- #include <sound/pcm.h>
- #include <sound/pcm_params.h>
- #include <sound/soc.h>
- #include <sound/soc-dapm.h>
- #include <sound/jack.h>
- #include <asm/mach-types.h>
- #include <mach/hardware.h>
- #include <mach/gpio.h>
- #include <sound/aml_platform.h>
- #include "aml_dai.h"
- #include "aml_pcm.h"
- #include "aml_m3_codec.h"
- #include "aml_audio_hw.h"
- #define HP_DET 1
- static struct aml_audio_platform * audio_platform_data = NULL;
- #if HP_DET
- static struct snd_soc_jack hp_jack;
- static struct snd_soc_jack_pin hp_jack_pins[] = {
- { .pin = "HP", .mask = SND_JACK_HEADSET },
- { .pin = "HP_L", .mask = SND_JACK_HEADSET },
- { .pin = "HP_R", .mask = SND_JACK_HEADSET },
- { .pin = "HP_L Switch", .mask = SND_JACK_HEADSET },
- { .pin = "HP_R Switch", .mask = SND_JACK_HEADSET },
- };
- static struct timer_list timer;
- static int hp_detect_flag = 0;
- static struct switch_dev sdev;
- #endif
- static int aml_m3_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_dai *codec_dai = rtd->codec_dai;
- struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
- int ret;
- // TODO
- #ifdef _AML_M3_HW_DEBUG_
- printk("***Entered %s:%s\n", __FILE__,__func__);
- #endif
-
- 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;
-
- 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;
-
- return 0;
- }
-
- static struct snd_soc_ops aml_m3_ops = {
- .hw_params = aml_m3_hw_params,
- };
- static int aml_m3_set_bias_level(struct snd_soc_card *card,
- enum snd_soc_bias_level level)
- {
- int ret = 0;
- //struct snd_soc_codec *codec = card->dapm.codec;
- // TODO
- #ifdef _AML_M3_HW_DEBUG_
- printk("***Entered %s:%s: %d\n", __FILE__,__func__, level);
- #endif
- switch (level) {
- case SND_SOC_BIAS_ON:
- case SND_SOC_BIAS_PREPARE:
- #if HP_DET
- del_timer_sync(&timer);
- timer.expires = jiffies + HZ*5;
- del_timer(&timer);
- add_timer(&timer);
- hp_detect_flag = 0xf0000000;
- #endif
- break;
- case SND_SOC_BIAS_OFF:
- case SND_SOC_BIAS_STANDBY:
- #if HP_DET
- del_timer(&timer);
- hp_detect_flag = 0xf0000000;
- if(audio_platform_data->mute_spk)
- audio_platform_data->mute_spk(1);
- #endif
- break;
- };
- return ret;
- }
- static const struct snd_soc_dapm_widget aml_m3_dapm_widgets[] = {
- SND_SOC_DAPM_SPK("Ext Spk", NULL),
- SND_SOC_DAPM_HP("HP", NULL),
- SND_SOC_DAPM_MIC("MIC IN", NULL),
- };
- static const struct snd_soc_dapm_route intercon[] = {
- {"Ext Spk", NULL, "LINEOUTL"},
- {"Ext Spk", NULL, "LINEOUTR"},
- {"HP", NULL, "HP_L"},
- {"HP", NULL, "HP_R"},
- //{"LINEINL", "MICBIAS", "MIC IN"},
- //{"LINEINR", "MICBIAS", "MIC IN"},
-
- {"MICBIAS", NULL, "MIC IN"},
- {"LINEINR", NULL, "MICBIAS"},
- {"LINEINL", NULL, "MICBIAS"},
- };
- #if HP_DET
- /* Hook switch */
- static spinlock_t lock;
- static void aml_m3_hp_detect_queue(struct work_struct*);
- static struct aml_m3_work_t{
- unsigned long data;
- struct work_struct aml_m3_workqueue;
- }aml_m3_work;
- extern void latch_(struct snd_soc_codec* codec);
- static void aml_m3_hp_detect_queue(struct work_struct* work)
- {
- int level = 0x0;
- u16 reg;
- struct aml_m3_work_t* pwork = container_of(work,struct aml_m3_work_t, aml_m3_workqueue);
- struct snd_soc_codec* codec = (struct snd_soc_codec*)(pwork->data);
- //if ((aml_dai[1].ac97_pdata) && ((struct aml_m3_pdata *) (aml_dai[1].ac97_pdata))->is_hp_pluged)
- //level = ((struct aml_m3_pdata *) (aml_dai[1].ac97_pdata))->is_hp_pluged();
- if(audio_platform_data->is_hp_pluged)
- level = audio_platform_data->is_hp_pluged();
- //printk("level = %x, hp_detect_flag = %x\n", level, hp_detect_flag);
- if(level == 0x1 && hp_detect_flag!= 0x1){ // HP
- printk("Headphone pluged in\n");
- snd_soc_jack_report(&hp_jack, SND_JACK_HEADSET, SND_JACK_HEADSET);
- reg = snd_soc_read(codec, ADAC_MUTE_CTRL_REG1);
- reg &= ~0xc0;
- snd_soc_write(codec, ADAC_MUTE_CTRL_REG1, reg); //unmute HP
- if(audio_platform_data->mute_spk)
- audio_platform_data->mute_spk(1);
- latch_(codec);
- hp_detect_flag = level;
- switch_set_state(&sdev, 1);
- }else if(level != hp_detect_flag){ // HDMI
- printk("Headphone unpluged\n");
- snd_soc_jack_report(&hp_jack,0, SND_JACK_HEADSET);
- reg = snd_soc_read(codec, ADAC_MUTE_CTRL_REG1);
- reg |= 0xc0;
- snd_soc_write(codec, ADAC_MUTE_CTRL_REG1, reg);//mute HP
- if(audio_platform_data->mute_spk)
- audio_platform_data->mute_spk(0);
- latch_(codec);
- hp_detect_flag = level;
- switch_set_state(&sdev, 0);
- }
- }
- static void aml_m3_hp_detect_timer(unsigned long data)
- {
- struct snd_soc_codec *codec = (struct snd_soc_codec*) data;
- aml_m3_work.data = (unsigned long)codec;
- schedule_work(&aml_m3_work.aml_m3_workqueue);
- mod_timer(&timer, jiffies + HZ*1);
- }
- #endif
- static int aml_m3_codec_init(struct snd_soc_pcm_runtime *runtime)
- {
- struct snd_soc_codec* codec = runtime->codec;
- struct snd_soc_card *card = codec->card;
-
struct snd_soc_dapm_context* dapm = &codec->dapm;
- int err;
- printk("***Entered %s:%s:\n", __FILE__,__func__);
-
- //Add board specific DAPM widgets and routes
- err = snd_soc_dapm_new_controls(&codec->dapm, aml_m3_dapm_widgets, ARRAY_SIZE(aml_m3_dapm_widgets));
- if(err){
- dev_warn(card->dev, "Failed to register DAPM widgets\n");
- return 0;
- }
- err = snd_soc_dapm_add_routes(&codec->dapm, intercon,
- ARRAY_SIZE(intercon));
- if(err){
- dev_warn(card->dev, "Failed to setup dapm widgets routine\n");
- return 0;
- }
- #if HP_DET
- hp_detect_flag = 1; // If is_hp_pluged function is not registered in bsp, set speaker as default.
- err = snd_soc_jack_new(codec, "hp_switch",
- SND_JACK_HEADSET, &hp_jack);
- if(err){
- dev_warn(card->dev, "Failed to alloc resource for hook switch\n");
- }else{
- err = snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins), hp_jack_pins);
- if(err){
- dev_warn(card->dev, "Failed to setup hook hp jack pin\n");
- }
- }
- /**/
- // create a timer to poll the HP IN status
- spin_lock_init(&lock);
- timer.function = &aml_m3_hp_detect_timer;
- timer.data = (unsigned long)codec;
- timer.expires = jiffies + HZ*10;
- init_timer(&timer);
- INIT_WORK(&aml_m3_work.aml_m3_workqueue, aml_m3_hp_detect_queue);
- #endif
- snd_soc_dapm_nc_pin(dapm,"LINPUT1");
- snd_soc_dapm_nc_pin(dapm,"RINPUT1");
- snd_soc_dapm_enable_pin(dapm, "Ext Spk");
- snd_soc_dapm_disable_pin(dapm, "HP");
- snd_soc_dapm_enable_pin(dapm, "MIC IN");
- snd_soc_dapm_disable_pin(dapm, "HP MIC");
- snd_soc_dapm_disable_pin(dapm, "FM IN");
- snd_soc_dapm_sync(dapm);
- /**/
- return 0;
- }
- static struct snd_soc_dai_link aml_m3_dai = {
- .name = "AML-M3",
- .stream_name = "AML M3 PCM",
- .codec_name = "aml_m3_codec",
- .platform_name = "aml-audio",
- .cpu_dai_name = "aml-dai0",
- .codec_dai_name = "AML-M3",
- .init = aml_m3_codec_init,
- .ops = &aml_m3_ops,
- };
- static struct snd_soc_card snd_soc_aml_m3 = {
- .name = "AML-M3",
- .dai_link = &aml_m3_dai,
- .num_links = 1,
- .set_bias_level = aml_m3_set_bias_level,
- };
- static struct platform_device *aml_m3_snd_device;
- static struct platform_device *aml_m3_platform_device;
- static int aml_m3_audio_probe(struct platform_device *pdev)
- {
- int ret;
- //pdev->dev.platform_data;
- // TODO
- printk("***Entered %s:%s\n", __FILE__,__func__);
- aml_m3_snd_device = platform_device_alloc("soc-audio", -1);
- if (!aml_m3_snd_device) {
- printk(KERN_ERR "ASoC: Platform device allocation failed\n");
- ret = -ENOMEM;
- }
- audio_platform_data = (struct aml_audio_platform*) pdev->dev.platform_data;
- platform_set_drvdata(aml_m3_snd_device, &snd_soc_aml_m3);
- ret = platform_device_add(aml_m3_snd_device);
- if (ret) {
- printk(KERN_ERR "ASoC: Platform device allocation failed\n");
- goto error2;
- }
-
- aml_m3_platform_device = platform_device_register_simple("aml_m3_codec",
- -1, NULL, 0);
- #if HP_DET
- sdev.name = "h2w";//for report headphone to android
- ret = switch_dev_register(&sdev);
- if (ret < 0){
- printk(KERN_ERR "ASoC: register switch dev failed\n");
- goto error1;
- }
- #endif
- return 0;
- error1:
- platform_device_unregister(aml_m3_snd_device);
- error2:
- platform_device_put(aml_m3_snd_device);
- return ret;
- }
- static int aml_m3_audio_remove(struct platform_device *pdev)
- {
- printk("***Entered %s:%s\n", __FILE__,__func__);
- #if HP_DET
- del_timer_sync(&timer);
- switch_dev_unregister(&sdev);
- #endif
- platform_device_unregister(aml_m3_snd_device);
- return 0;
- }
- static struct platform_driver aml_m3_audio_driver = {
- .probe = aml_m3_audio_probe,
- .remove = aml_m3_audio_remove,
- .driver = {
- .name = "aml_m3_audio",
- .owner = THIS_MODULE,
- },
- };
- static int __init aml_m3_init(void)
- {
- return platform_driver_register(&aml_m3_audio_driver);
- }
- static void __exit aml_m3_exit(void)
- {
- platform_driver_unregister(&aml_m3_audio_driver);
- }
- module_init(aml_m3_init);
- module_exit(aml_m3_exit);
- /* Module information */
- MODULE_AUTHOR("AMLogic, Inc.");
- MODULE_DESCRIPTION("ALSA SoC AML M3 AUDIO");
- MODULE_LICENSE("GPL");
|