aml_m3.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/kernel.h>
  4. #include <linux/clk.h>
  5. #include <linux/timer.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/i2c.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/switch.h>
  11. #include <sound/core.h>
  12. #include <sound/pcm.h>
  13. #include <sound/pcm_params.h>
  14. #include <sound/soc.h>
  15. #include <sound/soc-dapm.h>
  16. #include <sound/jack.h>
  17. #include <asm/mach-types.h>
  18. #include <mach/hardware.h>
  19. #include <mach/gpio.h>
  20. #include <sound/aml_platform.h>
  21. #include "aml_dai.h"
  22. #include "aml_pcm.h"
  23. #include "aml_m3_codec.h"
  24. #include "aml_audio_hw.h"
  25. #define HP_DET 1
  26. static struct aml_audio_platform * audio_platform_data = NULL;
  27. #if HP_DET
  28. static struct snd_soc_jack hp_jack;
  29. static struct snd_soc_jack_pin hp_jack_pins[] = {
  30. { .pin = "HP", .mask = SND_JACK_HEADSET },
  31. { .pin = "HP_L", .mask = SND_JACK_HEADSET },
  32. { .pin = "HP_R", .mask = SND_JACK_HEADSET },
  33. { .pin = "HP_L Switch", .mask = SND_JACK_HEADSET },
  34. { .pin = "HP_R Switch", .mask = SND_JACK_HEADSET },
  35. };
  36. static struct timer_list timer;
  37. static int hp_detect_flag = 0;
  38. static struct switch_dev sdev;
  39. #endif
  40. static int aml_m3_hw_params(struct snd_pcm_substream *substream,
  41. struct snd_pcm_hw_params *params)
  42. {
  43. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  44. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  45. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  46. int ret;
  47. // TODO
  48. #ifdef _AML_M3_HW_DEBUG_
  49. printk("***Entered %s:%s\n", __FILE__,__func__);
  50. #endif
  51. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF|SND_SOC_DAIFMT_CBS_CFS);
  52. if(ret<0)
  53. return ret;
  54. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF|SND_SOC_DAIFMT_CBS_CFS);
  55. if(ret<0)
  56. return ret;
  57. return 0;
  58. }
  59. static struct snd_soc_ops aml_m3_ops = {
  60. .hw_params = aml_m3_hw_params,
  61. };
  62. static int aml_m3_set_bias_level(struct snd_soc_card *card,
  63. enum snd_soc_bias_level level)
  64. {
  65. int ret = 0;
  66. //struct snd_soc_codec *codec = card->dapm.codec;
  67. // TODO
  68. #ifdef _AML_M3_HW_DEBUG_
  69. printk("***Entered %s:%s: %d\n", __FILE__,__func__, level);
  70. #endif
  71. switch (level) {
  72. case SND_SOC_BIAS_ON:
  73. case SND_SOC_BIAS_PREPARE:
  74. #if HP_DET
  75. del_timer_sync(&timer);
  76. timer.expires = jiffies + HZ*5;
  77. del_timer(&timer);
  78. add_timer(&timer);
  79. hp_detect_flag = 0xf0000000;
  80. #endif
  81. break;
  82. case SND_SOC_BIAS_OFF:
  83. case SND_SOC_BIAS_STANDBY:
  84. #if HP_DET
  85. del_timer(&timer);
  86. hp_detect_flag = 0xf0000000;
  87. if(audio_platform_data->mute_spk)
  88. audio_platform_data->mute_spk(1);
  89. #endif
  90. break;
  91. };
  92. return ret;
  93. }
  94. static const struct snd_soc_dapm_widget aml_m3_dapm_widgets[] = {
  95. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  96. SND_SOC_DAPM_HP("HP", NULL),
  97. SND_SOC_DAPM_MIC("MIC IN", NULL),
  98. };
  99. static const struct snd_soc_dapm_route intercon[] = {
  100. {"Ext Spk", NULL, "LINEOUTL"},
  101. {"Ext Spk", NULL, "LINEOUTR"},
  102. {"HP", NULL, "HP_L"},
  103. {"HP", NULL, "HP_R"},
  104. //{"LINEINL", "MICBIAS", "MIC IN"},
  105. //{"LINEINR", "MICBIAS", "MIC IN"},
  106. {"MICBIAS", NULL, "MIC IN"},
  107. {"LINEINR", NULL, "MICBIAS"},
  108. {"LINEINL", NULL, "MICBIAS"},
  109. };
  110. #if HP_DET
  111. /* Hook switch */
  112. static spinlock_t lock;
  113. static void aml_m3_hp_detect_queue(struct work_struct*);
  114. static struct aml_m3_work_t{
  115. unsigned long data;
  116. struct work_struct aml_m3_workqueue;
  117. }aml_m3_work;
  118. extern void latch_(struct snd_soc_codec* codec);
  119. static void aml_m3_hp_detect_queue(struct work_struct* work)
  120. {
  121. int level = 0x0;
  122. u16 reg;
  123. struct aml_m3_work_t* pwork = container_of(work,struct aml_m3_work_t, aml_m3_workqueue);
  124. struct snd_soc_codec* codec = (struct snd_soc_codec*)(pwork->data);
  125. //if ((aml_dai[1].ac97_pdata) && ((struct aml_m3_pdata *) (aml_dai[1].ac97_pdata))->is_hp_pluged)
  126. //level = ((struct aml_m3_pdata *) (aml_dai[1].ac97_pdata))->is_hp_pluged();
  127. if(audio_platform_data->is_hp_pluged)
  128. level = audio_platform_data->is_hp_pluged();
  129. //printk("level = %x, hp_detect_flag = %x\n", level, hp_detect_flag);
  130. if(level == 0x1 && hp_detect_flag!= 0x1){ // HP
  131. printk("Headphone pluged in\n");
  132. snd_soc_jack_report(&hp_jack, SND_JACK_HEADSET, SND_JACK_HEADSET);
  133. reg = snd_soc_read(codec, ADAC_MUTE_CTRL_REG1);
  134. reg &= ~0xc0;
  135. snd_soc_write(codec, ADAC_MUTE_CTRL_REG1, reg); //unmute HP
  136. if(audio_platform_data->mute_spk)
  137. audio_platform_data->mute_spk(1);
  138. latch_(codec);
  139. hp_detect_flag = level;
  140. switch_set_state(&sdev, 1);
  141. }else if(level != hp_detect_flag){ // HDMI
  142. printk("Headphone unpluged\n");
  143. snd_soc_jack_report(&hp_jack,0, SND_JACK_HEADSET);
  144. reg = snd_soc_read(codec, ADAC_MUTE_CTRL_REG1);
  145. reg |= 0xc0;
  146. snd_soc_write(codec, ADAC_MUTE_CTRL_REG1, reg);//mute HP
  147. if(audio_platform_data->mute_spk)
  148. audio_platform_data->mute_spk(0);
  149. latch_(codec);
  150. hp_detect_flag = level;
  151. switch_set_state(&sdev, 0);
  152. }
  153. }
  154. static void aml_m3_hp_detect_timer(unsigned long data)
  155. {
  156. struct snd_soc_codec *codec = (struct snd_soc_codec*) data;
  157. aml_m3_work.data = (unsigned long)codec;
  158. schedule_work(&aml_m3_work.aml_m3_workqueue);
  159. mod_timer(&timer, jiffies + HZ*1);
  160. }
  161. #endif
  162. static int aml_m3_codec_init(struct snd_soc_pcm_runtime *runtime)
  163. {
  164. struct snd_soc_codec* codec = runtime->codec;
  165. struct snd_soc_card *card = codec->card;
  166. struct snd_soc_dapm_context* dapm = &codec->dapm;
  167. int err;
  168. printk("***Entered %s:%s:\n", __FILE__,__func__);
  169. //Add board specific DAPM widgets and routes
  170. err = snd_soc_dapm_new_controls(&codec->dapm, aml_m3_dapm_widgets, ARRAY_SIZE(aml_m3_dapm_widgets));
  171. if(err){
  172. dev_warn(card->dev, "Failed to register DAPM widgets\n");
  173. return 0;
  174. }
  175. err = snd_soc_dapm_add_routes(&codec->dapm, intercon,
  176. ARRAY_SIZE(intercon));
  177. if(err){
  178. dev_warn(card->dev, "Failed to setup dapm widgets routine\n");
  179. return 0;
  180. }
  181. #if HP_DET
  182. hp_detect_flag = 1; // If is_hp_pluged function is not registered in bsp, set speaker as default.
  183. err = snd_soc_jack_new(codec, "hp_switch",
  184. SND_JACK_HEADSET, &hp_jack);
  185. if(err){
  186. dev_warn(card->dev, "Failed to alloc resource for hook switch\n");
  187. }else{
  188. err = snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins), hp_jack_pins);
  189. if(err){
  190. dev_warn(card->dev, "Failed to setup hook hp jack pin\n");
  191. }
  192. }
  193. /**/
  194. // create a timer to poll the HP IN status
  195. spin_lock_init(&lock);
  196. timer.function = &aml_m3_hp_detect_timer;
  197. timer.data = (unsigned long)codec;
  198. timer.expires = jiffies + HZ*10;
  199. init_timer(&timer);
  200. INIT_WORK(&aml_m3_work.aml_m3_workqueue, aml_m3_hp_detect_queue);
  201. #endif
  202. snd_soc_dapm_nc_pin(dapm,"LINPUT1");
  203. snd_soc_dapm_nc_pin(dapm,"RINPUT1");
  204. snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  205. snd_soc_dapm_disable_pin(dapm, "HP");
  206. snd_soc_dapm_enable_pin(dapm, "MIC IN");
  207. snd_soc_dapm_disable_pin(dapm, "HP MIC");
  208. snd_soc_dapm_disable_pin(dapm, "FM IN");
  209. snd_soc_dapm_sync(dapm);
  210. /**/
  211. return 0;
  212. }
  213. static struct snd_soc_dai_link aml_m3_dai = {
  214. .name = "AML-M3",
  215. .stream_name = "AML M3 PCM",
  216. .codec_name = "aml_m3_codec",
  217. .platform_name = "aml-audio",
  218. .cpu_dai_name = "aml-dai0",
  219. .codec_dai_name = "AML-M3",
  220. .init = aml_m3_codec_init,
  221. .ops = &aml_m3_ops,
  222. };
  223. static struct snd_soc_card snd_soc_aml_m3 = {
  224. .name = "AML-M3",
  225. .dai_link = &aml_m3_dai,
  226. .num_links = 1,
  227. .set_bias_level = aml_m3_set_bias_level,
  228. };
  229. static struct platform_device *aml_m3_snd_device;
  230. static struct platform_device *aml_m3_platform_device;
  231. static int aml_m3_audio_probe(struct platform_device *pdev)
  232. {
  233. int ret;
  234. //pdev->dev.platform_data;
  235. // TODO
  236. printk("***Entered %s:%s\n", __FILE__,__func__);
  237. aml_m3_snd_device = platform_device_alloc("soc-audio", -1);
  238. if (!aml_m3_snd_device) {
  239. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  240. ret = -ENOMEM;
  241. }
  242. audio_platform_data = (struct aml_audio_platform*) pdev->dev.platform_data;
  243. platform_set_drvdata(aml_m3_snd_device, &snd_soc_aml_m3);
  244. ret = platform_device_add(aml_m3_snd_device);
  245. if (ret) {
  246. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  247. goto error2;
  248. }
  249. aml_m3_platform_device = platform_device_register_simple("aml_m3_codec",
  250. -1, NULL, 0);
  251. #if HP_DET
  252. sdev.name = "h2w";//for report headphone to android
  253. ret = switch_dev_register(&sdev);
  254. if (ret < 0){
  255. printk(KERN_ERR "ASoC: register switch dev failed\n");
  256. goto error1;
  257. }
  258. #endif
  259. return 0;
  260. error1:
  261. platform_device_unregister(aml_m3_snd_device);
  262. error2:
  263. platform_device_put(aml_m3_snd_device);
  264. return ret;
  265. }
  266. static int aml_m3_audio_remove(struct platform_device *pdev)
  267. {
  268. printk("***Entered %s:%s\n", __FILE__,__func__);
  269. #if HP_DET
  270. del_timer_sync(&timer);
  271. switch_dev_unregister(&sdev);
  272. #endif
  273. platform_device_unregister(aml_m3_snd_device);
  274. return 0;
  275. }
  276. static struct platform_driver aml_m3_audio_driver = {
  277. .probe = aml_m3_audio_probe,
  278. .remove = aml_m3_audio_remove,
  279. .driver = {
  280. .name = "aml_m3_audio",
  281. .owner = THIS_MODULE,
  282. },
  283. };
  284. static int __init aml_m3_init(void)
  285. {
  286. return platform_driver_register(&aml_m3_audio_driver);
  287. }
  288. static void __exit aml_m3_exit(void)
  289. {
  290. platform_driver_unregister(&aml_m3_audio_driver);
  291. }
  292. module_init(aml_m3_init);
  293. module_exit(aml_m3_exit);
  294. /* Module information */
  295. MODULE_AUTHOR("AMLogic, Inc.");
  296. MODULE_DESCRIPTION("ALSA SoC AML M3 AUDIO");
  297. MODULE_LICENSE("GPL");