pxa-ssp.c 20 KB

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