aml_m1_mid_rt5621.c 10.0 KB

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