pxa-ssp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * pxa-ssp.c -- ALSA Soc Audio Layer
  3. *
  4. * Copyright 2005,2008 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood
  6. * Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * TODO:
  14. * o Test network mode for > 16bit sample size
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/io.h>
  22. #include <linux/pxa2xx_ssp.h>
  23. #include <linux/of.h>
  24. #include <linux/dmaengine.h>
  25. #include <asm/irq.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/initval.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include <sound/pxa2xx-lib.h>
  32. #include <sound/dmaengine_pcm.h>
  33. #include "pxa-ssp.h"
  34. /*
  35. * SSP audio private data
  36. */
  37. struct ssp_priv {
  38. struct ssp_device *ssp;
  39. struct clk *extclk;
  40. unsigned long ssp_clk;
  41. unsigned int sysclk;
  42. unsigned int dai_fmt;
  43. unsigned int configured_dai_fmt;
  44. #ifdef CONFIG_PM
  45. uint32_t cr0;
  46. uint32_t cr1;
  47. uint32_t to;
  48. uint32_t psp;
  49. #endif
  50. };
  51. static void dump_registers(struct ssp_device *ssp)
  52. {
  53. dev_dbg(&ssp->pdev->dev, "SSCR0 0x%08x SSCR1 0x%08x SSTO 0x%08x\n",
  54. pxa_ssp_read_reg(ssp, SSCR0), pxa_ssp_read_reg(ssp, SSCR1),
  55. pxa_ssp_read_reg(ssp, SSTO));
  56. dev_dbg(&ssp->pdev->dev, "SSPSP 0x%08x SSSR 0x%08x SSACD 0x%08x\n",
  57. pxa_ssp_read_reg(ssp, SSPSP), pxa_ssp_read_reg(ssp, SSSR),
  58. pxa_ssp_read_reg(ssp, SSACD));
  59. }
  60. static void pxa_ssp_enable(struct ssp_device *ssp)
  61. {
  62. uint32_t sscr0;
  63. sscr0 = __raw_readl(ssp->mmio_base + SSCR0) | SSCR0_SSE;
  64. __raw_writel(sscr0, ssp->mmio_base + SSCR0);
  65. }
  66. static void pxa_ssp_disable(struct ssp_device *ssp)
  67. {
  68. uint32_t sscr0;
  69. sscr0 = __raw_readl(ssp->mmio_base + SSCR0) & ~SSCR0_SSE;
  70. __raw_writel(sscr0, ssp->mmio_base + SSCR0);
  71. }
  72. static void pxa_ssp_set_dma_params(struct ssp_device *ssp, int width4,
  73. int out, struct snd_dmaengine_dai_dma_data *dma)
  74. {
  75. dma->addr_width = width4 ? DMA_SLAVE_BUSWIDTH_4_BYTES :
  76. DMA_SLAVE_BUSWIDTH_2_BYTES;
  77. dma->maxburst = 16;
  78. dma->addr = ssp->phys_base + SSDR;
  79. }
  80. static int pxa_ssp_startup(struct snd_pcm_substream *substream,
  81. struct snd_soc_dai *cpu_dai)
  82. {
  83. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  84. struct ssp_device *ssp = priv->ssp;
  85. struct snd_dmaengine_dai_dma_data *dma;
  86. int ret = 0;
  87. if (!cpu_dai->active) {
  88. clk_prepare_enable(ssp->clk);
  89. pxa_ssp_disable(ssp);
  90. }
  91. if (priv->extclk)
  92. clk_prepare_enable(priv->extclk);
  93. dma = kzalloc(sizeof(struct snd_dmaengine_dai_dma_data), GFP_KERNEL);
  94. if (!dma)
  95. return -ENOMEM;
  96. dma->chan_name = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  97. "tx" : "rx";
  98. snd_soc_dai_set_dma_data(cpu_dai, substream, dma);
  99. return ret;
  100. }
  101. static void pxa_ssp_shutdown(struct snd_pcm_substream *substream,
  102. struct snd_soc_dai *cpu_dai)
  103. {
  104. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  105. struct ssp_device *ssp = priv->ssp;
  106. if (!cpu_dai->active) {
  107. pxa_ssp_disable(ssp);
  108. clk_disable_unprepare(ssp->clk);
  109. }
  110. if (priv->extclk)
  111. clk_disable_unprepare(priv->extclk);
  112. kfree(snd_soc_dai_get_dma_data(cpu_dai, substream));
  113. snd_soc_dai_set_dma_data(cpu_dai, substream, NULL);
  114. }
  115. #ifdef CONFIG_PM
  116. static int pxa_ssp_suspend(struct snd_soc_dai *cpu_dai)
  117. {
  118. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  119. struct ssp_device *ssp = priv->ssp;
  120. if (!cpu_dai->active)
  121. clk_prepare_enable(ssp->clk);
  122. priv->cr0 = __raw_readl(ssp->mmio_base + SSCR0);
  123. priv->cr1 = __raw_readl(ssp->mmio_base + SSCR1);
  124. priv->to = __raw_readl(ssp->mmio_base + SSTO);
  125. priv->psp = __raw_readl(ssp->mmio_base + SSPSP);
  126. pxa_ssp_disable(ssp);
  127. clk_disable_unprepare(ssp->clk);
  128. return 0;
  129. }
  130. static int pxa_ssp_resume(struct snd_soc_dai *cpu_dai)
  131. {
  132. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  133. struct ssp_device *ssp = priv->ssp;
  134. uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE;
  135. clk_prepare_enable(ssp->clk);
  136. __raw_writel(sssr, ssp->mmio_base + SSSR);
  137. __raw_writel(priv->cr0 & ~SSCR0_SSE, ssp->mmio_base + SSCR0);
  138. __raw_writel(priv->cr1, ssp->mmio_base + SSCR1);
  139. __raw_writel(priv->to, ssp->mmio_base + SSTO);
  140. __raw_writel(priv->psp, ssp->mmio_base + SSPSP);
  141. if (cpu_dai->active)
  142. pxa_ssp_enable(ssp);
  143. else
  144. clk_disable_unprepare(ssp->clk);
  145. return 0;
  146. }
  147. #else
  148. #define pxa_ssp_suspend NULL
  149. #define pxa_ssp_resume NULL
  150. #endif
  151. /**
  152. * ssp_set_clkdiv - set SSP clock divider
  153. * @div: serial clock rate divider
  154. */
  155. static void pxa_ssp_set_scr(struct ssp_device *ssp, u32 div)
  156. {
  157. u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
  158. if (ssp->type == PXA25x_SSP) {
  159. sscr0 &= ~0x0000ff00;
  160. sscr0 |= ((div - 2)/2) << 8; /* 2..512 */
  161. } else {
  162. sscr0 &= ~0x000fff00;
  163. sscr0 |= (div - 1) << 8; /* 1..4096 */
  164. }
  165. pxa_ssp_write_reg(ssp, SSCR0, sscr0);
  166. }
  167. /*
  168. * Set the SSP ports SYSCLK.
  169. */
  170. static int pxa_ssp_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  171. int clk_id, unsigned int freq, int dir)
  172. {
  173. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  174. struct ssp_device *ssp = priv->ssp;
  175. u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0) &
  176. ~(SSCR0_ECS | SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
  177. if (priv->extclk) {
  178. int ret;
  179. /*
  180. * For DT based boards, if an extclk is given, use it
  181. * here and configure PXA_SSP_CLK_EXT.
  182. */
  183. ret = clk_set_rate(priv->extclk, freq);
  184. if (ret < 0)
  185. return ret;
  186. clk_id = PXA_SSP_CLK_EXT;
  187. }
  188. dev_dbg(&ssp->pdev->dev,
  189. "pxa_ssp_set_dai_sysclk id: %d, clk_id %d, freq %u\n",
  190. cpu_dai->id, clk_id, freq);
  191. switch (clk_id) {
  192. case PXA_SSP_CLK_NET_PLL:
  193. sscr0 |= SSCR0_MOD;
  194. break;
  195. case PXA_SSP_CLK_PLL:
  196. /* Internal PLL is fixed */
  197. if (ssp->type == PXA25x_SSP)
  198. priv->sysclk = 1843200;
  199. else
  200. priv->sysclk = 13000000;
  201. break;
  202. case PXA_SSP_CLK_EXT:
  203. priv->sysclk = freq;
  204. sscr0 |= SSCR0_ECS;
  205. break;
  206. case PXA_SSP_CLK_NET:
  207. priv->sysclk = freq;
  208. sscr0 |= SSCR0_NCS | SSCR0_MOD;
  209. break;
  210. case PXA_SSP_CLK_AUDIO:
  211. priv->sysclk = 0;
  212. pxa_ssp_set_scr(ssp, 1);
  213. sscr0 |= SSCR0_ACS;
  214. break;
  215. default:
  216. return -ENODEV;
  217. }
  218. /* The SSP clock must be disabled when changing SSP clock mode
  219. * on PXA2xx. On PXA3xx it must be enabled when doing so. */
  220. if (ssp->type != PXA3xx_SSP)
  221. clk_disable_unprepare(ssp->clk);
  222. pxa_ssp_write_reg(ssp, SSCR0, sscr0);
  223. if (ssp->type != PXA3xx_SSP)
  224. clk_prepare_enable(ssp->clk);
  225. return 0;
  226. }
  227. /*
  228. * Configure the PLL frequency pxa27x and (afaik - pxa320 only)
  229. */
  230. static int pxa_ssp_set_pll(struct ssp_priv *priv, unsigned int freq)
  231. {
  232. struct ssp_device *ssp = priv->ssp;
  233. u32 ssacd = pxa_ssp_read_reg(ssp, SSACD) & ~0x70;
  234. if (ssp->type == PXA3xx_SSP)
  235. pxa_ssp_write_reg(ssp, SSACDD, 0);
  236. switch (freq) {
  237. case 5622000:
  238. break;
  239. case 11345000:
  240. ssacd |= (0x1 << 4);
  241. break;
  242. case 12235000:
  243. ssacd |= (0x2 << 4);
  244. break;
  245. case 14857000:
  246. ssacd |= (0x3 << 4);
  247. break;
  248. case 32842000:
  249. ssacd |= (0x4 << 4);
  250. break;
  251. case 48000000:
  252. ssacd |= (0x5 << 4);
  253. break;
  254. case 0:
  255. /* Disable */
  256. break;
  257. default:
  258. /* PXA3xx has a clock ditherer which can be used to generate
  259. * a wider range of frequencies - calculate a value for it.
  260. */
  261. if (ssp->type == PXA3xx_SSP) {
  262. u32 val;
  263. u64 tmp = 19968;
  264. tmp *= 1000000;
  265. do_div(tmp, freq);
  266. val = tmp;
  267. val = (val << 16) | 64;
  268. pxa_ssp_write_reg(ssp, SSACDD, val);
  269. ssacd |= (0x6 << 4);
  270. dev_dbg(&ssp->pdev->dev,
  271. "Using SSACDD %x to supply %uHz\n",
  272. val, freq);
  273. break;
  274. }
  275. return -EINVAL;
  276. }
  277. pxa_ssp_write_reg(ssp, SSACD, ssacd);
  278. return 0;
  279. }
  280. /*
  281. * Set the active slots in TDM/Network mode
  282. */
  283. static int pxa_ssp_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
  284. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  285. {
  286. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  287. struct ssp_device *ssp = priv->ssp;
  288. u32 sscr0;
  289. sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
  290. sscr0 &= ~(SSCR0_MOD | SSCR0_SlotsPerFrm(8) | SSCR0_EDSS | SSCR0_DSS);
  291. /* set slot width */
  292. if (slot_width > 16)
  293. sscr0 |= SSCR0_EDSS | SSCR0_DataSize(slot_width - 16);
  294. else
  295. sscr0 |= SSCR0_DataSize(slot_width);
  296. if (slots > 1) {
  297. /* enable network mode */
  298. sscr0 |= SSCR0_MOD;
  299. /* set number of active slots */
  300. sscr0 |= SSCR0_SlotsPerFrm(slots);
  301. /* set active slot mask */
  302. pxa_ssp_write_reg(ssp, SSTSA, tx_mask);
  303. pxa_ssp_write_reg(ssp, SSRSA, rx_mask);
  304. }
  305. pxa_ssp_write_reg(ssp, SSCR0, sscr0);
  306. return 0;
  307. }
  308. /*
  309. * Tristate the SSP DAI lines
  310. */
  311. static int pxa_ssp_set_dai_tristate(struct snd_soc_dai *cpu_dai,
  312. int tristate)
  313. {
  314. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  315. struct ssp_device *ssp = priv->ssp;
  316. u32 sscr1;
  317. sscr1 = pxa_ssp_read_reg(ssp, SSCR1);
  318. if (tristate)
  319. sscr1 &= ~SSCR1_TTE;
  320. else
  321. sscr1 |= SSCR1_TTE;
  322. pxa_ssp_write_reg(ssp, SSCR1, sscr1);
  323. return 0;
  324. }
  325. static int pxa_ssp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  326. unsigned int fmt)
  327. {
  328. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  329. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  330. case SND_SOC_DAIFMT_CBM_CFM:
  331. case SND_SOC_DAIFMT_CBM_CFS:
  332. case SND_SOC_DAIFMT_CBS_CFS:
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  338. case SND_SOC_DAIFMT_NB_NF:
  339. case SND_SOC_DAIFMT_NB_IF:
  340. case SND_SOC_DAIFMT_IB_IF:
  341. case SND_SOC_DAIFMT_IB_NF:
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  347. case SND_SOC_DAIFMT_I2S:
  348. case SND_SOC_DAIFMT_DSP_A:
  349. case SND_SOC_DAIFMT_DSP_B:
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. /* Settings will be applied in hw_params() */
  355. priv->dai_fmt = fmt;
  356. return 0;
  357. }
  358. /*
  359. * Set up the SSP DAI format.
  360. * The SSP Port must be inactive before calling this function as the
  361. * physical interface format is changed.
  362. */
  363. static int pxa_ssp_configure_dai_fmt(struct ssp_priv *priv)
  364. {
  365. struct ssp_device *ssp = priv->ssp;
  366. u32 sscr0, sscr1, sspsp, scfr;
  367. /* check if we need to change anything at all */
  368. if (priv->configured_dai_fmt == priv->dai_fmt)
  369. return 0;
  370. /* reset port settings */
  371. sscr0 = pxa_ssp_read_reg(ssp, SSCR0) &
  372. ~(SSCR0_PSP | SSCR0_MOD);
  373. sscr1 = pxa_ssp_read_reg(ssp, SSCR1) &
  374. ~(SSCR1_SCLKDIR | SSCR1_SFRMDIR | SSCR1_SCFR |
  375. SSCR1_RWOT | SSCR1_TRAIL | SSCR1_TFT | SSCR1_RFT);
  376. sspsp = pxa_ssp_read_reg(ssp, SSPSP) &
  377. ~(SSPSP_SFRMP | SSPSP_SCMODE(3));
  378. sscr1 |= SSCR1_RxTresh(8) | SSCR1_TxTresh(7);
  379. switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  380. case SND_SOC_DAIFMT_CBM_CFM:
  381. sscr1 |= SSCR1_SCLKDIR | SSCR1_SFRMDIR | SSCR1_SCFR;
  382. break;
  383. case SND_SOC_DAIFMT_CBM_CFS:
  384. sscr1 |= SSCR1_SCLKDIR | SSCR1_SCFR;
  385. break;
  386. case SND_SOC_DAIFMT_CBS_CFS:
  387. break;
  388. default:
  389. return -EINVAL;
  390. }
  391. switch (priv->dai_fmt & SND_SOC_DAIFMT_INV_MASK) {
  392. case SND_SOC_DAIFMT_NB_NF:
  393. sspsp |= SSPSP_SFRMP;
  394. break;
  395. case SND_SOC_DAIFMT_NB_IF:
  396. break;
  397. case SND_SOC_DAIFMT_IB_IF:
  398. sspsp |= SSPSP_SCMODE(2);
  399. break;
  400. case SND_SOC_DAIFMT_IB_NF:
  401. sspsp |= SSPSP_SCMODE(2) | SSPSP_SFRMP;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. switch (priv->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  407. case SND_SOC_DAIFMT_I2S:
  408. sscr0 |= SSCR0_PSP;
  409. sscr1 |= SSCR1_RWOT | SSCR1_TRAIL;
  410. /* See hw_params() */
  411. break;
  412. case SND_SOC_DAIFMT_DSP_A:
  413. sspsp |= SSPSP_FSRT;
  414. /* fall through */
  415. case SND_SOC_DAIFMT_DSP_B:
  416. sscr0 |= SSCR0_MOD | SSCR0_PSP;
  417. sscr1 |= SSCR1_TRAIL | SSCR1_RWOT;
  418. break;
  419. default:
  420. return -EINVAL;
  421. }
  422. pxa_ssp_write_reg(ssp, SSCR0, sscr0);
  423. pxa_ssp_write_reg(ssp, SSCR1, sscr1);
  424. pxa_ssp_write_reg(ssp, SSPSP, sspsp);
  425. switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  426. case SND_SOC_DAIFMT_CBM_CFM:
  427. case SND_SOC_DAIFMT_CBM_CFS:
  428. scfr = pxa_ssp_read_reg(ssp, SSCR1) | SSCR1_SCFR;
  429. pxa_ssp_write_reg(ssp, SSCR1, scfr);
  430. while (pxa_ssp_read_reg(ssp, SSSR) & SSSR_BSY)
  431. cpu_relax();
  432. break;
  433. }
  434. dump_registers(ssp);
  435. /* Since we are configuring the timings for the format by hand
  436. * we have to defer some things until hw_params() where we
  437. * know parameters like the sample size.
  438. */
  439. priv->configured_dai_fmt = priv->dai_fmt;
  440. return 0;
  441. }
  442. struct pxa_ssp_clock_mode {
  443. int rate;
  444. int pll;
  445. u8 acds;
  446. u8 scdb;
  447. };
  448. static const struct pxa_ssp_clock_mode pxa_ssp_clock_modes[] = {
  449. { .rate = 8000, .pll = 32842000, .acds = SSACD_ACDS_32, .scdb = SSACD_SCDB_4X },
  450. { .rate = 11025, .pll = 5622000, .acds = SSACD_ACDS_4, .scdb = SSACD_SCDB_4X },
  451. { .rate = 16000, .pll = 32842000, .acds = SSACD_ACDS_16, .scdb = SSACD_SCDB_4X },
  452. { .rate = 22050, .pll = 5622000, .acds = SSACD_ACDS_2, .scdb = SSACD_SCDB_4X },
  453. { .rate = 44100, .pll = 11345000, .acds = SSACD_ACDS_2, .scdb = SSACD_SCDB_4X },
  454. { .rate = 48000, .pll = 12235000, .acds = SSACD_ACDS_2, .scdb = SSACD_SCDB_4X },
  455. { .rate = 96000, .pll = 12235000, .acds = SSACD_ACDS_4, .scdb = SSACD_SCDB_1X },
  456. {}
  457. };
  458. /*
  459. * Set the SSP audio DMA parameters and sample size.
  460. * Can be called multiple times by oss emulation.
  461. */
  462. static int pxa_ssp_hw_params(struct snd_pcm_substream *substream,
  463. struct snd_pcm_hw_params *params,
  464. struct snd_soc_dai *cpu_dai)
  465. {
  466. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  467. struct ssp_device *ssp = priv->ssp;
  468. int chn = params_channels(params);
  469. u32 sscr0, sspsp;
  470. int width = snd_pcm_format_physical_width(params_format(params));
  471. int ttsa = pxa_ssp_read_reg(ssp, SSTSA) & 0xf;
  472. struct snd_dmaengine_dai_dma_data *dma_data;
  473. int rate = params_rate(params);
  474. int bclk = rate * chn * (width / 8);
  475. int ret;
  476. dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
  477. /* Network mode with one active slot (ttsa == 1) can be used
  478. * to force 16-bit frame width on the wire (for S16_LE), even
  479. * with two channels. Use 16-bit DMA transfers for this case.
  480. */
  481. pxa_ssp_set_dma_params(ssp,
  482. ((chn == 2) && (ttsa != 1)) || (width == 32),
  483. substream->stream == SNDRV_PCM_STREAM_PLAYBACK, dma_data);
  484. /* we can only change the settings if the port is not in use */
  485. if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE)
  486. return 0;
  487. ret = pxa_ssp_configure_dai_fmt(priv);
  488. if (ret < 0)
  489. return ret;
  490. /* clear selected SSP bits */
  491. sscr0 = pxa_ssp_read_reg(ssp, SSCR0) & ~(SSCR0_DSS | SSCR0_EDSS);
  492. /* bit size */
  493. switch (params_format(params)) {
  494. case SNDRV_PCM_FORMAT_S16_LE:
  495. if (ssp->type == PXA3xx_SSP)
  496. sscr0 |= SSCR0_FPCKE;
  497. sscr0 |= SSCR0_DataSize(16);
  498. break;
  499. case SNDRV_PCM_FORMAT_S24_LE:
  500. sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(8));
  501. break;
  502. case SNDRV_PCM_FORMAT_S32_LE:
  503. sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(16));
  504. break;
  505. }
  506. pxa_ssp_write_reg(ssp, SSCR0, sscr0);
  507. if (sscr0 & SSCR0_ACS) {
  508. ret = pxa_ssp_set_pll(priv, bclk);
  509. /*
  510. * If we were able to generate the bclk directly,
  511. * all is fine. Otherwise, look up the closest rate
  512. * from the table and also set the dividers.
  513. */
  514. if (ret < 0) {
  515. const struct pxa_ssp_clock_mode *m;
  516. int ssacd, acds;
  517. for (m = pxa_ssp_clock_modes; m->rate; m++) {
  518. if (m->rate == rate)
  519. break;
  520. }
  521. if (!m->rate)
  522. return -EINVAL;
  523. acds = m->acds;
  524. /* The values in the table are for 16 bits */
  525. if (width == 32)
  526. acds--;
  527. ret = pxa_ssp_set_pll(priv, bclk);
  528. if (ret < 0)
  529. return ret;
  530. ssacd = pxa_ssp_read_reg(ssp, SSACD);
  531. ssacd &= ~(SSACD_ACDS(7) | SSACD_SCDB_1X);
  532. ssacd |= SSACD_ACDS(m->acds);
  533. ssacd |= m->scdb;
  534. pxa_ssp_write_reg(ssp, SSACD, ssacd);
  535. }
  536. } else if (sscr0 & SSCR0_ECS) {
  537. /*
  538. * For setups with external clocking, the PLL and its diviers
  539. * are not active. Instead, the SCR bits in SSCR0 can be used
  540. * to divide the clock.
  541. */
  542. pxa_ssp_set_scr(ssp, bclk / rate);
  543. }
  544. switch (priv->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  545. case SND_SOC_DAIFMT_I2S:
  546. sspsp = pxa_ssp_read_reg(ssp, SSPSP);
  547. if (((priv->sysclk / bclk) == 64) && (width == 16)) {
  548. /* This is a special case where the bitclk is 64fs
  549. * and we're not dealing with 2*32 bits of audio
  550. * samples.
  551. *
  552. * The SSP values used for that are all found out by
  553. * trying and failing a lot; some of the registers
  554. * needed for that mode are only available on PXA3xx.
  555. */
  556. if (ssp->type != PXA3xx_SSP)
  557. return -EINVAL;
  558. sspsp |= SSPSP_SFRMWDTH(width * 2);
  559. sspsp |= SSPSP_SFRMDLY(width * 4);
  560. sspsp |= SSPSP_EDMYSTOP(3);
  561. sspsp |= SSPSP_DMYSTOP(3);
  562. sspsp |= SSPSP_DMYSTRT(1);
  563. } else {
  564. /* The frame width is the width the LRCLK is
  565. * asserted for; the delay is expressed in
  566. * half cycle units. We need the extra cycle
  567. * because the data starts clocking out one BCLK
  568. * after LRCLK changes polarity.
  569. */
  570. sspsp |= SSPSP_SFRMWDTH(width + 1);
  571. sspsp |= SSPSP_SFRMDLY((width + 1) * 2);
  572. sspsp |= SSPSP_DMYSTRT(1);
  573. }
  574. pxa_ssp_write_reg(ssp, SSPSP, sspsp);
  575. break;
  576. default:
  577. break;
  578. }
  579. /* When we use a network mode, we always require TDM slots
  580. * - complain loudly and fail if they've not been set up yet.
  581. */
  582. if ((sscr0 & SSCR0_MOD) && !ttsa) {
  583. dev_err(&ssp->pdev->dev, "No TDM timeslot configured\n");
  584. return -EINVAL;
  585. }
  586. dump_registers(ssp);
  587. return 0;
  588. }
  589. static void pxa_ssp_set_running_bit(struct snd_pcm_substream *substream,
  590. struct ssp_device *ssp, int value)
  591. {
  592. uint32_t sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
  593. uint32_t sscr1 = pxa_ssp_read_reg(ssp, SSCR1);
  594. uint32_t sspsp = pxa_ssp_read_reg(ssp, SSPSP);
  595. uint32_t sssr = pxa_ssp_read_reg(ssp, SSSR);
  596. if (value && (sscr0 & SSCR0_SSE))
  597. pxa_ssp_write_reg(ssp, SSCR0, sscr0 & ~SSCR0_SSE);
  598. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  599. if (value)
  600. sscr1 |= SSCR1_TSRE;
  601. else
  602. sscr1 &= ~SSCR1_TSRE;
  603. } else {
  604. if (value)
  605. sscr1 |= SSCR1_RSRE;
  606. else
  607. sscr1 &= ~SSCR1_RSRE;
  608. }
  609. pxa_ssp_write_reg(ssp, SSCR1, sscr1);
  610. if (value) {
  611. pxa_ssp_write_reg(ssp, SSSR, sssr);
  612. pxa_ssp_write_reg(ssp, SSPSP, sspsp);
  613. pxa_ssp_write_reg(ssp, SSCR0, sscr0 | SSCR0_SSE);
  614. }
  615. }
  616. static int pxa_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
  617. struct snd_soc_dai *cpu_dai)
  618. {
  619. int ret = 0;
  620. struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
  621. struct ssp_device *ssp = priv->ssp;
  622. int val;
  623. switch (cmd) {
  624. case SNDRV_PCM_TRIGGER_RESUME:
  625. pxa_ssp_enable(ssp);
  626. break;
  627. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  628. pxa_ssp_set_running_bit(substream, ssp, 1);
  629. val = pxa_ssp_read_reg(ssp, SSSR);
  630. pxa_ssp_write_reg(ssp, SSSR, val);
  631. break;
  632. case SNDRV_PCM_TRIGGER_START:
  633. pxa_ssp_set_running_bit(substream, ssp, 1);
  634. break;
  635. case SNDRV_PCM_TRIGGER_STOP:
  636. pxa_ssp_set_running_bit(substream, ssp, 0);
  637. break;
  638. case SNDRV_PCM_TRIGGER_SUSPEND:
  639. pxa_ssp_disable(ssp);
  640. break;
  641. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  642. pxa_ssp_set_running_bit(substream, ssp, 0);
  643. break;
  644. default:
  645. ret = -EINVAL;
  646. }
  647. dump_registers(ssp);
  648. return ret;
  649. }
  650. static int pxa_ssp_probe(struct snd_soc_dai *dai)
  651. {
  652. struct device *dev = dai->dev;
  653. struct ssp_priv *priv;
  654. int ret;
  655. priv = kzalloc(sizeof(struct ssp_priv), GFP_KERNEL);
  656. if (!priv)
  657. return -ENOMEM;
  658. if (dev->of_node) {
  659. struct device_node *ssp_handle;
  660. ssp_handle = of_parse_phandle(dev->of_node, "port", 0);
  661. if (!ssp_handle) {
  662. dev_err(dev, "unable to get 'port' phandle\n");
  663. ret = -ENODEV;
  664. goto err_priv;
  665. }
  666. priv->ssp = pxa_ssp_request_of(ssp_handle, "SoC audio");
  667. if (priv->ssp == NULL) {
  668. ret = -ENODEV;
  669. goto err_priv;
  670. }
  671. priv->extclk = devm_clk_get(dev, "extclk");
  672. if (IS_ERR(priv->extclk)) {
  673. ret = PTR_ERR(priv->extclk);
  674. if (ret == -EPROBE_DEFER)
  675. return ret;
  676. priv->extclk = NULL;
  677. }
  678. } else {
  679. priv->ssp = pxa_ssp_request(dai->id + 1, "SoC audio");
  680. if (priv->ssp == NULL) {
  681. ret = -ENODEV;
  682. goto err_priv;
  683. }
  684. }
  685. priv->dai_fmt = (unsigned int) -1;
  686. snd_soc_dai_set_drvdata(dai, priv);
  687. return 0;
  688. err_priv:
  689. kfree(priv);
  690. return ret;
  691. }
  692. static int pxa_ssp_remove(struct snd_soc_dai *dai)
  693. {
  694. struct ssp_priv *priv = snd_soc_dai_get_drvdata(dai);
  695. pxa_ssp_free(priv->ssp);
  696. kfree(priv);
  697. return 0;
  698. }
  699. #define PXA_SSP_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  700. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  701. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  702. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  703. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  704. #define PXA_SSP_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  705. static const struct snd_soc_dai_ops pxa_ssp_dai_ops = {
  706. .startup = pxa_ssp_startup,
  707. .shutdown = pxa_ssp_shutdown,
  708. .trigger = pxa_ssp_trigger,
  709. .hw_params = pxa_ssp_hw_params,
  710. .set_sysclk = pxa_ssp_set_dai_sysclk,
  711. .set_fmt = pxa_ssp_set_dai_fmt,
  712. .set_tdm_slot = pxa_ssp_set_dai_tdm_slot,
  713. .set_tristate = pxa_ssp_set_dai_tristate,
  714. };
  715. static struct snd_soc_dai_driver pxa_ssp_dai = {
  716. .probe = pxa_ssp_probe,
  717. .remove = pxa_ssp_remove,
  718. .suspend = pxa_ssp_suspend,
  719. .resume = pxa_ssp_resume,
  720. .playback = {
  721. .channels_min = 1,
  722. .channels_max = 8,
  723. .rates = PXA_SSP_RATES,
  724. .formats = PXA_SSP_FORMATS,
  725. },
  726. .capture = {
  727. .channels_min = 1,
  728. .channels_max = 8,
  729. .rates = PXA_SSP_RATES,
  730. .formats = PXA_SSP_FORMATS,
  731. },
  732. .ops = &pxa_ssp_dai_ops,
  733. };
  734. static const struct snd_soc_component_driver pxa_ssp_component = {
  735. .name = "pxa-ssp",
  736. .ops = &pxa2xx_pcm_ops,
  737. .pcm_new = pxa2xx_soc_pcm_new,
  738. .pcm_free = pxa2xx_pcm_free_dma_buffers,
  739. };
  740. #ifdef CONFIG_OF
  741. static const struct of_device_id pxa_ssp_of_ids[] = {
  742. { .compatible = "mrvl,pxa-ssp-dai" },
  743. {}
  744. };
  745. MODULE_DEVICE_TABLE(of, pxa_ssp_of_ids);
  746. #endif
  747. static int asoc_ssp_probe(struct platform_device *pdev)
  748. {
  749. return devm_snd_soc_register_component(&pdev->dev, &pxa_ssp_component,
  750. &pxa_ssp_dai, 1);
  751. }
  752. static struct platform_driver asoc_ssp_driver = {
  753. .driver = {
  754. .name = "pxa-ssp-dai",
  755. .of_match_table = of_match_ptr(pxa_ssp_of_ids),
  756. },
  757. .probe = asoc_ssp_probe,
  758. };
  759. module_platform_driver(asoc_ssp_driver);
  760. /* Module information */
  761. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  762. MODULE_DESCRIPTION("PXA SSP/PCM SoC Interface");
  763. MODULE_LICENSE("GPL");
  764. MODULE_ALIAS("platform:pxa-ssp-dai");