pxa-ssp.c 19 KB

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