playpaq_wm8510.c 11 KB

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