pxa-ssp.c 23 KB

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