pxa-ssp.c 21 KB

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