pxa-ssp.c 20 KB

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