playpaq_wm8510.c 11 KB

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