pxa-ssp.c 20 KB

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