pxa-ssp.c 20 KB

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