aml_m1_mid_wm8900.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/soc.h>
  14. #include <sound/soc-dapm.h>
  15. #include <sound/jack.h>
  16. #include <sound/wm8900.h>
  17. #include <asm/mach-types.h>
  18. #include <mach/hardware.h>
  19. #include <mach/gpio.h>
  20. #include "aml_dai.h"
  21. #include "aml_pcm.h"
  22. #include "../codecs/wm8900.h"
  23. #define HP_DET 1
  24. #if HP_DET
  25. static struct timer_list timer;
  26. static int hp_detect_flag = 0;
  27. void mute_spk(struct snd_soc_codec* codec, int flag);
  28. #endif
  29. static int aml_m1_hw_params(struct snd_pcm_substream *substream,
  30. struct snd_pcm_hw_params *params)
  31. {
  32. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  33. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  34. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  35. int ret;
  36. // TODO
  37. printk("***Entered %s:%s\n", __FILE__,__func__);
  38. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF|SND_SOC_DAIFMT_CBS_CFS);
  39. if(ret<0)
  40. return ret;
  41. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF|SND_SOC_DAIFMT_CBS_CFS);
  42. if(ret<0)
  43. return ret;
  44. return 0;
  45. }
  46. static struct snd_soc_ops aml_m1_ops = {
  47. .hw_params = aml_m1_hw_params,
  48. };
  49. static int aml_m1_set_bias_level(struct snd_soc_card *card,
  50. enum snd_soc_bias_level level)
  51. {
  52. int ret = 0;
  53. struct snd_soc_codec *codec = card->codec;
  54. // TODO
  55. printk("***Entered %s:%s: %d\n", __FILE__,__func__, level);
  56. switch (level) {
  57. case SND_SOC_BIAS_ON:
  58. case SND_SOC_BIAS_PREPARE:
  59. #if HP_DET
  60. del_timer_sync(&timer);
  61. timer.expires = jiffies + HZ*5;
  62. del_timer(&timer);
  63. add_timer(&timer);
  64. hp_detect_flag = 0xf0000000;
  65. #endif
  66. break;
  67. case SND_SOC_BIAS_OFF:
  68. case SND_SOC_BIAS_STANDBY:
  69. #if HP_DET
  70. del_timer(&timer);
  71. hp_detect_flag = 0xf0000000;
  72. mute_spk(codec,1);
  73. #endif
  74. break;
  75. };
  76. return ret;
  77. }
  78. static const struct snd_soc_dapm_widget aml_m1_dapm_widgets[] = {
  79. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  80. SND_SOC_DAPM_HP("HP", NULL),
  81. SND_SOC_DAPM_MIC("MIC IN", NULL),
  82. SND_SOC_DAPM_MIC("HP MIC", NULL),
  83. SND_SOC_DAPM_LINE("FM IN", NULL),
  84. };
  85. static const struct snd_soc_dapm_route intercon[] = {
  86. /* speaker connected to LINEOUT */
  87. {"Ext Spk", NULL, "LINEOUT1L"},
  88. {"Ext Spk", NULL, "LINEOUT1R"},
  89. /* mic is connected to Mic Jack, with WM8731 Mic Bias */
  90. {"HP", NULL, "HP_L"},
  91. {"HP", NULL, "HP_R"},
  92. {"LINPUT2", NULL, "Mic Bias"},
  93. {"Mic Bias", NULL, "MIC IN"},
  94. {"RINPUT2", NULL, "Mic Bias"},
  95. {"Mic Bias", NULL, "HP MIC"},
  96. {"LINPUT3", NULL, "FM IN"},
  97. {"RINPUT3", NULL, "FM IN"},
  98. };
  99. #if HP_DET
  100. /* Hook switch */
  101. static struct snd_soc_jack hp_jack;
  102. static struct snd_soc_jack_pin hp_jack_pins[] = {
  103. { .pin = "HP", .mask = SND_JACK_HEADSET },
  104. };
  105. static spinlock_t lock;
  106. static void wm8900_hp_detect_queue(struct work_struct*);
  107. static struct wm8900_work_t{
  108. unsigned long data;
  109. struct work_struct wm8900_workqueue;
  110. }wm8900_work;
  111. void mute_spk(struct snd_soc_codec* codec, int flag)
  112. {
  113. int gpio_status = 0;
  114. if(flag){
  115. gpio_status = snd_soc_read(codec, WM8900_REG_GPIO);
  116. gpio_status &= ~(7<<4);
  117. gpio_status |= (6<<4);
  118. snd_soc_write(codec, WM8900_REG_GPIO, gpio_status);
  119. }else{
  120. gpio_status = snd_soc_read(codec, WM8900_REG_GPIO);
  121. gpio_status &= ~(7<<4);
  122. gpio_status |= (7<<4);
  123. snd_soc_write(codec, WM8900_REG_GPIO, gpio_status);
  124. }
  125. }
  126. static void wm8900_hp_detect_queue(struct work_struct* work)
  127. {
  128. int level = 0x0;
  129. struct wm8900_work_t* pwork = container_of(work,struct wm8900_work_t, wm8900_workqueue);
  130. struct snd_soc_codec* codec = (struct snd_soc_codec*)(pwork->data);
  131. if ((wm8900_dai.ac97_pdata) && ((struct wm8900_platform_data *) (wm8900_dai.ac97_pdata))->is_hp_pluged)
  132. level = ((struct wm8900_platform_data *) (wm8900_dai.ac97_pdata))->is_hp_pluged();
  133. //printk("level = %x, hp_detect_flag = %x\n", level, hp_detect_flag);
  134. if(level == 0x1 && hp_detect_flag!= 0x1){ // HP
  135. printk("Headphone pluged in\n");
  136. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  137. snd_soc_dapm_enable_pin(codec, "MIC IN");
  138. snd_soc_dapm_sync(codec);
  139. // pull down the gpio to mute spk
  140. mute_spk(codec, 1);
  141. snd_soc_jack_report(&hp_jack, SND_JACK_HEADSET, SND_JACK_HEADSET);
  142. hp_detect_flag = level;
  143. }else if(level != hp_detect_flag){ // HDMI
  144. printk("Headphone unpluged\n");
  145. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  146. snd_soc_dapm_enable_pin(codec, "MIC IN");
  147. snd_soc_dapm_sync(codec);
  148. snd_soc_jack_report(&hp_jack,0, SND_JACK_HEADSET);
  149. hp_detect_flag = level;
  150. mute_spk(codec, 0);
  151. }
  152. }
  153. static void wm8900_hp_detect_timer(unsigned long data)
  154. {
  155. struct snd_soc_codec *codec = (struct snd_soc_codec*) data;
  156. wm8900_work.data = (unsigned long)codec;
  157. schedule_work(&wm8900_work.wm8900_workqueue);
  158. mod_timer(&timer, jiffies + HZ*1);
  159. }
  160. #endif
  161. static int aml_m1_codec_init(struct snd_soc_codec *codec)
  162. {
  163. struct snd_soc_card *card = codec->socdev->card;
  164. int err;
  165. //Add board specific DAPM widgets and routes
  166. err = snd_soc_dapm_new_controls(codec, aml_m1_dapm_widgets, ARRAY_SIZE(aml_m1_dapm_widgets));
  167. if(err){
  168. dev_warn(card->dev, "Failed to register DAPM widgets\n");
  169. return 0;
  170. }
  171. err = snd_soc_dapm_add_routes(codec, intercon,
  172. ARRAY_SIZE(intercon));
  173. if(err){
  174. dev_warn(card->dev, "Failed to setup dapm widgets routine\n");
  175. return 0;
  176. }
  177. #if HP_DET
  178. if ((wm8900_dai.ac97_pdata) && ((struct wm8900_platform_data *) (wm8900_dai.ac97_pdata))->is_hp_pluged)
  179. hp_detect_flag = ((struct wm8900_platform_data *) (wm8900_dai.ac97_pdata))->is_hp_pluged() ? (0) : (1);
  180. else
  181. hp_detect_flag = 1; // If is_hp_pluged function is not registered in bsp, set speaker as default.
  182. err = snd_soc_jack_new(card, "hp_switch",
  183. SND_JACK_HEADSET, &hp_jack);
  184. if(err){
  185. dev_warn(card->dev, "Failed to alloc resource for hook switch\n");
  186. }else{
  187. err = snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins), hp_jack_pins);
  188. if(err){
  189. dev_warn(card->dev, "Failed to setup hook hp jack pin\n");
  190. }
  191. }
  192. // create a timer to poll the HP IN status
  193. spin_lock_init(&lock);
  194. timer.function = &wm8900_hp_detect_timer;
  195. timer.data = (unsigned long)codec;
  196. timer.expires = jiffies + HZ*10;
  197. init_timer(&timer);
  198. INIT_WORK(&wm8900_work.wm8900_workqueue, wm8900_hp_detect_queue);
  199. #endif
  200. snd_soc_dapm_nc_pin(codec,"LINPUT1");
  201. snd_soc_dapm_nc_pin(codec,"RINPUT1");
  202. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  203. snd_soc_dapm_disable_pin(codec, "HP");
  204. snd_soc_dapm_enable_pin(codec, "MIC IN");
  205. snd_soc_dapm_disable_pin(codec, "HP MIC");
  206. snd_soc_dapm_disable_pin(codec, "FM IN");
  207. snd_soc_dapm_sync(codec);
  208. return 0;
  209. }
  210. static struct snd_soc_dai_link aml_m1_dai = {
  211. .name = "AML-M1",
  212. .stream_name = "AML M1 PCM",
  213. .cpu_dai = &aml_dai[0], //////
  214. .codec_dai = &wm8900_dai,
  215. .init = aml_m1_codec_init,
  216. .ops = &aml_m1_ops,
  217. };
  218. static struct snd_soc_card snd_soc_aml_m1 = {
  219. .name = "AML-M1",
  220. .platform = &aml_soc_platform,
  221. .dai_link = &aml_m1_dai,
  222. .num_links = 1,
  223. .set_bias_level = aml_m1_set_bias_level,
  224. };
  225. static struct snd_soc_device aml_m1_snd_devdata = {
  226. .card = &snd_soc_aml_m1,
  227. .codec_dev = &soc_codec_dev_wm8900,
  228. };
  229. static struct platform_device *aml_m1_snd_device;
  230. static struct platform_device *aml_m1_platform_device;
  231. static int aml_m1_audio_probe(struct platform_device *pdev)
  232. {
  233. int ret;
  234. // TODO
  235. printk("***Entered %s:%s\n", __FILE__,__func__);
  236. aml_m1_snd_device = platform_device_alloc("soc-audio", -1);
  237. if (!aml_m1_snd_device) {
  238. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  239. ret = -ENOMEM;
  240. }
  241. platform_set_drvdata(aml_m1_snd_device,&aml_m1_snd_devdata);
  242. aml_m1_snd_devdata.dev = &aml_m1_snd_device->dev;
  243. ret = platform_device_add(aml_m1_snd_device);
  244. if (ret) {
  245. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  246. goto error;
  247. }
  248. aml_m1_platform_device = platform_device_register_simple("aml_m1_codec", -1, NULL, 0);
  249. return 0;
  250. error:
  251. platform_device_put(aml_m1_snd_device);
  252. return ret;
  253. }
  254. static int aml_m1_audio_remove(struct platform_device *pdev)
  255. {
  256. printk("***Entered %s:%s\n", __FILE__,__func__);
  257. #if HP_DET
  258. del_timer_sync(&timer);
  259. #endif
  260. platform_device_unregister(aml_m1_snd_device);
  261. return 0;
  262. }
  263. static struct platform_driver aml_m1_audio_driver = {
  264. .probe = aml_m1_audio_probe,
  265. .remove = aml_m1_audio_remove,
  266. .driver = {
  267. .name = "aml_m1_audio_wm8900",
  268. .owner = THIS_MODULE,
  269. },
  270. };
  271. static int __init aml_m1_init(void)
  272. {
  273. return platform_driver_register(&aml_m1_audio_driver);
  274. }
  275. static void __exit aml_m1_exit(void)
  276. {
  277. platform_driver_unregister(&aml_m1_audio_driver);
  278. }
  279. module_init(aml_m1_init);
  280. module_exit(aml_m1_exit);
  281. /* Module information */
  282. MODULE_AUTHOR("AMLogic, Inc.");
  283. MODULE_DESCRIPTION("ALSA SoC AML M1 AUDIO");
  284. MODULE_LICENSE("GPL");