pxa-ssp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. #define DEBUG
  2. /*
  3. * pxa-ssp.c -- ALSA Soc Audio Layer
  4. *
  5. * Copyright 2005,2008 Wolfson Microelectronics PLC.
  6. * Author: Liam Girdwood
  7. * Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * TODO:
  15. * o Test network mode for > 16bit sample size
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/io.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/initval.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/pxa2xx-lib.h>
  28. #include <mach/hardware.h>
  29. #include <mach/pxa-regs.h>
  30. #include <mach/regs-ssp.h>
  31. #include <mach/audio.h>
  32. #include <mach/ssp.h>
  33. #include "pxa2xx-pcm.h"
  34. #include "pxa-ssp.h"
  35. /*
  36. * SSP audio private data
  37. */
  38. struct ssp_priv {
  39. struct ssp_dev dev;
  40. unsigned int sysclk;
  41. int dai_fmt;
  42. #ifdef CONFIG_PM
  43. struct ssp_state state;
  44. #endif
  45. };
  46. #define PXA2xx_SSP1_BASE 0x41000000
  47. #define PXA27x_SSP2_BASE 0x41700000
  48. #define PXA27x_SSP3_BASE 0x41900000
  49. #define PXA3xx_SSP4_BASE 0x41a00000
  50. static struct pxa2xx_pcm_dma_params pxa_ssp1_pcm_mono_out = {
  51. .name = "SSP1 PCM Mono out",
  52. .dev_addr = PXA2xx_SSP1_BASE + SSDR,
  53. .drcmr = &DRCMR(14),
  54. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  55. DCMD_BURST16 | DCMD_WIDTH2,
  56. };
  57. static struct pxa2xx_pcm_dma_params pxa_ssp1_pcm_mono_in = {
  58. .name = "SSP1 PCM Mono in",
  59. .dev_addr = PXA2xx_SSP1_BASE + SSDR,
  60. .drcmr = &DRCMR(13),
  61. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  62. DCMD_BURST16 | DCMD_WIDTH2,
  63. };
  64. static struct pxa2xx_pcm_dma_params pxa_ssp1_pcm_stereo_out = {
  65. .name = "SSP1 PCM Stereo out",
  66. .dev_addr = PXA2xx_SSP1_BASE + SSDR,
  67. .drcmr = &DRCMR(14),
  68. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  69. DCMD_BURST16 | DCMD_WIDTH4,
  70. };
  71. static struct pxa2xx_pcm_dma_params pxa_ssp1_pcm_stereo_in = {
  72. .name = "SSP1 PCM Stereo in",
  73. .dev_addr = PXA2xx_SSP1_BASE + SSDR,
  74. .drcmr = &DRCMR(13),
  75. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  76. DCMD_BURST16 | DCMD_WIDTH4,
  77. };
  78. static struct pxa2xx_pcm_dma_params pxa_ssp2_pcm_mono_out = {
  79. .name = "SSP2 PCM Mono out",
  80. .dev_addr = PXA27x_SSP2_BASE + SSDR,
  81. .drcmr = &DRCMR(16),
  82. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  83. DCMD_BURST16 | DCMD_WIDTH2,
  84. };
  85. static struct pxa2xx_pcm_dma_params pxa_ssp2_pcm_mono_in = {
  86. .name = "SSP2 PCM Mono in",
  87. .dev_addr = PXA27x_SSP2_BASE + SSDR,
  88. .drcmr = &DRCMR(15),
  89. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  90. DCMD_BURST16 | DCMD_WIDTH2,
  91. };
  92. static struct pxa2xx_pcm_dma_params pxa_ssp2_pcm_stereo_out = {
  93. .name = "SSP2 PCM Stereo out",
  94. .dev_addr = PXA27x_SSP2_BASE + SSDR,
  95. .drcmr = &DRCMR(16),
  96. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  97. DCMD_BURST16 | DCMD_WIDTH4,
  98. };
  99. static struct pxa2xx_pcm_dma_params pxa_ssp2_pcm_stereo_in = {
  100. .name = "SSP2 PCM Stereo in",
  101. .dev_addr = PXA27x_SSP2_BASE + SSDR,
  102. .drcmr = &DRCMR(15),
  103. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  104. DCMD_BURST16 | DCMD_WIDTH4,
  105. };
  106. static struct pxa2xx_pcm_dma_params pxa_ssp3_pcm_mono_out = {
  107. .name = "SSP3 PCM Mono out",
  108. .dev_addr = PXA27x_SSP3_BASE + SSDR,
  109. .drcmr = &DRCMR(67),
  110. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  111. DCMD_BURST16 | DCMD_WIDTH2,
  112. };
  113. static struct pxa2xx_pcm_dma_params pxa_ssp3_pcm_mono_in = {
  114. .name = "SSP3 PCM Mono in",
  115. .dev_addr = PXA27x_SSP3_BASE + SSDR,
  116. .drcmr = &DRCMR(66),
  117. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  118. DCMD_BURST16 | DCMD_WIDTH2,
  119. };
  120. static struct pxa2xx_pcm_dma_params pxa_ssp3_pcm_stereo_out = {
  121. .name = "SSP3 PCM Stereo out",
  122. .dev_addr = PXA27x_SSP3_BASE + SSDR,
  123. .drcmr = &DRCMR(67),
  124. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  125. DCMD_BURST16 | DCMD_WIDTH4,
  126. };
  127. static struct pxa2xx_pcm_dma_params pxa_ssp3_pcm_stereo_in = {
  128. .name = "SSP3 PCM Stereo in",
  129. .dev_addr = PXA27x_SSP3_BASE + SSDR,
  130. .drcmr = &DRCMR(66),
  131. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  132. DCMD_BURST16 | DCMD_WIDTH4,
  133. };
  134. static struct pxa2xx_pcm_dma_params pxa_ssp4_pcm_mono_out = {
  135. .name = "SSP4 PCM Mono out",
  136. .dev_addr = PXA3xx_SSP4_BASE + SSDR,
  137. .drcmr = &DRCMR(67),
  138. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  139. DCMD_BURST16 | DCMD_WIDTH2,
  140. };
  141. static struct pxa2xx_pcm_dma_params pxa_ssp4_pcm_mono_in = {
  142. .name = "SSP4 PCM Mono in",
  143. .dev_addr = PXA3xx_SSP4_BASE + SSDR,
  144. .drcmr = &DRCMR(66),
  145. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  146. DCMD_BURST16 | DCMD_WIDTH2,
  147. };
  148. static struct pxa2xx_pcm_dma_params pxa_ssp4_pcm_stereo_out = {
  149. .name = "SSP4 PCM Stereo out",
  150. .dev_addr = PXA3xx_SSP4_BASE + SSDR,
  151. .drcmr = &DRCMR(67),
  152. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  153. DCMD_BURST16 | DCMD_WIDTH4,
  154. };
  155. static struct pxa2xx_pcm_dma_params pxa_ssp4_pcm_stereo_in = {
  156. .name = "SSP4 PCM Stereo in",
  157. .dev_addr = PXA3xx_SSP4_BASE + SSDR,
  158. .drcmr = &DRCMR(66),
  159. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  160. DCMD_BURST16 | DCMD_WIDTH4,
  161. };
  162. static void dump_registers(struct ssp_device *ssp)
  163. {
  164. dev_dbg(&ssp->pdev->dev, "SSCR0 0x%08x SSCR1 0x%08x SSTO 0x%08x\n",
  165. ssp_read_reg(ssp, SSCR0), ssp_read_reg(ssp, SSCR1),
  166. ssp_read_reg(ssp, SSTO));
  167. dev_dbg(&ssp->pdev->dev, "SSPSP 0x%08x SSSR 0x%08x SSACD 0x%08x\n",
  168. ssp_read_reg(ssp, SSPSP), ssp_read_reg(ssp, SSSR),
  169. ssp_read_reg(ssp, SSACD));
  170. }
  171. static struct pxa2xx_pcm_dma_params *ssp_dma_params[4][4] = {
  172. {
  173. &pxa_ssp1_pcm_mono_out, &pxa_ssp1_pcm_mono_in,
  174. &pxa_ssp1_pcm_stereo_out, &pxa_ssp1_pcm_stereo_in,
  175. },
  176. {
  177. &pxa_ssp2_pcm_mono_out, &pxa_ssp2_pcm_mono_in,
  178. &pxa_ssp2_pcm_stereo_out, &pxa_ssp2_pcm_stereo_in,
  179. },
  180. {
  181. &pxa_ssp3_pcm_mono_out, &pxa_ssp3_pcm_mono_in,
  182. &pxa_ssp3_pcm_stereo_out, &pxa_ssp3_pcm_stereo_in,
  183. },
  184. {
  185. &pxa_ssp4_pcm_mono_out, &pxa_ssp4_pcm_mono_in,
  186. &pxa_ssp4_pcm_stereo_out, &pxa_ssp4_pcm_stereo_in,
  187. },
  188. };
  189. static int pxa_ssp_startup(struct snd_pcm_substream *substream,
  190. struct snd_soc_dai *dai)
  191. {
  192. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  193. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  194. struct ssp_priv *priv = cpu_dai->private_data;
  195. int ret = 0;
  196. if (!cpu_dai->active) {
  197. ret = ssp_init(&priv->dev, cpu_dai->id + 1, SSP_NO_IRQ);
  198. if (ret < 0)
  199. return ret;
  200. ssp_disable(&priv->dev);
  201. }
  202. return ret;
  203. }
  204. static void pxa_ssp_shutdown(struct snd_pcm_substream *substream,
  205. struct snd_soc_dai *dai)
  206. {
  207. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  208. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  209. struct ssp_priv *priv = cpu_dai->private_data;
  210. if (!cpu_dai->active) {
  211. ssp_disable(&priv->dev);
  212. ssp_exit(&priv->dev);
  213. }
  214. }
  215. #ifdef CONFIG_PM
  216. static int pxa_ssp_suspend(struct platform_device *pdev,
  217. struct snd_soc_dai *cpu_dai)
  218. {
  219. struct ssp_priv *priv = cpu_dai->private_data;
  220. if (!cpu_dai->active)
  221. return 0;
  222. ssp_save_state(&priv->dev, &priv->state);
  223. clk_disable(priv->dev.ssp->clk);
  224. return 0;
  225. }
  226. static int pxa_ssp_resume(struct platform_device *pdev,
  227. struct snd_soc_dai *cpu_dai)
  228. {
  229. struct ssp_priv *priv = cpu_dai->private_data;
  230. if (!cpu_dai->active)
  231. return 0;
  232. clk_enable(priv->dev.ssp->clk);
  233. ssp_restore_state(&priv->dev, &priv->state);
  234. ssp_enable(&priv->dev);
  235. return 0;
  236. }
  237. #else
  238. #define pxa_ssp_suspend NULL
  239. #define pxa_ssp_resume NULL
  240. #endif
  241. /**
  242. * ssp_set_clkdiv - set SSP clock divider
  243. * @div: serial clock rate divider
  244. */
  245. static void ssp_set_scr(struct ssp_dev *dev, u32 div)
  246. {
  247. struct ssp_device *ssp = dev->ssp;
  248. u32 sscr0 = ssp_read_reg(dev->ssp, SSCR0) & ~SSCR0_SCR;
  249. ssp_write_reg(ssp, SSCR0, (sscr0 | SSCR0_SerClkDiv(div)));
  250. }
  251. /*
  252. * Set the SSP ports SYSCLK.
  253. */
  254. static int pxa_ssp_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  255. int clk_id, unsigned int freq, int dir)
  256. {
  257. struct ssp_priv *priv = cpu_dai->private_data;
  258. struct ssp_device *ssp = priv->dev.ssp;
  259. int val;
  260. u32 sscr0 = ssp_read_reg(ssp, SSCR0) &
  261. ~(SSCR0_ECS | SSCR0_NCS | SSCR0_MOD | SSCR0_ADC);
  262. dev_dbg(&ssp->pdev->dev,
  263. "pxa_ssp_set_dai_sysclk id: %d, clk_id %d, freq %d\n",
  264. cpu_dai->id, clk_id, freq);
  265. switch (clk_id) {
  266. case PXA_SSP_CLK_NET_PLL:
  267. sscr0 |= SSCR0_MOD;
  268. break;
  269. case PXA_SSP_CLK_PLL:
  270. /* Internal PLL is fixed */
  271. if (cpu_is_pxa25x())
  272. priv->sysclk = 1843200;
  273. else
  274. priv->sysclk = 13000000;
  275. break;
  276. case PXA_SSP_CLK_EXT:
  277. priv->sysclk = freq;
  278. sscr0 |= SSCR0_ECS;
  279. break;
  280. case PXA_SSP_CLK_NET:
  281. priv->sysclk = freq;
  282. sscr0 |= SSCR0_NCS | SSCR0_MOD;
  283. break;
  284. case PXA_SSP_CLK_AUDIO:
  285. priv->sysclk = 0;
  286. ssp_set_scr(&priv->dev, 1);
  287. sscr0 |= SSCR0_ADC;
  288. break;
  289. default:
  290. return -ENODEV;
  291. }
  292. /* The SSP clock must be disabled when changing SSP clock mode
  293. * on PXA2xx. On PXA3xx it must be enabled when doing so. */
  294. if (!cpu_is_pxa3xx())
  295. clk_disable(priv->dev.ssp->clk);
  296. val = ssp_read_reg(ssp, SSCR0) | sscr0;
  297. ssp_write_reg(ssp, SSCR0, val);
  298. if (!cpu_is_pxa3xx())
  299. clk_enable(priv->dev.ssp->clk);
  300. return 0;
  301. }
  302. /*
  303. * Set the SSP clock dividers.
  304. */
  305. static int pxa_ssp_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
  306. int div_id, int div)
  307. {
  308. struct ssp_priv *priv = cpu_dai->private_data;
  309. struct ssp_device *ssp = priv->dev.ssp;
  310. int val;
  311. switch (div_id) {
  312. case PXA_SSP_AUDIO_DIV_ACDS:
  313. val = (ssp_read_reg(ssp, SSACD) & ~0x7) | SSACD_ACDS(div);
  314. ssp_write_reg(ssp, SSACD, val);
  315. break;
  316. case PXA_SSP_AUDIO_DIV_SCDB:
  317. val = ssp_read_reg(ssp, SSACD);
  318. val &= ~SSACD_SCDB;
  319. #if defined(CONFIG_PXA3xx)
  320. if (cpu_is_pxa3xx())
  321. val &= ~SSACD_SCDX8;
  322. #endif
  323. switch (div) {
  324. case PXA_SSP_CLK_SCDB_1:
  325. val |= SSACD_SCDB;
  326. break;
  327. case PXA_SSP_CLK_SCDB_4:
  328. break;
  329. #if defined(CONFIG_PXA3xx)
  330. case PXA_SSP_CLK_SCDB_8:
  331. if (cpu_is_pxa3xx())
  332. val |= SSACD_SCDX8;
  333. else
  334. return -EINVAL;
  335. break;
  336. #endif
  337. default:
  338. return -EINVAL;
  339. }
  340. ssp_write_reg(ssp, SSACD, val);
  341. break;
  342. case PXA_SSP_DIV_SCR:
  343. ssp_set_scr(&priv->dev, div);
  344. break;
  345. default:
  346. return -ENODEV;
  347. }
  348. return 0;
  349. }
  350. /*
  351. * Configure the PLL frequency pxa27x and (afaik - pxa320 only)
  352. */
  353. static int pxa_ssp_set_dai_pll(struct snd_soc_dai *cpu_dai,
  354. int pll_id, unsigned int freq_in, unsigned int freq_out)
  355. {
  356. struct ssp_priv *priv = cpu_dai->private_data;
  357. struct ssp_device *ssp = priv->dev.ssp;
  358. u32 ssacd = ssp_read_reg(ssp, SSACD) & ~0x70;
  359. #if defined(CONFIG_PXA3xx)
  360. if (cpu_is_pxa3xx())
  361. ssp_write_reg(ssp, SSACDD, 0);
  362. #endif
  363. switch (freq_out) {
  364. case 5622000:
  365. break;
  366. case 11345000:
  367. ssacd |= (0x1 << 4);
  368. break;
  369. case 12235000:
  370. ssacd |= (0x2 << 4);
  371. break;
  372. case 14857000:
  373. ssacd |= (0x3 << 4);
  374. break;
  375. case 32842000:
  376. ssacd |= (0x4 << 4);
  377. break;
  378. case 48000000:
  379. ssacd |= (0x5 << 4);
  380. break;
  381. case 0:
  382. /* Disable */
  383. break;
  384. default:
  385. #ifdef CONFIG_PXA3xx
  386. /* PXA3xx has a clock ditherer which can be used to generate
  387. * a wider range of frequencies - calculate a value for it.
  388. */
  389. if (cpu_is_pxa3xx()) {
  390. u32 val;
  391. u64 tmp = 19968;
  392. tmp *= 1000000;
  393. do_div(tmp, freq_out);
  394. val = tmp;
  395. val = (val << 16) | 64;;
  396. ssp_write_reg(ssp, SSACDD, val);
  397. ssacd |= (0x6 << 4);
  398. dev_dbg(&ssp->pdev->dev,
  399. "Using SSACDD %x to supply %dHz\n",
  400. val, freq_out);
  401. break;
  402. }
  403. #endif
  404. return -EINVAL;
  405. }
  406. ssp_write_reg(ssp, SSACD, ssacd);
  407. return 0;
  408. }
  409. /*
  410. * Set the active slots in TDM/Network mode
  411. */
  412. static int pxa_ssp_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
  413. unsigned int mask, int slots)
  414. {
  415. struct ssp_priv *priv = cpu_dai->private_data;
  416. struct ssp_device *ssp = priv->dev.ssp;
  417. u32 sscr0;
  418. sscr0 = ssp_read_reg(ssp, SSCR0) & ~SSCR0_SlotsPerFrm(7);
  419. /* set number of active slots */
  420. sscr0 |= SSCR0_SlotsPerFrm(slots);
  421. ssp_write_reg(ssp, SSCR0, sscr0);
  422. /* set active slot mask */
  423. ssp_write_reg(ssp, SSTSA, mask);
  424. ssp_write_reg(ssp, SSRSA, mask);
  425. return 0;
  426. }
  427. /*
  428. * Tristate the SSP DAI lines
  429. */
  430. static int pxa_ssp_set_dai_tristate(struct snd_soc_dai *cpu_dai,
  431. int tristate)
  432. {
  433. struct ssp_priv *priv = cpu_dai->private_data;
  434. struct ssp_device *ssp = priv->dev.ssp;
  435. u32 sscr1;
  436. sscr1 = ssp_read_reg(ssp, SSCR1);
  437. if (tristate)
  438. sscr1 &= ~SSCR1_TTE;
  439. else
  440. sscr1 |= SSCR1_TTE;
  441. ssp_write_reg(ssp, SSCR1, sscr1);
  442. return 0;
  443. }
  444. /*
  445. * Set up the SSP DAI format.
  446. * The SSP Port must be inactive before calling this function as the
  447. * physical interface format is changed.
  448. */
  449. static int pxa_ssp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  450. unsigned int fmt)
  451. {
  452. struct ssp_priv *priv = cpu_dai->private_data;
  453. struct ssp_device *ssp = priv->dev.ssp;
  454. u32 sscr0;
  455. u32 sscr1;
  456. u32 sspsp;
  457. /* reset port settings */
  458. sscr0 = ssp_read_reg(ssp, SSCR0) &
  459. (SSCR0_ECS | SSCR0_NCS | SSCR0_MOD | SSCR0_ADC);
  460. sscr1 = SSCR1_RxTresh(8) | SSCR1_TxTresh(7);
  461. sspsp = 0;
  462. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  463. case SND_SOC_DAIFMT_CBM_CFM:
  464. sscr1 |= SSCR1_SCLKDIR | SSCR1_SFRMDIR;
  465. break;
  466. case SND_SOC_DAIFMT_CBM_CFS:
  467. sscr1 |= SSCR1_SCLKDIR;
  468. break;
  469. case SND_SOC_DAIFMT_CBS_CFS:
  470. break;
  471. default:
  472. return -EINVAL;
  473. }
  474. ssp_write_reg(ssp, SSCR0, sscr0);
  475. ssp_write_reg(ssp, SSCR1, sscr1);
  476. ssp_write_reg(ssp, SSPSP, sspsp);
  477. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  478. case SND_SOC_DAIFMT_I2S:
  479. sscr0 |= SSCR0_MOD | SSCR0_PSP;
  480. sscr1 |= SSCR1_RWOT | SSCR1_TRAIL;
  481. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  482. case SND_SOC_DAIFMT_NB_NF:
  483. sspsp |= SSPSP_FSRT;
  484. break;
  485. case SND_SOC_DAIFMT_NB_IF:
  486. sspsp |= SSPSP_SFRMP | SSPSP_FSRT;
  487. break;
  488. case SND_SOC_DAIFMT_IB_IF:
  489. sspsp |= SSPSP_SFRMP;
  490. break;
  491. default:
  492. return -EINVAL;
  493. }
  494. break;
  495. case SND_SOC_DAIFMT_DSP_A:
  496. sspsp |= SSPSP_FSRT;
  497. case SND_SOC_DAIFMT_DSP_B:
  498. sscr0 |= SSCR0_MOD | SSCR0_PSP;
  499. sscr1 |= SSCR1_TRAIL | SSCR1_RWOT;
  500. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  501. case SND_SOC_DAIFMT_NB_NF:
  502. sspsp |= SSPSP_SFRMP;
  503. break;
  504. case SND_SOC_DAIFMT_IB_IF:
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. break;
  510. default:
  511. return -EINVAL;
  512. }
  513. ssp_write_reg(ssp, SSCR0, sscr0);
  514. ssp_write_reg(ssp, SSCR1, sscr1);
  515. ssp_write_reg(ssp, SSPSP, sspsp);
  516. dump_registers(ssp);
  517. /* Since we are configuring the timings for the format by hand
  518. * we have to defer some things until hw_params() where we
  519. * know parameters like the sample size.
  520. */
  521. priv->dai_fmt = fmt;
  522. return 0;
  523. }
  524. /*
  525. * Set the SSP audio DMA parameters and sample size.
  526. * Can be called multiple times by oss emulation.
  527. */
  528. static int pxa_ssp_hw_params(struct snd_pcm_substream *substream,
  529. struct snd_pcm_hw_params *params,
  530. struct snd_soc_dai *dai)
  531. {
  532. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  533. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  534. struct ssp_priv *priv = cpu_dai->private_data;
  535. struct ssp_device *ssp = priv->dev.ssp;
  536. int dma = 0, chn = params_channels(params);
  537. u32 sscr0;
  538. u32 sspsp;
  539. int width = snd_pcm_format_physical_width(params_format(params));
  540. /* select correct DMA params */
  541. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  542. dma = 1; /* capture DMA offset is 1,3 */
  543. if (chn == 2)
  544. dma += 2; /* stereo DMA offset is 2, mono is 0 */
  545. cpu_dai->dma_data = ssp_dma_params[cpu_dai->id][dma];
  546. dev_dbg(&ssp->pdev->dev, "pxa_ssp_hw_params: dma %d\n", dma);
  547. /* we can only change the settings if the port is not in use */
  548. if (ssp_read_reg(ssp, SSCR0) & SSCR0_SSE)
  549. return 0;
  550. /* clear selected SSP bits */
  551. sscr0 = ssp_read_reg(ssp, SSCR0) & ~(SSCR0_DSS | SSCR0_EDSS);
  552. ssp_write_reg(ssp, SSCR0, sscr0);
  553. /* bit size */
  554. sscr0 = ssp_read_reg(ssp, SSCR0);
  555. switch (params_format(params)) {
  556. case SNDRV_PCM_FORMAT_S16_LE:
  557. #ifdef CONFIG_PXA3xx
  558. if (cpu_is_pxa3xx())
  559. sscr0 |= SSCR0_FPCKE;
  560. #endif
  561. sscr0 |= SSCR0_DataSize(16);
  562. if (params_channels(params) > 1)
  563. sscr0 |= SSCR0_EDSS;
  564. break;
  565. case SNDRV_PCM_FORMAT_S24_LE:
  566. sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(8));
  567. /* we must be in network mode (2 slots) for 24 bit stereo */
  568. break;
  569. case SNDRV_PCM_FORMAT_S32_LE:
  570. sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(16));
  571. /* we must be in network mode (2 slots) for 32 bit stereo */
  572. break;
  573. }
  574. ssp_write_reg(ssp, SSCR0, sscr0);
  575. switch (priv->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  576. case SND_SOC_DAIFMT_I2S:
  577. /* Cleared when the DAI format is set */
  578. sspsp = ssp_read_reg(ssp, SSPSP) | SSPSP_SFRMWDTH(width);
  579. ssp_write_reg(ssp, SSPSP, sspsp);
  580. break;
  581. default:
  582. break;
  583. }
  584. /* We always use a network mode so we always require TDM slots
  585. * - complain loudly and fail if they've not been set up yet.
  586. */
  587. if (!(ssp_read_reg(ssp, SSTSA) & 0xf)) {
  588. dev_err(&ssp->pdev->dev, "No TDM timeslot configured\n");
  589. return -EINVAL;
  590. }
  591. dump_registers(ssp);
  592. return 0;
  593. }
  594. static int pxa_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
  595. struct snd_soc_dai *dai)
  596. {
  597. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  598. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  599. int ret = 0;
  600. struct ssp_priv *priv = cpu_dai->private_data;
  601. struct ssp_device *ssp = priv->dev.ssp;
  602. int val;
  603. switch (cmd) {
  604. case SNDRV_PCM_TRIGGER_RESUME:
  605. ssp_enable(&priv->dev);
  606. break;
  607. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  608. val = ssp_read_reg(ssp, SSCR1);
  609. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  610. val |= SSCR1_TSRE;
  611. else
  612. val |= SSCR1_RSRE;
  613. ssp_write_reg(ssp, SSCR1, val);
  614. val = ssp_read_reg(ssp, SSSR);
  615. ssp_write_reg(ssp, SSSR, val);
  616. break;
  617. case SNDRV_PCM_TRIGGER_START:
  618. val = ssp_read_reg(ssp, SSCR1);
  619. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  620. val |= SSCR1_TSRE;
  621. else
  622. val |= SSCR1_RSRE;
  623. ssp_write_reg(ssp, SSCR1, val);
  624. ssp_enable(&priv->dev);
  625. break;
  626. case SNDRV_PCM_TRIGGER_STOP:
  627. val = ssp_read_reg(ssp, SSCR1);
  628. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  629. val &= ~SSCR1_TSRE;
  630. else
  631. val &= ~SSCR1_RSRE;
  632. ssp_write_reg(ssp, SSCR1, val);
  633. break;
  634. case SNDRV_PCM_TRIGGER_SUSPEND:
  635. ssp_disable(&priv->dev);
  636. break;
  637. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  638. val = ssp_read_reg(ssp, SSCR1);
  639. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  640. val &= ~SSCR1_TSRE;
  641. else
  642. val &= ~SSCR1_RSRE;
  643. ssp_write_reg(ssp, SSCR1, val);
  644. break;
  645. default:
  646. ret = -EINVAL;
  647. }
  648. dump_registers(ssp);
  649. return ret;
  650. }
  651. static int pxa_ssp_probe(struct platform_device *pdev,
  652. struct snd_soc_dai *dai)
  653. {
  654. struct ssp_priv *priv;
  655. int ret;
  656. priv = kzalloc(sizeof(struct ssp_priv), GFP_KERNEL);
  657. if (!priv)
  658. return -ENOMEM;
  659. priv->dev.ssp = ssp_request(dai->id, "SoC audio");
  660. if (priv->dev.ssp == NULL) {
  661. ret = -ENODEV;
  662. goto err_priv;
  663. }
  664. dai->private_data = priv;
  665. return 0;
  666. err_priv:
  667. kfree(priv);
  668. return ret;
  669. }
  670. static void pxa_ssp_remove(struct platform_device *pdev,
  671. struct snd_soc_dai *dai)
  672. {
  673. struct ssp_priv *priv = dai->private_data;
  674. ssp_free(priv->dev.ssp);
  675. }
  676. #define PXA_SSP_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  677. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  678. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  679. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  680. #define PXA_SSP_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  681. SNDRV_PCM_FMTBIT_S24_LE | \
  682. SNDRV_PCM_FMTBIT_S32_LE)
  683. struct snd_soc_dai pxa_ssp_dai[] = {
  684. {
  685. .name = "pxa2xx-ssp1",
  686. .id = 0,
  687. .type = SND_SOC_DAI_PCM,
  688. .probe = pxa_ssp_probe,
  689. .remove = pxa_ssp_remove,
  690. .suspend = pxa_ssp_suspend,
  691. .resume = pxa_ssp_resume,
  692. .playback = {
  693. .channels_min = 1,
  694. .channels_max = 2,
  695. .rates = PXA_SSP_RATES,
  696. .formats = PXA_SSP_FORMATS,
  697. },
  698. .capture = {
  699. .channels_min = 1,
  700. .channels_max = 2,
  701. .rates = PXA_SSP_RATES,
  702. .formats = PXA_SSP_FORMATS,
  703. },
  704. .ops = {
  705. .startup = pxa_ssp_startup,
  706. .shutdown = pxa_ssp_shutdown,
  707. .trigger = pxa_ssp_trigger,
  708. .hw_params = pxa_ssp_hw_params,
  709. .set_sysclk = pxa_ssp_set_dai_sysclk,
  710. .set_clkdiv = pxa_ssp_set_dai_clkdiv,
  711. .set_pll = pxa_ssp_set_dai_pll,
  712. .set_fmt = pxa_ssp_set_dai_fmt,
  713. .set_tdm_slot = pxa_ssp_set_dai_tdm_slot,
  714. .set_tristate = pxa_ssp_set_dai_tristate,
  715. },
  716. },
  717. { .name = "pxa2xx-ssp2",
  718. .id = 1,
  719. .type = SND_SOC_DAI_PCM,
  720. .probe = pxa_ssp_probe,
  721. .remove = pxa_ssp_remove,
  722. .suspend = pxa_ssp_suspend,
  723. .resume = pxa_ssp_resume,
  724. .playback = {
  725. .channels_min = 1,
  726. .channels_max = 2,
  727. .rates = PXA_SSP_RATES,
  728. .formats = PXA_SSP_FORMATS,
  729. },
  730. .capture = {
  731. .channels_min = 1,
  732. .channels_max = 2,
  733. .rates = PXA_SSP_RATES,
  734. .formats = PXA_SSP_FORMATS,
  735. },
  736. .ops = {
  737. .startup = pxa_ssp_startup,
  738. .shutdown = pxa_ssp_shutdown,
  739. .trigger = pxa_ssp_trigger,
  740. .hw_params = pxa_ssp_hw_params,
  741. .set_sysclk = pxa_ssp_set_dai_sysclk,
  742. .set_clkdiv = pxa_ssp_set_dai_clkdiv,
  743. .set_pll = pxa_ssp_set_dai_pll,
  744. .set_fmt = pxa_ssp_set_dai_fmt,
  745. .set_tdm_slot = pxa_ssp_set_dai_tdm_slot,
  746. .set_tristate = pxa_ssp_set_dai_tristate,
  747. },
  748. },
  749. {
  750. .name = "pxa2xx-ssp3",
  751. .id = 2,
  752. .type = SND_SOC_DAI_PCM,
  753. .probe = pxa_ssp_probe,
  754. .remove = pxa_ssp_remove,
  755. .suspend = pxa_ssp_suspend,
  756. .resume = pxa_ssp_resume,
  757. .playback = {
  758. .channels_min = 1,
  759. .channels_max = 2,
  760. .rates = PXA_SSP_RATES,
  761. .formats = PXA_SSP_FORMATS,
  762. },
  763. .capture = {
  764. .channels_min = 1,
  765. .channels_max = 2,
  766. .rates = PXA_SSP_RATES,
  767. .formats = PXA_SSP_FORMATS,
  768. },
  769. .ops = {
  770. .startup = pxa_ssp_startup,
  771. .shutdown = pxa_ssp_shutdown,
  772. .trigger = pxa_ssp_trigger,
  773. .hw_params = pxa_ssp_hw_params,
  774. .set_sysclk = pxa_ssp_set_dai_sysclk,
  775. .set_clkdiv = pxa_ssp_set_dai_clkdiv,
  776. .set_pll = pxa_ssp_set_dai_pll,
  777. .set_fmt = pxa_ssp_set_dai_fmt,
  778. .set_tdm_slot = pxa_ssp_set_dai_tdm_slot,
  779. .set_tristate = pxa_ssp_set_dai_tristate,
  780. },
  781. },
  782. {
  783. .name = "pxa2xx-ssp4",
  784. .id = 3,
  785. .type = SND_SOC_DAI_PCM,
  786. .probe = pxa_ssp_probe,
  787. .remove = pxa_ssp_remove,
  788. .suspend = pxa_ssp_suspend,
  789. .resume = pxa_ssp_resume,
  790. .playback = {
  791. .channels_min = 1,
  792. .channels_max = 2,
  793. .rates = PXA_SSP_RATES,
  794. .formats = PXA_SSP_FORMATS,
  795. },
  796. .capture = {
  797. .channels_min = 1,
  798. .channels_max = 2,
  799. .rates = PXA_SSP_RATES,
  800. .formats = PXA_SSP_FORMATS,
  801. },
  802. .ops = {
  803. .startup = pxa_ssp_startup,
  804. .shutdown = pxa_ssp_shutdown,
  805. .trigger = pxa_ssp_trigger,
  806. .hw_params = pxa_ssp_hw_params,
  807. .set_sysclk = pxa_ssp_set_dai_sysclk,
  808. .set_clkdiv = pxa_ssp_set_dai_clkdiv,
  809. .set_pll = pxa_ssp_set_dai_pll,
  810. .set_fmt = pxa_ssp_set_dai_fmt,
  811. .set_tdm_slot = pxa_ssp_set_dai_tdm_slot,
  812. .set_tristate = pxa_ssp_set_dai_tristate,
  813. },
  814. },
  815. };
  816. EXPORT_SYMBOL_GPL(pxa_ssp_dai);
  817. /* Module information */
  818. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  819. MODULE_DESCRIPTION("PXA SSP/PCM SoC Interface");
  820. MODULE_LICENSE("GPL");