playpaq_wm8510.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* sound/soc/at32/playpaq_wm8510.c
  2. * ASoC machine driver for PlayPaq using WM8510 codec
  3. *
  4. * Copyright (C) 2008 Long Range Systems
  5. * Geoffrey Wossum <gwossum@acm.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This code is largely inspired by sound/soc/at91/eti_b1_wm8731.c
  12. *
  13. * NOTE: If you don't have the AT32 enhanced portmux configured (which
  14. * isn't currently in the mainline or Atmel patched kernel), you will
  15. * need to set the MCLK pin (PA30) to peripheral A in your board initialization
  16. * code. Something like:
  17. * at32_select_periph(GPIO_PIN_PA(30), GPIO_PERIPH_A, 0);
  18. *
  19. */
  20. /* #define DEBUG */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/clk.h>
  26. #include <linux/timer.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/platform_device.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include <mach/at32ap700x.h>
  34. #include <mach/portmux.h>
  35. #include "../codecs/wm8510.h"
  36. #include "atmel-pcm.h"
  37. #include "atmel_ssc_dai.h"
  38. /*-------------------------------------------------------------------------*\
  39. * constants
  40. \*-------------------------------------------------------------------------*/
  41. #define MCLK_PIN GPIO_PIN_PA(30)
  42. #define MCLK_PERIPH GPIO_PERIPH_A
  43. /*-------------------------------------------------------------------------*\
  44. * data types
  45. \*-------------------------------------------------------------------------*/
  46. /* SSC clocking data */
  47. struct ssc_clock_data {
  48. /* CMR div */
  49. unsigned int cmr_div;
  50. /* Frame period (as needed by xCMR.PERIOD) */
  51. unsigned int period;
  52. /* The SSC clock rate these settings where calculated for */
  53. unsigned long ssc_rate;
  54. };
  55. /*-------------------------------------------------------------------------*\
  56. * module data
  57. \*-------------------------------------------------------------------------*/
  58. static struct clk *_gclk0;
  59. static struct clk *_pll0;
  60. #define CODEC_CLK (_gclk0)
  61. /*-------------------------------------------------------------------------*\
  62. * Sound SOC operations
  63. \*-------------------------------------------------------------------------*/
  64. #if defined CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE
  65. static struct ssc_clock_data playpaq_wm8510_calc_ssc_clock(
  66. struct snd_pcm_hw_params *params,
  67. struct snd_soc_dai *cpu_dai)
  68. {
  69. struct at32_ssc_info *ssc_p = snd_soc_dai_get_drvdata(cpu_dai);
  70. struct ssc_device *ssc = ssc_p->ssc;
  71. struct ssc_clock_data cd;
  72. unsigned int rate, width_bits, channels;
  73. unsigned int bitrate, ssc_div;
  74. unsigned actual_rate;
  75. /*
  76. * Figure out required bitrate
  77. */
  78. rate = params_rate(params);
  79. channels = params_channels(params);
  80. width_bits = snd_pcm_format_physical_width(params_format(params));
  81. bitrate = rate * width_bits * channels;
  82. /*
  83. * Figure out required SSC divider and period for required bitrate
  84. */
  85. cd.ssc_rate = clk_get_rate(ssc->clk);
  86. ssc_div = cd.ssc_rate / bitrate;
  87. cd.cmr_div = ssc_div / 2;
  88. if (ssc_div & 1) {
  89. /* round cmr_div up */
  90. cd.cmr_div++;
  91. }
  92. cd.period = width_bits - 1;
  93. /*
  94. * Find actual rate, compare to requested rate
  95. */
  96. actual_rate = (cd.ssc_rate / (cd.cmr_div * 2)) / (2 * (cd.period + 1));
  97. pr_debug("playpaq_wm8510: Request rate = %u, actual rate = %u\n",
  98. rate, actual_rate);
  99. return cd;
  100. }
  101. #endif /* CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE */
  102. static int playpaq_wm8510_hw_params(struct snd_pcm_substream *substream,
  103. struct snd_pcm_hw_params *params)
  104. {
  105. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  106. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  107. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  108. struct at32_ssc_info *ssc_p = snd_soc_dai_get_drvdata(cpu_dai);
  109. struct ssc_device *ssc = ssc_p->ssc;
  110. unsigned int pll_out = 0, bclk = 0, mclk_div = 0;
  111. int ret;
  112. /* Due to difficulties with getting the correct clocks from the AT32's
  113. * PLL0, we're going to let the CODEC be in charge of all the clocks
  114. */
  115. #if !defined CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE
  116. const unsigned int fmt = (SND_SOC_DAIFMT_I2S |
  117. SND_SOC_DAIFMT_NB_NF |
  118. SND_SOC_DAIFMT_CBM_CFM);
  119. #else
  120. struct ssc_clock_data cd;
  121. const unsigned int fmt = (SND_SOC_DAIFMT_I2S |
  122. SND_SOC_DAIFMT_NB_NF |
  123. SND_SOC_DAIFMT_CBS_CFS);
  124. #endif
  125. if (ssc == NULL) {
  126. pr_warning("playpaq_wm8510_hw_params: ssc is NULL!\n");
  127. return -EINVAL;
  128. }
  129. /*
  130. * Figure out PLL and BCLK dividers for WM8510
  131. */
  132. switch (params_rate(params)) {
  133. case 48000:
  134. pll_out = 24576000;
  135. mclk_div = WM8510_MCLKDIV_2;
  136. bclk = WM8510_BCLKDIV_8;
  137. break;
  138. case 44100:
  139. pll_out = 22579200;
  140. mclk_div = WM8510_MCLKDIV_2;
  141. bclk = WM8510_BCLKDIV_8;
  142. break;
  143. case 22050:
  144. pll_out = 22579200;
  145. mclk_div = WM8510_MCLKDIV_4;
  146. bclk = WM8510_BCLKDIV_8;
  147. break;
  148. case 16000:
  149. pll_out = 24576000;
  150. mclk_div = WM8510_MCLKDIV_6;
  151. bclk = WM8510_BCLKDIV_8;
  152. break;
  153. case 11025:
  154. pll_out = 22579200;
  155. mclk_div = WM8510_MCLKDIV_8;
  156. bclk = WM8510_BCLKDIV_8;
  157. break;
  158. case 8000:
  159. pll_out = 24576000;
  160. mclk_div = WM8510_MCLKDIV_12;
  161. bclk = WM8510_BCLKDIV_8;
  162. break;
  163. default:
  164. pr_warning("playpaq_wm8510: Unsupported sample rate %d\n",
  165. params_rate(params));
  166. return -EINVAL;
  167. }
  168. /*
  169. * set CPU and CODEC DAI configuration
  170. */
  171. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  172. if (ret < 0) {
  173. pr_warning("playpaq_wm8510: "
  174. "Failed to set CODEC DAI format (%d)\n",
  175. ret);
  176. return ret;
  177. }
  178. ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  179. if (ret < 0) {
  180. pr_warning("playpaq_wm8510: "
  181. "Failed to set CPU DAI format (%d)\n",
  182. ret);
  183. return ret;
  184. }
  185. /*
  186. * Set CPU clock configuration
  187. */
  188. #if defined CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE
  189. cd = playpaq_wm8510_calc_ssc_clock(params, cpu_dai);
  190. pr_debug("playpaq_wm8510: cmr_div = %d, period = %d\n",
  191. cd.cmr_div, cd.period);
  192. ret = snd_soc_dai_set_clkdiv(cpu_dai, AT32_SSC_CMR_DIV, cd.cmr_div);
  193. if (ret < 0) {
  194. pr_warning("playpaq_wm8510: Failed to set CPU CMR_DIV (%d)\n",
  195. ret);
  196. return ret;
  197. }
  198. ret = snd_soc_dai_set_clkdiv(cpu_dai, AT32_SSC_TCMR_PERIOD,
  199. cd.period);
  200. if (ret < 0) {
  201. pr_warning("playpaq_wm8510: "
  202. "Failed to set CPU transmit period (%d)\n",
  203. ret);
  204. return ret;
  205. }
  206. #endif /* CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE */
  207. /*
  208. * Set CODEC clock configuration
  209. */
  210. pr_debug("playpaq_wm8510: "
  211. "pll_in = %ld, pll_out = %u, bclk = %x, mclk = %x\n",
  212. clk_get_rate(CODEC_CLK), pll_out, bclk, mclk_div);
  213. #if !defined CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE
  214. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8510_BCLKDIV, bclk);
  215. if (ret < 0) {
  216. pr_warning
  217. ("playpaq_wm8510: Failed to set CODEC DAI BCLKDIV (%d)\n",
  218. ret);
  219. return ret;
  220. }
  221. #endif /* CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE */
  222. ret = snd_soc_dai_set_pll(codec_dai, 0, 0,
  223. clk_get_rate(CODEC_CLK), pll_out);
  224. if (ret < 0) {
  225. pr_warning("playpaq_wm8510: Failed to set CODEC DAI PLL (%d)\n",
  226. ret);
  227. return ret;
  228. }
  229. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8510_MCLKDIV, mclk_div);
  230. if (ret < 0) {
  231. pr_warning("playpaq_wm8510: Failed to set CODEC MCLKDIV (%d)\n",
  232. ret);
  233. return ret;
  234. }
  235. return 0;
  236. }
  237. static struct snd_soc_ops playpaq_wm8510_ops = {
  238. .hw_params = playpaq_wm8510_hw_params,
  239. };
  240. static const struct snd_soc_dapm_widget playpaq_dapm_widgets[] = {
  241. SND_SOC_DAPM_MIC("Int Mic", NULL),
  242. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  243. };
  244. static const struct snd_soc_dapm_route intercon[] = {
  245. /* speaker connected to SPKOUT */
  246. {"Ext Spk", NULL, "SPKOUTP"},
  247. {"Ext Spk", NULL, "SPKOUTN"},
  248. {"Mic Bias", NULL, "Int Mic"},
  249. {"MICN", NULL, "Mic Bias"},
  250. {"MICP", NULL, "Mic Bias"},
  251. };
  252. static int playpaq_wm8510_init(struct snd_soc_pcm_runtime *rtd)
  253. {
  254. struct snd_soc_codec *codec = rtd->codec;
  255. struct snd_soc_dapm_context *dapm = &codec->dapm;
  256. int i;
  257. /*
  258. * Add DAPM widgets
  259. */
  260. for (i = 0; i < ARRAY_SIZE(playpaq_dapm_widgets); i++)
  261. snd_soc_dapm_new_control(dapm, &playpaq_dapm_widgets[i]);
  262. /*
  263. * Setup audio path interconnects
  264. */
  265. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  266. /* always connected pins */
  267. snd_soc_dapm_enable_pin(dapm, "Int Mic");
  268. snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  269. snd_soc_dapm_sync(dapm);
  270. /* Make CSB show PLL rate */
  271. snd_soc_dai_set_clkdiv(rtd->codec_dai, WM8510_OPCLKDIV,
  272. WM8510_OPCLKDIV_1 | 4);
  273. return 0;
  274. }
  275. static struct snd_soc_dai_link playpaq_wm8510_dai = {
  276. .name = "WM8510",
  277. .stream_name = "WM8510 PCM",
  278. .cpu_dai_name= "atmel-ssc-dai.0",
  279. .platform_name = "atmel-pcm-audio",
  280. .codec_name = "wm8510-codec.0-0x1a",
  281. .codec_dai_name = "wm8510-hifi",
  282. .init = playpaq_wm8510_init,
  283. .ops = &playpaq_wm8510_ops,
  284. };
  285. static struct snd_soc_card snd_soc_playpaq = {
  286. .name = "LRS_PlayPaq_WM8510",
  287. .dai_link = &playpaq_wm8510_dai,
  288. .num_links = 1,
  289. };
  290. static struct platform_device *playpaq_snd_device;
  291. static int __init playpaq_asoc_init(void)
  292. {
  293. int ret = 0;
  294. /*
  295. * Configure MCLK for WM8510
  296. */
  297. _gclk0 = clk_get(NULL, "gclk0");
  298. if (IS_ERR(_gclk0)) {
  299. _gclk0 = NULL;
  300. goto err_gclk0;
  301. }
  302. _pll0 = clk_get(NULL, "pll0");
  303. if (IS_ERR(_pll0)) {
  304. _pll0 = NULL;
  305. goto err_pll0;
  306. }
  307. if (clk_set_parent(_gclk0, _pll0)) {
  308. pr_warning("snd-soc-playpaq: "
  309. "Failed to set PLL0 as parent for DAC clock\n");
  310. goto err_set_clk;
  311. }
  312. clk_set_rate(CODEC_CLK, 12000000);
  313. clk_enable(CODEC_CLK);
  314. #if defined CONFIG_AT32_ENHANCED_PORTMUX
  315. at32_select_periph(MCLK_PIN, MCLK_PERIPH, 0);
  316. #endif
  317. /*
  318. * Create and register platform device
  319. */
  320. playpaq_snd_device = platform_device_alloc("soc-audio", 0);
  321. if (playpaq_snd_device == NULL) {
  322. ret = -ENOMEM;
  323. goto err_device_alloc;
  324. }
  325. platform_set_drvdata(playpaq_snd_device, &snd_soc_playpaq);
  326. ret = platform_device_add(playpaq_snd_device);
  327. if (ret) {
  328. pr_warning("playpaq_wm8510: platform_device_add failed (%d)\n",
  329. ret);
  330. goto err_device_add;
  331. }
  332. return 0;
  333. err_device_add:
  334. if (playpaq_snd_device != NULL) {
  335. platform_device_put(playpaq_snd_device);
  336. playpaq_snd_device = NULL;
  337. }
  338. err_device_alloc:
  339. err_set_clk:
  340. if (_pll0 != NULL) {
  341. clk_put(_pll0);
  342. _pll0 = NULL;
  343. }
  344. err_pll0:
  345. if (_gclk0 != NULL) {
  346. clk_put(_gclk0);
  347. _gclk0 = NULL;
  348. }
  349. return ret;
  350. }
  351. static void __exit playpaq_asoc_exit(void)
  352. {
  353. if (_gclk0 != NULL) {
  354. clk_put(_gclk0);
  355. _gclk0 = NULL;
  356. }
  357. if (_pll0 != NULL) {
  358. clk_put(_pll0);
  359. _pll0 = NULL;
  360. }
  361. #if defined CONFIG_AT32_ENHANCED_PORTMUX
  362. at32_free_pin(MCLK_PIN);
  363. #endif
  364. platform_device_unregister(playpaq_snd_device);
  365. playpaq_snd_device = NULL;
  366. }
  367. module_init(playpaq_asoc_init);
  368. module_exit(playpaq_asoc_exit);
  369. MODULE_AUTHOR("Geoffrey Wossum <gwossum@acm.org>");
  370. MODULE_DESCRIPTION("ASoC machine driver for LRS PlayPaq");
  371. MODULE_LICENSE("GPL");