pxa-ssp.c 22 KB

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