playpaq_wm8510.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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_cpu_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_codec_dai *codec_dai = rtd->dai->codec_dai;
  109. struct snd_soc_cpu_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 = codec_dai->dai_ops.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 = cpu_dai->dai_ops.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 = cpu_dai->dai_ops.set_clkdiv(cpu_dai,
  195. AT32_SSC_CMR_DIV, cd.cmr_div);
  196. if (ret < 0) {
  197. pr_warning("playpaq_wm8510: Failed to set CPU CMR_DIV (%d)\n",
  198. ret);
  199. return ret;
  200. }
  201. ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai, AT32_SSC_TCMR_PERIOD,
  202. cd.period);
  203. if (ret < 0) {
  204. pr_warning("playpaq_wm8510: "
  205. "Failed to set CPU transmit period (%d)\n",
  206. ret);
  207. return ret;
  208. }
  209. #endif /* CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE */
  210. /*
  211. * Set CODEC clock configuration
  212. */
  213. pr_debug("playpaq_wm8510: "
  214. "pll_in = %ld, pll_out = %u, bclk = %x, mclk = %x\n",
  215. clk_get_rate(CODEC_CLK), pll_out, bclk, mclk_div);
  216. #if !defined CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE
  217. ret = codec_dai->dai_ops.set_clkdiv(codec_dai, WM8510_BCLKDIV, bclk);
  218. if (ret < 0) {
  219. pr_warning
  220. ("playpaq_wm8510: Failed to set CODEC DAI BCLKDIV (%d)\n",
  221. ret);
  222. return ret;
  223. }
  224. #endif /* CONFIG_SND_AT32_SOC_PLAYPAQ_SLAVE */
  225. ret = codec_dai->dai_ops.set_pll(codec_dai, 0,
  226. clk_get_rate(CODEC_CLK), pll_out);
  227. if (ret < 0) {
  228. pr_warning("playpaq_wm8510: Failed to set CODEC DAI PLL (%d)\n",
  229. ret);
  230. return ret;
  231. }
  232. ret = codec_dai->dai_ops.set_clkdiv(codec_dai,
  233. WM8510_MCLKDIV, mclk_div);
  234. if (ret < 0) {
  235. pr_warning("playpaq_wm8510: Failed to set CODEC MCLKDIV (%d)\n",
  236. ret);
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. static struct snd_soc_ops playpaq_wm8510_ops = {
  242. .hw_params = playpaq_wm8510_hw_params,
  243. };
  244. static const struct snd_soc_dapm_widget playpaq_dapm_widgets[] = {
  245. SND_SOC_DAPM_MIC("Int Mic", NULL),
  246. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  247. };
  248. static const char *intercon[][3] = {
  249. /* speaker connected to SPKOUT */
  250. {"Ext Spk", NULL, "SPKOUTP"},
  251. {"Ext Spk", NULL, "SPKOUTN"},
  252. {"Mic Bias", NULL, "Int Mic"},
  253. {"MICN", NULL, "Mic Bias"},
  254. {"MICP", NULL, "Mic Bias"},
  255. /* Terminator */
  256. {NULL, NULL, NULL},
  257. };
  258. static int playpaq_wm8510_init(struct snd_soc_codec *codec)
  259. {
  260. int i;
  261. /*
  262. * Add DAPM widgets
  263. */
  264. for (i = 0; i < ARRAY_SIZE(playpaq_dapm_widgets); i++)
  265. snd_soc_dapm_new_control(codec, &playpaq_dapm_widgets[i]);
  266. /*
  267. * Setup audio path interconnects
  268. */
  269. for (i = 0; intercon[i][0] != NULL; i++) {
  270. snd_soc_dapm_connect_input(codec,
  271. intercon[i][0],
  272. intercon[i][1], intercon[i][2]);
  273. }
  274. /* always connected endpoints */
  275. snd_soc_dapm_set_endpoint(codec, "Int Mic", 1);
  276. snd_soc_dapm_set_endpoint(codec, "Ext Spk", 1);
  277. snd_soc_dapm_sync_endpoints(codec);
  278. /* Make CSB show PLL rate */
  279. codec->dai->dai_ops.set_clkdiv(codec->dai, WM8510_OPCLKDIV,
  280. WM8510_OPCLKDIV_1 | 4);
  281. return 0;
  282. }
  283. static struct snd_soc_dai_link playpaq_wm8510_dai = {
  284. .name = "WM8510",
  285. .stream_name = "WM8510 PCM",
  286. .cpu_dai = &at32_ssc_dai[0],
  287. .codec_dai = &wm8510_dai,
  288. .init = playpaq_wm8510_init,
  289. .ops = &playpaq_wm8510_ops,
  290. };
  291. static struct snd_soc_machine snd_soc_machine_playpaq = {
  292. .name = "LRS_PlayPaq_WM8510",
  293. .dai_link = &playpaq_wm8510_dai,
  294. .num_links = 1,
  295. };
  296. static struct wm8510_setup_data playpaq_wm8510_setup = {
  297. .i2c_address = 0x1a,
  298. };
  299. static struct snd_soc_device playpaq_wm8510_snd_devdata = {
  300. .machine = &snd_soc_machine_playpaq,
  301. .platform = &at32_soc_platform,
  302. .codec_dev = &soc_codec_dev_wm8510,
  303. .codec_data = &playpaq_wm8510_setup,
  304. };
  305. static struct platform_device *playpaq_snd_device;
  306. static int __init playpaq_asoc_init(void)
  307. {
  308. int ret = 0;
  309. struct at32_ssc_info *ssc_p = playpaq_wm8510_dai.cpu_dai->private_data;
  310. struct ssc_device *ssc = NULL;
  311. /*
  312. * Request SSC device
  313. */
  314. ssc = ssc_request(0);
  315. if (IS_ERR(ssc)) {
  316. ret = PTR_ERR(ssc);
  317. ssc = NULL;
  318. goto err_ssc;
  319. }
  320. ssc_p->ssc = ssc;
  321. /*
  322. * Configure MCLK for WM8510
  323. */
  324. _gclk0 = clk_get(NULL, "gclk0");
  325. if (IS_ERR(_gclk0)) {
  326. _gclk0 = NULL;
  327. goto err_gclk0;
  328. }
  329. _pll0 = clk_get(NULL, "pll0");
  330. if (IS_ERR(_pll0)) {
  331. _pll0 = NULL;
  332. goto err_pll0;
  333. }
  334. if (clk_set_parent(_gclk0, _pll0)) {
  335. pr_warning("snd-soc-playpaq: "
  336. "Failed to set PLL0 as parent for DAC clock\n");
  337. goto err_set_clk;
  338. }
  339. clk_set_rate(CODEC_CLK, 12000000);
  340. clk_enable(CODEC_CLK);
  341. #if defined CONFIG_AT32_ENHANCED_PORTMUX
  342. at32_select_periph(MCLK_PIN, MCLK_PERIPH, 0);
  343. #endif
  344. /*
  345. * Create and register platform device
  346. */
  347. playpaq_snd_device = platform_device_alloc("soc-audio", 0);
  348. if (playpaq_snd_device == NULL) {
  349. ret = -ENOMEM;
  350. goto err_device_alloc;
  351. }
  352. platform_set_drvdata(playpaq_snd_device, &playpaq_wm8510_snd_devdata);
  353. playpaq_wm8510_snd_devdata.dev = &playpaq_snd_device->dev;
  354. ret = platform_device_add(playpaq_snd_device);
  355. if (ret) {
  356. pr_warning("playpaq_wm8510: platform_device_add failed (%d)\n",
  357. ret);
  358. goto err_device_add;
  359. }
  360. return 0;
  361. err_device_add:
  362. if (playpaq_snd_device != NULL) {
  363. platform_device_put(playpaq_snd_device);
  364. playpaq_snd_device = NULL;
  365. }
  366. err_device_alloc:
  367. err_set_clk:
  368. if (_pll0 != NULL) {
  369. clk_put(_pll0);
  370. _pll0 = NULL;
  371. }
  372. err_pll0:
  373. if (_gclk0 != NULL) {
  374. clk_put(_gclk0);
  375. _gclk0 = NULL;
  376. }
  377. err_gclk0:
  378. if (ssc != NULL) {
  379. ssc_free(ssc);
  380. ssc = NULL;
  381. }
  382. err_ssc:
  383. return ret;
  384. }
  385. static void __exit playpaq_asoc_exit(void)
  386. {
  387. struct at32_ssc_info *ssc_p = playpaq_wm8510_dai.cpu_dai->private_data;
  388. struct ssc_device *ssc;
  389. if (ssc_p != NULL) {
  390. ssc = ssc_p->ssc;
  391. if (ssc != NULL)
  392. ssc_free(ssc);
  393. ssc_p->ssc = NULL;
  394. }
  395. if (_gclk0 != NULL) {
  396. clk_put(_gclk0);
  397. _gclk0 = NULL;
  398. }
  399. if (_pll0 != NULL) {
  400. clk_put(_pll0);
  401. _pll0 = NULL;
  402. }
  403. #if defined CONFIG_AT32_ENHANCED_PORTMUX
  404. at32_free_pin(MCLK_PIN);
  405. #endif
  406. platform_device_unregister(playpaq_snd_device);
  407. playpaq_snd_device = NULL;
  408. }
  409. module_init(playpaq_asoc_init);
  410. module_exit(playpaq_asoc_exit);
  411. MODULE_AUTHOR("Geoffrey Wossum <gwossum@acm.org>");
  412. MODULE_DESCRIPTION("ASoC machine driver for LRS PlayPaq");
  413. MODULE_LICENSE("GPL");