aml_m6_wm8960.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * aml_m6_wm8960.c -- SoC audio for AML M6
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/clk.h>
  15. #include <linux/timer.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/i2c.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/jack.h>
  26. #include <sound/wm8960.h>
  27. #include <asm/mach-types.h>
  28. #include <mach/hardware.h>
  29. #include "../codecs/wm8960.h"
  30. #include "aml_dai.h"
  31. #include "aml_pcm.h"
  32. #include "aml_audio_hw.h"
  33. #define HP_DET 1
  34. struct wm8960_private_data {
  35. int bias_level;
  36. int clock_en;
  37. #if HP_DET
  38. int timer_en;
  39. int detect_flag;
  40. struct timer_list timer;
  41. struct work_struct work;
  42. struct mutex lock;
  43. struct snd_soc_jack jack;
  44. void* data;
  45. #endif
  46. };
  47. static struct wm8960_data *wm8960_snd_pdata = NULL;
  48. static struct wm8960_private_data* wm8960_snd_priv = NULL;
  49. static void wm8960_dev_init(void)
  50. {
  51. if (wm8960_snd_pdata->device_init) {
  52. wm8960_snd_pdata->device_init();
  53. }
  54. }
  55. static void wm8960_dev_uninit(void)
  56. {
  57. if (wm8960_snd_pdata->device_uninit) {
  58. wm8960_snd_pdata->device_uninit();
  59. }
  60. }
  61. static void wm8960_set_clock(int enable)
  62. {
  63. /* set clock gating */
  64. wm8960_snd_priv->clock_en = enable;
  65. return ;
  66. }
  67. #if HP_DET
  68. static int wm8960_detect_hp(void)
  69. {
  70. int flag = -1;
  71. if (wm8960_snd_pdata->hp_detect)
  72. {
  73. flag = wm8960_snd_pdata->hp_detect();
  74. }
  75. return flag;
  76. }
  77. static void wm8960_start_timer(unsigned long delay)
  78. {
  79. wm8960_snd_priv->timer.expires = jiffies + delay;
  80. wm8960_snd_priv->timer.data = (unsigned long)wm8960_snd_priv;
  81. wm8960_snd_priv->detect_flag = -1;
  82. add_timer(&wm8960_snd_priv->timer);
  83. wm8960_snd_priv->timer_en = 1;
  84. }
  85. static void wm8960_stop_timer(void)
  86. {
  87. del_timer_sync(&wm8960_snd_priv->timer);
  88. cancel_work_sync(&wm8960_snd_priv->work);
  89. wm8960_snd_priv->timer_en = 0;
  90. wm8960_snd_priv->detect_flag = -1;
  91. }
  92. static void wm8960_work_func(struct work_struct *work)
  93. {
  94. struct wm8960_private_data *pdata = NULL;
  95. struct snd_soc_codec *codec = NULL;
  96. int flag = -1;
  97. int status = SND_JACK_HEADPHONE;
  98. pdata = container_of(work, struct wm8960_private_data, work);
  99. codec = (struct snd_soc_codec *)pdata->data;
  100. flag = wm8960_detect_hp();
  101. if(pdata->detect_flag != flag) {
  102. if (flag == 1) {
  103. printk(KERN_INFO "wm8960 hp pluged\n");
  104. /* Speaker */
  105. snd_soc_update_bits(codec, WM8960_LOUT2, 0x7f, 0);
  106. snd_soc_update_bits(codec, WM8960_ROUT2, 0x7f, 0);
  107. /* Headphone */
  108. snd_soc_update_bits(codec, WM8960_LOUT1, 0x7f, 0x7f);
  109. snd_soc_update_bits(codec, WM8960_ROUT1, 0x7f, 0x7f);
  110. /* DAC Mono Mix clear */
  111. snd_soc_update_bits(codec, WM8960_ADDCTL1, 1 << 4, 0);
  112. /* jack report */
  113. snd_soc_jack_report(&pdata->jack, status, SND_JACK_HEADPHONE);
  114. } else {
  115. printk(KERN_INFO "wm8960 hp unpluged\n");
  116. /* Speaker */
  117. snd_soc_update_bits(codec, WM8960_LOUT2, 0x7f, 0x7f);
  118. snd_soc_update_bits(codec, WM8960_ROUT2, 0x7f, 0x7f);
  119. /* Headphone */
  120. snd_soc_update_bits(codec, WM8960_LOUT1, 0x7f, 0);
  121. snd_soc_update_bits(codec, WM8960_ROUT1, 0x7f, 0);
  122. /* DAC Mono Mix set */
  123. snd_soc_update_bits(codec, WM8960_ADDCTL1, 1 << 4, 1 << 4);
  124. /* jack report */
  125. snd_soc_jack_report(&pdata->jack, 0, SND_JACK_HEADPHONE);
  126. }
  127. pdata->detect_flag = flag;
  128. }
  129. }
  130. static void wm8960_timer_func(unsigned long data)
  131. {
  132. struct wm8960_private_data *pdata = (struct wm8960_private_data *)data;
  133. unsigned long delay = msecs_to_jiffies(200);
  134. schedule_work(&pdata->work);
  135. mod_timer(&pdata->timer, jiffies + delay);
  136. }
  137. #endif
  138. static int wm8960_prepare(struct snd_pcm_substream *substream)
  139. {
  140. printk(KERN_DEBUG "enter %s stream: %s\n", __func__, (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? "playback" : "capture");
  141. #if HP_DET
  142. mutex_lock(&wm8960_snd_priv->lock);
  143. if (!wm8960_snd_priv->timer_en) {
  144. wm8960_start_timer(msecs_to_jiffies(100));
  145. }
  146. mutex_unlock(&wm8960_snd_priv->lock);
  147. #endif
  148. return 0;
  149. }
  150. static int wm8960_hw_params(struct snd_pcm_substream *substream,
  151. struct snd_pcm_hw_params *params)
  152. {
  153. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  154. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  155. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  156. int ret;
  157. printk(KERN_DEBUG "enter %s rate: %d format: %d\n", __func__, params_rate(params), params_format(params));
  158. /* set codec DAI configuration */
  159. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  160. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  161. if (ret < 0) {
  162. printk(KERN_ERR "%s: set codec dai fmt failed!\n", __func__);
  163. return ret;
  164. }
  165. /* set cpu DAI configuration */
  166. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  167. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  168. if (ret < 0) {
  169. printk(KERN_ERR "%s: set cpu dai fmt failed!\n", __func__);
  170. return ret;
  171. }
  172. /* set cpu DAI clock */
  173. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, params_rate(params) * 256, SND_SOC_CLOCK_OUT);
  174. if (ret < 0) {
  175. printk(KERN_ERR "%s: set cpu dai sysclk failed (rate: %d)!\n", __func__, params_rate(params));
  176. return ret;
  177. }
  178. return 0;
  179. }
  180. static struct snd_soc_ops wm8960_soc_ops = {
  181. .prepare = wm8960_prepare,
  182. .hw_params = wm8960_hw_params,
  183. };
  184. static int wm8960_set_bias_level(struct snd_soc_card *card,
  185. enum snd_soc_bias_level level)
  186. {
  187. int ret = 0;
  188. printk(KERN_DEBUG "enter %s level: %d\n", __func__, level);
  189. if (wm8960_snd_priv->bias_level == (int)level)
  190. return 0;
  191. switch (level) {
  192. case SND_SOC_BIAS_ON:
  193. #if HP_DET
  194. /* start timer */
  195. mutex_lock(&wm8960_snd_priv->lock);
  196. if (!wm8960_snd_priv->timer_en) {
  197. wm8960_start_timer(msecs_to_jiffies(100));
  198. }
  199. mutex_unlock(&wm8960_snd_priv->lock);
  200. #endif
  201. break;
  202. case SND_SOC_BIAS_PREPARE:
  203. /* clock enable */
  204. if (!wm8960_snd_priv->clock_en) {
  205. wm8960_set_clock(1);
  206. }
  207. break;
  208. case SND_SOC_BIAS_OFF:
  209. case SND_SOC_BIAS_STANDBY:
  210. /* clock disable */
  211. if (wm8960_snd_priv->clock_en) {
  212. wm8960_set_clock(0);
  213. }
  214. #if HP_DET
  215. /* stop timer */
  216. mutex_lock(&wm8960_snd_priv->lock);
  217. if (wm8960_snd_priv->timer_en) {
  218. wm8960_stop_timer();
  219. }
  220. mutex_unlock(&wm8960_snd_priv->lock);
  221. #endif
  222. break;
  223. default:
  224. return ret;
  225. }
  226. wm8960_snd_priv->bias_level = (int)level;
  227. return ret;
  228. }
  229. #ifdef CONFIG_PM_SLEEP
  230. static int wm8960_suspend_pre(struct snd_soc_card *card)
  231. {
  232. printk(KERN_DEBUG "enter %s\n", __func__);
  233. #if HP_DET
  234. /* stop timer */
  235. mutex_lock(&wm8960_snd_priv->lock);
  236. if (wm8960_snd_priv->timer_en) {
  237. wm8960_stop_timer();
  238. }
  239. mutex_unlock(&wm8960_snd_priv->lock);
  240. #endif
  241. return 0;
  242. }
  243. static int wm8960_suspend_post(struct snd_soc_card *card)
  244. {
  245. printk(KERN_DEBUG "enter %s\n", __func__);
  246. return 0;
  247. }
  248. static int wm8960_resume_pre(struct snd_soc_card *card)
  249. {
  250. printk(KERN_DEBUG "enter %s\n", __func__);
  251. return 0;
  252. }
  253. static int wm8960_resume_post(struct snd_soc_card *card)
  254. {
  255. printk(KERN_DEBUG "enter %s\n", __func__);
  256. return 0;
  257. }
  258. #else
  259. #define wm8960_suspend_pre NULL
  260. #define wm8960_suspend_post NULL
  261. #define wm8960_resume_pre NULL
  262. #define wm8960_resume_post NULL
  263. #endif
  264. static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = {
  265. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  266. SND_SOC_DAPM_HP("HP", NULL),
  267. SND_SOC_DAPM_MIC("Mic", NULL),
  268. };
  269. static const struct snd_soc_dapm_route wm8960_dapm_intercon[] = {
  270. {"Ext Spk", NULL, "SPK_LP"},
  271. {"Ext Spk", NULL, "SPK_LN"},
  272. {"HP", NULL, "HP_L"},
  273. {"HP", NULL, "HP_R"},
  274. {"MICB", NULL, "Mic"},
  275. {"LINPUT1", NULL, "MICB"},
  276. {"LINPUT2", NULL, "MICB"},
  277. };
  278. #if HP_DET
  279. static struct snd_soc_jack_pin jack_pins[] = {
  280. {
  281. .pin = "HP",
  282. .mask = SND_JACK_HEADPHONE,
  283. }
  284. };
  285. #endif
  286. static int wm8960_codec_init(struct snd_soc_pcm_runtime *rtd)
  287. {
  288. struct snd_soc_codec *codec = rtd->codec;
  289. //struct snd_soc_dai *codec_dai = rtd->codec_dai;
  290. struct snd_soc_dapm_context *dapm = &codec->dapm;
  291. int ret = 0;
  292. printk(KERN_DEBUG "enter %s wm8960_snd_pdata: %p\n", __func__, wm8960_snd_pdata);
  293. /* Add specific widgets */
  294. snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets,
  295. ARRAY_SIZE(wm8960_dapm_widgets));
  296. /* Set up specific audio path interconnects */
  297. snd_soc_dapm_add_routes(dapm, wm8960_dapm_intercon, ARRAY_SIZE(wm8960_dapm_intercon));
  298. /* set ADCLRC/GPIO1 Pin Function Select */
  299. snd_soc_update_bits(codec, WM8960_IFACE2, (1 << 6), (1 << 6));
  300. /* not connected */
  301. snd_soc_dapm_nc_pin(dapm, "LINPUT3");
  302. snd_soc_dapm_nc_pin(dapm, "RINPUT3");
  303. snd_soc_dapm_nc_pin(dapm, "RINPUT2");
  304. snd_soc_dapm_nc_pin(dapm, "RINPUT1");
  305. snd_soc_dapm_nc_pin(dapm, "OUT3");
  306. snd_soc_dapm_nc_pin(dapm, "SPK_RP");
  307. snd_soc_dapm_nc_pin(dapm, "SPK_RN");
  308. /* always connected */
  309. snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  310. snd_soc_dapm_enable_pin(dapm, "Mic");
  311. /* disable connected */
  312. snd_soc_dapm_disable_pin(dapm, "HP");
  313. snd_soc_dapm_sync(dapm);
  314. #if HP_DET
  315. ret = snd_soc_jack_new(codec, "hp switch", SND_JACK_HEADPHONE, &wm8960_snd_priv->jack);
  316. if (ret) {
  317. printk(KERN_WARNING "Failed to alloc resource for hp switch\n");
  318. } else {
  319. ret = snd_soc_jack_add_pins(&wm8960_snd_priv->jack, ARRAY_SIZE(jack_pins), jack_pins);
  320. if (ret) {
  321. printk(KERN_WARNING "Failed to setup hp pins\n");
  322. }
  323. }
  324. wm8960_snd_priv->data= (void*)codec;
  325. init_timer(&wm8960_snd_priv->timer);
  326. wm8960_snd_priv->timer.function = wm8960_timer_func;
  327. wm8960_snd_priv->timer.data = (unsigned long)wm8960_snd_priv;
  328. INIT_WORK(&wm8960_snd_priv->work, wm8960_work_func);
  329. mutex_init(&wm8960_snd_priv->lock);
  330. #endif
  331. return 0;
  332. }
  333. static struct snd_soc_dai_link wm8960_dai_link[] = {
  334. {
  335. .name = "WM8960",
  336. .stream_name = "WM8960 PCM",
  337. .cpu_dai_name = "aml-dai0",
  338. .codec_dai_name = "wm8960-hifi",
  339. .init = wm8960_codec_init,
  340. .platform_name = "aml-audio.0",
  341. .codec_name = "wm8960-codec.1-001a",
  342. .ops = &wm8960_soc_ops,
  343. },
  344. };
  345. static struct snd_soc_card snd_soc_wm8960 = {
  346. .name = "AML-WM8960",
  347. .driver_name = "SOC-Audio",
  348. .dai_link = &wm8960_dai_link[0],
  349. .num_links = ARRAY_SIZE(wm8960_dai_link),
  350. .set_bias_level = wm8960_set_bias_level,
  351. #ifdef CONFIG_PM_SLEEP
  352. .suspend_pre = wm8960_suspend_pre,
  353. .suspend_post = wm8960_suspend_post,
  354. .resume_pre = wm8960_resume_pre,
  355. .resume_post = wm8960_resume_post,
  356. #endif
  357. };
  358. static struct platform_device *wm8960_snd_device = NULL;
  359. static int wm8960_audio_probe(struct platform_device *pdev)
  360. {
  361. int ret = 0;
  362. printk(KERN_DEBUG "enter %s\n", __func__);
  363. wm8960_snd_pdata = pdev->dev.platform_data;
  364. snd_BUG_ON(!wm8960_snd_pdata);
  365. wm8960_snd_priv = (struct wm8960_private_data*)kzalloc(sizeof(struct wm8960_private_data), GFP_KERNEL);
  366. if (!wm8960_snd_priv) {
  367. printk(KERN_ERR "ASoC: Platform driver data allocation failed\n");
  368. return -ENOMEM;
  369. }
  370. wm8960_snd_device = platform_device_alloc("soc-audio", -1);
  371. if (!wm8960_snd_device) {
  372. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  373. ret = -ENOMEM;
  374. goto err;
  375. }
  376. platform_set_drvdata(wm8960_snd_device, &snd_soc_wm8960);
  377. ret = platform_device_add(wm8960_snd_device);
  378. if (ret) {
  379. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  380. goto err_device_add;
  381. }
  382. wm8960_snd_priv->bias_level = SND_SOC_BIAS_OFF;
  383. wm8960_snd_priv->clock_en = 0;
  384. wm8960_dev_init();
  385. return ret;
  386. err_device_add:
  387. platform_device_put(wm8960_snd_device);
  388. err:
  389. kfree(wm8960_snd_priv);
  390. return ret;
  391. }
  392. static int wm8960_audio_remove(struct platform_device *pdev)
  393. {
  394. int ret = 0;
  395. wm8960_dev_uninit();
  396. platform_device_put(wm8960_snd_device);
  397. kfree(wm8960_snd_priv);
  398. wm8960_snd_device = NULL;
  399. wm8960_snd_priv = NULL;
  400. wm8960_snd_pdata = NULL;
  401. return ret;
  402. }
  403. static struct platform_driver aml_m6_wm8960_driver = {
  404. .probe = wm8960_audio_probe,
  405. .remove = __devexit_p(wm8960_audio_remove),
  406. .driver = {
  407. .name = "aml_wm8960_audio",
  408. .owner = THIS_MODULE,
  409. },
  410. };
  411. static int __init aml_m6_wm8960_init(void)
  412. {
  413. return platform_driver_register(&aml_m6_wm8960_driver);
  414. }
  415. static void __exit aml_m6_wm8960_exit(void)
  416. {
  417. platform_driver_unregister(&aml_m6_wm8960_driver);
  418. }
  419. module_init(aml_m6_wm8960_init);
  420. module_exit(aml_m6_wm8960_exit);
  421. /* Module information */
  422. MODULE_AUTHOR("AMLogic, Inc.");
  423. MODULE_DESCRIPTION("AML WM8960 audio driver");
  424. MODULE_LICENSE("GPL");