pxa-ssp.c 19 KB

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