pxa-ssp.c 19 KB

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