aml_m6_rt5631.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * aml_m6_rt5631.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/rt5631.h>
  27. #include <asm/mach-types.h>
  28. #include <mach/hardware.h>
  29. #include <linux/switch.h>
  30. #include "../codecs/rt5631.h"
  31. #include "aml_dai.h"
  32. #include "aml_pcm.h"
  33. #include "aml_audio_hw.h"
  34. #define HP_DET 1
  35. struct rt5631_private_data {
  36. int bias_level;
  37. int clock_en;
  38. #if HP_DET
  39. int timer_en;
  40. int detect_flag;
  41. struct timer_list timer;
  42. struct work_struct work;
  43. struct mutex lock;
  44. struct snd_soc_jack jack;
  45. void* data;
  46. struct switch_dev sdev; // for android
  47. #endif
  48. };
  49. static struct rt5631_platform_data *rt5631_snd_pdata = NULL;
  50. static struct rt5631_private_data* rt5631_snd_priv = NULL;
  51. static void rt5631_dev_init(void)
  52. {
  53. if (rt5631_snd_pdata->device_init) {
  54. rt5631_snd_pdata->device_init();
  55. }
  56. }
  57. static void rt5631_dev_uninit(void)
  58. {
  59. if (rt5631_snd_pdata->device_uninit) {
  60. rt5631_snd_pdata->device_uninit();
  61. }
  62. }
  63. static void rt5631_set_clock(int enable)
  64. {
  65. /* set clock gating */
  66. rt5631_snd_priv->clock_en = enable;
  67. return ;
  68. }
  69. static void rt5631_set_output(struct snd_soc_codec *codec)
  70. {
  71. struct snd_soc_dapm_context *dapm = &codec->dapm;
  72. if (rt5631_snd_pdata->spk_output != RT5631_SPK_STEREO) {
  73. if (rt5631_snd_pdata->spk_output == RT5631_SPK_RIGHT) {
  74. snd_soc_dapm_nc_pin(dapm, "SPOL");
  75. snd_soc_update_bits(codec, RT5631_SPK_MONO_OUT_CTRL,
  76. 0xf000,
  77. RT5631_M_SPKVOL_L_TO_SPOL_MIXER | RT5631_M_SPKVOL_R_TO_SPOL_MIXER);
  78. } else {
  79. snd_soc_dapm_nc_pin(dapm, "SPOR");
  80. snd_soc_update_bits(codec, RT5631_SPK_MONO_OUT_CTRL,
  81. 0xf000,
  82. RT5631_M_SPKVOL_L_TO_SPOR_MIXER | RT5631_M_SPKVOL_R_TO_SPOR_MIXER);
  83. }
  84. snd_soc_update_bits(codec, RT5631_SPK_MONO_HP_OUT_CTRL,
  85. RT5631_SPK_L_MUX_SEL_MASK | RT5631_SPK_R_MUX_SEL_MASK | RT5631_HP_L_MUX_SEL_MASK | RT5631_HP_R_MUX_SEL_MASK,
  86. RT5631_SPK_L_MUX_SEL_SPKMIXER_L | RT5631_SPK_R_MUX_SEL_SPKMIXER_R | RT5631_HP_L_MUX_SEL_HPVOL_L | RT5631_HP_R_MUX_SEL_HPVOL_R);
  87. } else {
  88. snd_soc_update_bits(codec, RT5631_SPK_MONO_OUT_CTRL,
  89. 0xf000,
  90. RT5631_M_SPKVOL_R_TO_SPOL_MIXER | RT5631_M_SPKVOL_L_TO_SPOR_MIXER);
  91. snd_soc_update_bits(codec, RT5631_SPK_MONO_HP_OUT_CTRL,
  92. RT5631_SPK_L_MUX_SEL_MASK | RT5631_SPK_R_MUX_SEL_MASK | RT5631_HP_L_MUX_SEL_MASK | RT5631_HP_R_MUX_SEL_MASK,
  93. RT5631_SPK_L_MUX_SEL_SPKMIXER_L | RT5631_SPK_R_MUX_SEL_SPKMIXER_R | RT5631_HP_L_MUX_SEL_HPVOL_L | RT5631_HP_R_MUX_SEL_HPVOL_R);
  94. }
  95. }
  96. static void rt5631_set_input(struct snd_soc_codec *codec)
  97. {
  98. if (rt5631_snd_pdata->mic_input == RT5631_MIC_SINGLEENDED) {
  99. /* single-ended input mode */
  100. snd_soc_update_bits(codec, RT5631_MIC_CTRL_1,
  101. RT5631_MIC1_DIFF_INPUT_CTRL,
  102. 0);
  103. } else {
  104. /* differential input mode */
  105. snd_soc_update_bits(codec, RT5631_MIC_CTRL_1,
  106. RT5631_MIC1_DIFF_INPUT_CTRL,
  107. RT5631_MIC1_DIFF_INPUT_CTRL);
  108. }
  109. }
  110. #if HP_DET
  111. static int rt5631_detect_hp(void)
  112. {
  113. int flag = -1;
  114. if (rt5631_snd_pdata->hp_detect)
  115. {
  116. flag = rt5631_snd_pdata->hp_detect();
  117. }
  118. return flag;
  119. }
  120. static void rt5631_start_timer(unsigned long delay)
  121. {
  122. rt5631_snd_priv->timer.expires = jiffies + delay;
  123. rt5631_snd_priv->timer.data = (unsigned long)rt5631_snd_priv;
  124. rt5631_snd_priv->detect_flag = -1;
  125. add_timer(&rt5631_snd_priv->timer);
  126. rt5631_snd_priv->timer_en = 1;
  127. }
  128. static void rt5631_stop_timer(void)
  129. {
  130. del_timer_sync(&rt5631_snd_priv->timer);
  131. cancel_work_sync(&rt5631_snd_priv->work);
  132. rt5631_snd_priv->timer_en = 0;
  133. rt5631_snd_priv->detect_flag = -1;
  134. }
  135. static void rt5631_work_func(struct work_struct *work)
  136. {
  137. struct rt5631_private_data *pdata = NULL;
  138. struct snd_soc_codec *codec = NULL;
  139. int jack_type = 0;
  140. int flag = -1;
  141. int status = SND_JACK_HEADPHONE;
  142. pdata = container_of(work, struct rt5631_private_data, work);
  143. codec = (struct snd_soc_codec *)pdata->data;
  144. flag = rt5631_detect_hp();
  145. if(pdata->detect_flag != flag) {
  146. if (flag == 1) {
  147. jack_type = rt5631_headset_detect(codec, 1);
  148. printk(KERN_INFO "rt5631 hp pluged jack_type: %d\n", jack_type);
  149. snd_soc_jack_report(&pdata->jack, status, SND_JACK_HEADPHONE);
  150. switch_set_state(&pdata->sdev, 1);
  151. } else {
  152. printk(KERN_INFO "rt5631 hp unpluged\n");
  153. rt5631_headset_detect(codec, 0);
  154. snd_soc_jack_report(&pdata->jack, 0, SND_JACK_HEADPHONE);
  155. switch_set_state(&pdata->sdev, 0);
  156. }
  157. pdata->detect_flag = flag;
  158. }
  159. }
  160. static void rt5631_timer_func(unsigned long data)
  161. {
  162. struct rt5631_private_data *pdata = (struct rt5631_private_data *)data;
  163. unsigned long delay = msecs_to_jiffies(200);
  164. schedule_work(&pdata->work);
  165. mod_timer(&pdata->timer, jiffies + delay);
  166. }
  167. #endif
  168. static int rt5631_prepare(struct snd_pcm_substream *substream)
  169. {
  170. printk(KERN_DEBUG "enter %s stream: %s\n", __func__, (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? "playback" : "capture");
  171. #if HP_DET
  172. mutex_lock(&rt5631_snd_priv->lock);
  173. if (!rt5631_snd_priv->timer_en) {
  174. rt5631_start_timer(msecs_to_jiffies(100));
  175. }
  176. mutex_unlock(&rt5631_snd_priv->lock);
  177. #endif
  178. return 0;
  179. }
  180. static int rt5631_hw_params(struct snd_pcm_substream *substream,
  181. struct snd_pcm_hw_params *params)
  182. {
  183. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  184. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  185. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  186. int ret;
  187. printk(KERN_DEBUG "enter %s stream: %s rate: %d format: %d\n", __func__, (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? "playback" : "capture", params_rate(params), params_format(params));
  188. /* set codec DAI configuration */
  189. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  190. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  191. if (ret < 0) {
  192. printk(KERN_ERR "%s: set codec dai fmt failed!\n", __func__);
  193. return ret;
  194. }
  195. /* set cpu DAI configuration */
  196. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  197. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  198. if (ret < 0) {
  199. printk(KERN_ERR "%s: set cpu dai fmt failed!\n", __func__);
  200. return ret;
  201. }
  202. /* set codec DAI clock */
  203. ret = snd_soc_dai_set_sysclk(codec_dai, 0, params_rate(params) * 256, SND_SOC_CLOCK_IN);
  204. if (ret < 0) {
  205. printk(KERN_ERR "%s: set codec dai sysclk failed (rate: %d)!\n", __func__, params_rate(params));
  206. return ret;
  207. }
  208. /* set cpu DAI clock */
  209. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, params_rate(params) * 256, SND_SOC_CLOCK_OUT);
  210. if (ret < 0) {
  211. printk(KERN_ERR "%s: set cpu dai sysclk failed (rate: %d)!\n", __func__, params_rate(params));
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. static struct snd_soc_ops rt5631_soc_ops = {
  217. .prepare = rt5631_prepare,
  218. .hw_params = rt5631_hw_params,
  219. };
  220. static int rt5631_set_bias_level(struct snd_soc_card *card,
  221. enum snd_soc_bias_level level)
  222. {
  223. int ret = 0;
  224. printk(KERN_DEBUG "enter %s level: %d\n", __func__, level);
  225. if (rt5631_snd_priv->bias_level == (int)level)
  226. return 0;
  227. switch (level) {
  228. case SND_SOC_BIAS_ON:
  229. #if HP_DET
  230. mutex_lock(&rt5631_snd_priv->lock);
  231. if (!rt5631_snd_priv->timer_en) {
  232. rt5631_start_timer(msecs_to_jiffies(100));
  233. }
  234. mutex_unlock(&rt5631_snd_priv->lock);
  235. #endif
  236. break;
  237. case SND_SOC_BIAS_PREPARE:
  238. /* clock enable */
  239. if (!rt5631_snd_priv->clock_en) {
  240. rt5631_set_clock(1);
  241. }
  242. break;
  243. case SND_SOC_BIAS_OFF:
  244. case SND_SOC_BIAS_STANDBY:
  245. /* clock disable */
  246. if (rt5631_snd_priv->clock_en) {
  247. rt5631_set_clock(0);
  248. }
  249. #if HP_DET
  250. /* stop timer */
  251. mutex_lock(&rt5631_snd_priv->lock);
  252. if (rt5631_snd_priv->timer_en) {
  253. rt5631_stop_timer();
  254. }
  255. mutex_unlock(&rt5631_snd_priv->lock);
  256. #endif
  257. break;
  258. default:
  259. return ret;
  260. }
  261. rt5631_snd_priv->bias_level = (int)level;
  262. return ret;
  263. }
  264. #ifdef CONFIG_PM_SLEEP
  265. static int rt5631_suspend_pre(struct snd_soc_card *card)
  266. {
  267. printk(KERN_DEBUG "enter %s\n", __func__);
  268. #if HP_DET
  269. /* stop timer */
  270. mutex_lock(&rt5631_snd_priv->lock);
  271. if (rt5631_snd_priv->timer_en) {
  272. rt5631_stop_timer();
  273. }
  274. mutex_unlock(&rt5631_snd_priv->lock);
  275. #endif
  276. return 0;
  277. }
  278. static int rt5631_suspend_post(struct snd_soc_card *card)
  279. {
  280. printk(KERN_DEBUG "enter %s\n", __func__);
  281. return 0;
  282. }
  283. static int rt5631_resume_pre(struct snd_soc_card *card)
  284. {
  285. printk(KERN_DEBUG "enter %s\n", __func__);
  286. return 0;
  287. }
  288. static int rt5631_resume_post(struct snd_soc_card *card)
  289. {
  290. printk(KERN_DEBUG "enter %s\n", __func__);
  291. return 0;
  292. }
  293. #else
  294. #define rt5631_suspend_pre NULL
  295. #define rt5631_suspend_post NULL
  296. #define rt5631_resume_pre NULL
  297. #define rt5631_resume_post NULL
  298. #endif
  299. static const struct snd_soc_dapm_widget rt5631_dapm_widgets[] = {
  300. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  301. SND_SOC_DAPM_HP("HP", NULL),
  302. };
  303. static const struct snd_soc_dapm_route rt5631_dapm_intercon[] = {
  304. {"Ext Spk", NULL, "SPOL"},
  305. {"Ext Spk", NULL, "SPOR"},
  306. {"HP", NULL, "HPOL"},
  307. {"HP", NULL, "HPOR"},
  308. };
  309. #if HP_DET
  310. static struct snd_soc_jack_pin jack_pins[] = {
  311. {
  312. .pin = "HP",
  313. .mask = SND_JACK_HEADPHONE,
  314. }
  315. };
  316. #endif
  317. static int rt5631_codec_init(struct snd_soc_pcm_runtime *rtd)
  318. {
  319. struct snd_soc_codec *codec = rtd->codec;
  320. //struct snd_soc_dai *codec_dai = rtd->codec_dai;
  321. struct snd_soc_dapm_context *dapm = &codec->dapm;
  322. int ret = 0;
  323. printk(KERN_DEBUG "enter %s rt5631_snd_pdata: %p\n", __func__, rt5631_snd_pdata);
  324. /* Add specific widgets */
  325. snd_soc_dapm_new_controls(dapm, rt5631_dapm_widgets,
  326. ARRAY_SIZE(rt5631_dapm_widgets));
  327. /* Set up specific audio path interconnects */
  328. snd_soc_dapm_add_routes(dapm, rt5631_dapm_intercon, ARRAY_SIZE(rt5631_dapm_intercon));
  329. /* Setup spk/hp/mono output */
  330. rt5631_set_output(codec);
  331. /* Setuo mic input */
  332. rt5631_set_input(codec);
  333. /* not connected */
  334. snd_soc_dapm_nc_pin(dapm, "MONO");
  335. snd_soc_dapm_nc_pin(dapm, "AUXO2");
  336. snd_soc_dapm_nc_pin(dapm, "DMIC");
  337. snd_soc_dapm_nc_pin(dapm, "AXIL");
  338. snd_soc_dapm_nc_pin(dapm, "AXIR");
  339. /* always connected */
  340. snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  341. /* disable connected */
  342. snd_soc_dapm_disable_pin(dapm, "HP");
  343. snd_soc_dapm_sync(dapm);
  344. #if HP_DET
  345. ret = snd_soc_jack_new(codec, "hp switch", SND_JACK_HEADPHONE, &rt5631_snd_priv->jack);
  346. if (ret) {
  347. printk(KERN_WARNING "Failed to alloc resource for hp switch\n");
  348. } else {
  349. ret = snd_soc_jack_add_pins(&rt5631_snd_priv->jack, ARRAY_SIZE(jack_pins), jack_pins);
  350. if (ret) {
  351. printk(KERN_WARNING "Failed to setup hp pins\n");
  352. }
  353. }
  354. rt5631_snd_priv->data= (void*)codec;
  355. init_timer(&rt5631_snd_priv->timer);
  356. rt5631_snd_priv->timer.function = rt5631_timer_func;
  357. rt5631_snd_priv->timer.data = (unsigned long)rt5631_snd_priv;
  358. INIT_WORK(&rt5631_snd_priv->work, rt5631_work_func);
  359. mutex_init(&rt5631_snd_priv->lock);
  360. #endif
  361. return 0;
  362. }
  363. static struct snd_soc_dai_link rt5631_dai_link[] = {
  364. {
  365. .name = "RT5631",
  366. .stream_name = "RT5631 PCM",
  367. .cpu_dai_name = "aml-dai0",
  368. .codec_dai_name = "rt5631-hifi",
  369. .init = rt5631_codec_init,
  370. .platform_name = "aml-audio.0",
  371. .codec_name = "rt5631.1-001a",
  372. .ops = &rt5631_soc_ops,
  373. },
  374. };
  375. static struct snd_soc_card snd_soc_rt5631 = {
  376. .name = "AML-RT5631",
  377. .driver_name = "SOC-Audio",
  378. .dai_link = &rt5631_dai_link[0],
  379. .num_links = ARRAY_SIZE(rt5631_dai_link),
  380. .set_bias_level = rt5631_set_bias_level,
  381. #ifdef CONFIG_PM_SLEEP
  382. .suspend_pre = rt5631_suspend_pre,
  383. .suspend_post = rt5631_suspend_post,
  384. .resume_pre = rt5631_resume_pre,
  385. .resume_post = rt5631_resume_post,
  386. #endif
  387. };
  388. static struct platform_device *rt5631_snd_device = NULL;
  389. static int rt5631_audio_probe(struct platform_device *pdev)
  390. {
  391. int ret = 0;
  392. printk(KERN_DEBUG "enter %s\n", __func__);
  393. rt5631_snd_pdata = pdev->dev.platform_data;
  394. snd_BUG_ON(!rt5631_snd_pdata);
  395. rt5631_snd_priv = (struct rt5631_private_data*)kzalloc(sizeof(struct rt5631_private_data), GFP_KERNEL);
  396. if (!rt5631_snd_priv) {
  397. printk(KERN_ERR "ASoC: Platform driver data allocation failed\n");
  398. return -ENOMEM;
  399. }
  400. rt5631_snd_device = platform_device_alloc("soc-audio", -1);
  401. if (!rt5631_snd_device) {
  402. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  403. ret = -ENOMEM;
  404. goto err;
  405. }
  406. platform_set_drvdata(rt5631_snd_device, &snd_soc_rt5631);
  407. rt5631_snd_device->dev.platform_data = rt5631_snd_pdata;
  408. ret = platform_device_add(rt5631_snd_device);
  409. if (ret) {
  410. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  411. goto err_device_add;
  412. }
  413. rt5631_snd_priv->bias_level = SND_SOC_BIAS_OFF;
  414. rt5631_snd_priv->clock_en = 0;
  415. #if HP_DET
  416. rt5631_snd_priv->sdev.name = "h2w";//for report headphone to android
  417. ret = switch_dev_register(&rt5631_snd_priv->sdev);
  418. if (ret < 0){
  419. printk(KERN_ERR "ASoC: register switch dev failed\n");
  420. goto err;
  421. }
  422. #endif
  423. rt5631_dev_init();
  424. return ret;
  425. err_device_add:
  426. platform_device_put(rt5631_snd_device);
  427. err:
  428. kfree(rt5631_snd_priv);
  429. return ret;
  430. }
  431. static int rt5631_audio_remove(struct platform_device *pdev)
  432. {
  433. int ret = 0;
  434. rt5631_dev_uninit();
  435. platform_device_put(rt5631_snd_device);
  436. kfree(rt5631_snd_priv);
  437. rt5631_snd_device = NULL;
  438. rt5631_snd_priv = NULL;
  439. rt5631_snd_pdata = NULL;
  440. return ret;
  441. }
  442. static struct platform_driver aml_m6_rt5631_driver = {
  443. .probe = rt5631_audio_probe,
  444. .remove = __devexit_p(rt5631_audio_remove),
  445. .driver = {
  446. .name = "aml_rt5631_audio",
  447. .owner = THIS_MODULE,
  448. },
  449. };
  450. static int __init aml_m6_rt5631_init(void)
  451. {
  452. return platform_driver_register(&aml_m6_rt5631_driver);
  453. }
  454. static void __exit aml_m6_rt5631_exit(void)
  455. {
  456. platform_driver_unregister(&aml_m6_rt5631_driver);
  457. }
  458. module_init(aml_m6_rt5631_init);
  459. module_exit(aml_m6_rt5631_exit);
  460. /* Module information */
  461. MODULE_AUTHOR("AMLogic, Inc.");
  462. MODULE_DESCRIPTION("AML RT5631 audio driver");
  463. MODULE_LICENSE("GPL");