playpaq_wm8510.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 <mach/at32ap700x.h>
  36. #include <mach/portmux.h>
  37. #include "../codecs/wm8510.h"
  38. #include "atmel-pcm.h"
  39. #include "atmel_ssc_dai.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 struct snd_soc_dapm_route intercon[] = {
  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. };
  254. static int playpaq_wm8510_init(struct snd_soc_codec *codec)
  255. {
  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(codec, &playpaq_dapm_widgets[i]);
  262. /*
  263. * Setup audio path interconnects
  264. */
  265. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  266. /* always connected pins */
  267. snd_soc_dapm_enable_pin(codec, "Int Mic");
  268. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  269. snd_soc_dapm_sync(codec);
  270. /* Make CSB show PLL rate */
  271. snd_soc_dai_set_clkdiv(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 = &at32_ssc_dai[0],
  279. .codec_dai = &wm8510_dai,
  280. .init = playpaq_wm8510_init,
  281. .ops = &playpaq_wm8510_ops,
  282. };
  283. static struct snd_soc_machine snd_soc_machine_playpaq = {
  284. .name = "LRS_PlayPaq_WM8510",
  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. .machine = &snd_soc_machine_playpaq,
  294. .platform = &at32_soc_platform,
  295. .codec_dev = &soc_codec_dev_wm8510,
  296. .codec_data = &playpaq_wm8510_setup,
  297. };
  298. static struct platform_device *playpaq_snd_device;
  299. static int __init playpaq_asoc_init(void)
  300. {
  301. int ret = 0;
  302. struct at32_ssc_info *ssc_p = playpaq_wm8510_dai.cpu_dai->private_data;
  303. struct ssc_device *ssc = NULL;
  304. /*
  305. * Request SSC device
  306. */
  307. ssc = ssc_request(0);
  308. if (IS_ERR(ssc)) {
  309. ret = PTR_ERR(ssc);
  310. goto err_ssc;
  311. }
  312. ssc_p->ssc = ssc;
  313. /*
  314. * Configure MCLK for WM8510
  315. */
  316. _gclk0 = clk_get(NULL, "gclk0");
  317. if (IS_ERR(_gclk0)) {
  318. _gclk0 = NULL;
  319. goto err_gclk0;
  320. }
  321. _pll0 = clk_get(NULL, "pll0");
  322. if (IS_ERR(_pll0)) {
  323. _pll0 = NULL;
  324. goto err_pll0;
  325. }
  326. if (clk_set_parent(_gclk0, _pll0)) {
  327. pr_warning("snd-soc-playpaq: "
  328. "Failed to set PLL0 as parent for DAC clock\n");
  329. goto err_set_clk;
  330. }
  331. clk_set_rate(CODEC_CLK, 12000000);
  332. clk_enable(CODEC_CLK);
  333. #if defined CONFIG_AT32_ENHANCED_PORTMUX
  334. at32_select_periph(MCLK_PIN, MCLK_PERIPH, 0);
  335. #endif
  336. /*
  337. * Create and register platform device
  338. */
  339. playpaq_snd_device = platform_device_alloc("soc-audio", 0);
  340. if (playpaq_snd_device == NULL) {
  341. ret = -ENOMEM;
  342. goto err_device_alloc;
  343. }
  344. platform_set_drvdata(playpaq_snd_device, &playpaq_wm8510_snd_devdata);
  345. playpaq_wm8510_snd_devdata.dev = &playpaq_snd_device->dev;
  346. ret = platform_device_add(playpaq_snd_device);
  347. if (ret) {
  348. pr_warning("playpaq_wm8510: platform_device_add failed (%d)\n",
  349. ret);
  350. goto err_device_add;
  351. }
  352. return 0;
  353. err_device_add:
  354. if (playpaq_snd_device != NULL) {
  355. platform_device_put(playpaq_snd_device);
  356. playpaq_snd_device = NULL;
  357. }
  358. err_device_alloc:
  359. err_set_clk:
  360. if (_pll0 != NULL) {
  361. clk_put(_pll0);
  362. _pll0 = NULL;
  363. }
  364. err_pll0:
  365. if (_gclk0 != NULL) {
  366. clk_put(_gclk0);
  367. _gclk0 = NULL;
  368. }
  369. err_gclk0:
  370. ssc_free(ssc);
  371. err_ssc:
  372. return ret;
  373. }
  374. static void __exit playpaq_asoc_exit(void)
  375. {
  376. struct at32_ssc_info *ssc_p = playpaq_wm8510_dai.cpu_dai->private_data;
  377. struct ssc_device *ssc;
  378. if (ssc_p != NULL) {
  379. ssc = ssc_p->ssc;
  380. if (ssc != NULL)
  381. ssc_free(ssc);
  382. ssc_p->ssc = NULL;
  383. }
  384. if (_gclk0 != NULL) {
  385. clk_put(_gclk0);
  386. _gclk0 = NULL;
  387. }
  388. if (_pll0 != NULL) {
  389. clk_put(_pll0);
  390. _pll0 = NULL;
  391. }
  392. #if defined CONFIG_AT32_ENHANCED_PORTMUX
  393. at32_free_pin(MCLK_PIN);
  394. #endif
  395. platform_device_unregister(playpaq_snd_device);
  396. playpaq_snd_device = NULL;
  397. }
  398. module_init(playpaq_asoc_init);
  399. module_exit(playpaq_asoc_exit);
  400. MODULE_AUTHOR("Geoffrey Wossum <gwossum@acm.org>");
  401. MODULE_DESCRIPTION("ASoC machine driver for LRS PlayPaq");
  402. MODULE_LICENSE("GPL");