pxa-ssp.c 21 KB

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