aml_m1_mid_cs42l52.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/cs42l52.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/cs42l52.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 cs42l52_hp_detect_queue(struct work_struct*);
  107. static struct cs42l52_work_t{
  108. unsigned long data;
  109. struct work_struct cs42l52_workqueue;
  110. }cs42l52_work;
  111. void mute_spk(struct snd_soc_codec* codec, int flag)
  112. {
  113. /*
  114. int gpio_status = 0;
  115. if(flag){
  116. gpio_status = snd_soc_read(codec, WM8900_REG_GPIO);
  117. gpio_status &= ~(7<<4);
  118. gpio_status |= (6<<4);
  119. snd_soc_write(codec, WM8900_REG_GPIO, gpio_status);
  120. }else{
  121. gpio_status = snd_soc_read(codec, WM8900_REG_GPIO);
  122. gpio_status &= ~(7<<4);
  123. gpio_status |= (7<<4);
  124. snd_soc_write(codec, WM8900_REG_GPIO, gpio_status);
  125. }
  126. */
  127. }
  128. static void cs42l52_hp_detect_queue(struct work_struct* work)
  129. {
  130. int level = 0x0;
  131. struct cs42l52_work_t* pwork = container_of(work,struct cs42l52_work_t, cs42l52_workqueue);
  132. struct snd_soc_codec* codec = (struct snd_soc_codec*)(pwork->data);
  133. if ((soc_cs42l52_dai.ac97_pdata) && ((struct cs42l52_platform_data *) (soc_cs42l52_dai.ac97_pdata))->is_hp_pluged)
  134. level = ((struct cs42l52_platform_data *) (soc_cs42l52_dai.ac97_pdata))->is_hp_pluged();
  135. //printk("level = %x, hp_detect_flag = %x\n", level, hp_detect_flag);
  136. if(level == 0x1 && hp_detect_flag!= 0x1){ // HP
  137. printk("Headphone pluged in\n");
  138. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  139. snd_soc_dapm_enable_pin(codec, "MIC IN");
  140. snd_soc_dapm_sync(codec);
  141. // pull down the gpio to mute spk
  142. mute_spk(codec, 1);
  143. snd_soc_jack_report(&hp_jack, SND_JACK_HEADSET, SND_JACK_HEADSET);
  144. hp_detect_flag = level;
  145. }else if(level != hp_detect_flag){ // HDMI
  146. printk("Headphone unpluged\n");
  147. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  148. snd_soc_dapm_enable_pin(codec, "MIC IN");
  149. snd_soc_dapm_sync(codec);
  150. snd_soc_jack_report(&hp_jack,0, SND_JACK_HEADSET);
  151. hp_detect_flag = level;
  152. mute_spk(codec, 0);
  153. }
  154. }
  155. static void cs42l52_hp_detect_timer(unsigned long data)
  156. {
  157. struct snd_soc_codec *codec = (struct snd_soc_codec*) data;
  158. cs42l52_work.data = (unsigned long)codec;
  159. schedule_work(&cs42l52_work.cs42l52_workqueue);
  160. mod_timer(&timer, jiffies + HZ*1);
  161. }
  162. #endif
  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. //Add board specific DAPM widgets and routes
  168. err = snd_soc_dapm_new_controls(codec, aml_m1_dapm_widgets, ARRAY_SIZE(aml_m1_dapm_widgets));
  169. if(err){
  170. dev_warn(card->dev, "Failed to register DAPM widgets\n");
  171. return 0;
  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. #if HP_DET
  180. if ((soc_cs42l52_dai.ac97_pdata) && ((struct cs42l52_platform_data *) (soc_cs42l52_dai.ac97_pdata))->is_hp_pluged)
  181. hp_detect_flag = ((struct cs42l52_platform_data *) (soc_cs42l52_dai.ac97_pdata))->is_hp_pluged() ? (0) : (1);
  182. else
  183. hp_detect_flag = 1; // If is_hp_pluged function is not registered in bsp, set speaker as default.
  184. err = snd_soc_jack_new(card, "hp_switch",
  185. SND_JACK_HEADSET, &hp_jack);
  186. if(err){
  187. dev_warn(card->dev, "Failed to alloc resource for hook switch\n");
  188. }else{
  189. err = snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins), hp_jack_pins);
  190. if(err){
  191. dev_warn(card->dev, "Failed to setup hook hp jack pin\n");
  192. }
  193. }
  194. // create a timer to poll the HP IN status
  195. spin_lock_init(&lock);
  196. timer.function = &cs42l52_hp_detect_timer;
  197. timer.data = (unsigned long)codec;
  198. timer.expires = jiffies + HZ*10;
  199. init_timer(&timer);
  200. INIT_WORK(&cs42l52_work.cs42l52_workqueue, cs42l52_hp_detect_queue);
  201. #endif
  202. snd_soc_dapm_nc_pin(codec,"LINPUT1");
  203. snd_soc_dapm_nc_pin(codec,"RINPUT1");
  204. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  205. snd_soc_dapm_disable_pin(codec, "HP");
  206. snd_soc_dapm_enable_pin(codec, "MIC IN");
  207. snd_soc_dapm_disable_pin(codec, "HP MIC");
  208. snd_soc_dapm_disable_pin(codec, "FM IN");
  209. snd_soc_dapm_sync(codec);
  210. return 0;
  211. }
  212. static struct snd_soc_dai_link aml_m1_dai = {
  213. .name = "AML-M1",
  214. .stream_name = "AML M1 PCM",
  215. .cpu_dai = &aml_dai[0], //////
  216. .codec_dai = &soc_cs42l52_dai,
  217. .init = aml_m1_codec_init,
  218. .ops = &aml_m1_ops,
  219. };
  220. static struct snd_soc_card snd_soc_aml_m1 = {
  221. .name = "AML-M1",
  222. .platform = &aml_soc_platform,
  223. .dai_link = &aml_m1_dai,
  224. .num_links = 1,
  225. .set_bias_level = aml_m1_set_bias_level,
  226. };
  227. static struct snd_soc_device aml_m1_snd_devdata = {
  228. .card = &snd_soc_aml_m1,
  229. .codec_dev = &soc_codec_cs42l52_dev,
  230. };
  231. static struct platform_device *aml_m1_snd_device;
  232. static struct platform_device *aml_m1_platform_device;
  233. static int aml_m1_audio_probe(struct platform_device *pdev)
  234. {
  235. int ret;
  236. // TODO
  237. printk("***Entered %s:%s\n", __FILE__,__func__);
  238. aml_m1_snd_device = platform_device_alloc("soc-audio", -1);
  239. if (!aml_m1_snd_device) {
  240. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  241. ret = -ENOMEM;
  242. }
  243. platform_set_drvdata(aml_m1_snd_device,&aml_m1_snd_devdata);
  244. aml_m1_snd_devdata.dev = &aml_m1_snd_device->dev;
  245. ret = platform_device_add(aml_m1_snd_device);
  246. if (ret) {
  247. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  248. goto error;
  249. }
  250. aml_m1_platform_device = platform_device_register_simple("aml_m1_codec", -1, NULL, 0);
  251. return 0;
  252. error:
  253. platform_device_put(aml_m1_snd_device);
  254. return ret;
  255. }
  256. static int aml_m1_audio_remove(struct platform_device *pdev)
  257. {
  258. printk("***Entered %s:%s\n", __FILE__,__func__);
  259. #if HP_DET
  260. del_timer_sync(&timer);
  261. #endif
  262. platform_device_unregister(aml_m1_snd_device);
  263. return 0;
  264. }
  265. static struct platform_driver aml_m1_audio_driver = {
  266. .probe = aml_m1_audio_probe,
  267. .remove = aml_m1_audio_remove,
  268. .driver = {
  269. .name = "aml_m1_audio_cs42l52",
  270. .owner = THIS_MODULE,
  271. },
  272. };
  273. static int __init aml_m1_init(void)
  274. {
  275. return platform_driver_register(&aml_m1_audio_driver);
  276. }
  277. static void __exit aml_m1_exit(void)
  278. {
  279. platform_driver_unregister(&aml_m1_audio_driver);
  280. }
  281. module_init(aml_m1_init);
  282. module_exit(aml_m1_exit);
  283. /* Module information */
  284. MODULE_AUTHOR("AMLogic, Inc.");
  285. MODULE_DESCRIPTION("ALSA SoC AML M1 AUDIO");
  286. MODULE_LICENSE("GPL");