pxa-ssp.c 20 KB

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