atmel_ssc_dai.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * atmel_ssc_dai.c -- ALSA SoC ATMEL SSC Audio Layer Platform driver
  3. *
  4. * Copyright (C) 2005 SAN People
  5. * Copyright (C) 2008 Atmel
  6. *
  7. * Author: Sedji Gaouaou <sedji.gaouaou@atmel.com>
  8. * ATMEL CORP.
  9. *
  10. * Based on at91-ssc.c by
  11. * Frank Mandarino <fmandarino@endrelia.com>
  12. * Based on pxa2xx Platform drivers by
  13. * Liam Girdwood <lrg@slimlogic.co.uk>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/init.h>
  30. #include <linux/module.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/device.h>
  33. #include <linux/delay.h>
  34. #include <linux/clk.h>
  35. #include <linux/atmel_pdc.h>
  36. #include <linux/atmel-ssc.h>
  37. #include <sound/core.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/initval.h>
  41. #include <sound/soc.h>
  42. #include <mach/hardware.h>
  43. #include "atmel-pcm.h"
  44. #include "atmel_ssc_dai.h"
  45. #if defined(CONFIG_ARCH_AT91SAM9260) || defined(CONFIG_ARCH_AT91SAM9G20)
  46. #define NUM_SSC_DEVICES 1
  47. #else
  48. #define NUM_SSC_DEVICES 3
  49. #endif
  50. /*
  51. * SSC PDC registers required by the PCM DMA engine.
  52. */
  53. static struct atmel_pdc_regs pdc_tx_reg = {
  54. .xpr = ATMEL_PDC_TPR,
  55. .xcr = ATMEL_PDC_TCR,
  56. .xnpr = ATMEL_PDC_TNPR,
  57. .xncr = ATMEL_PDC_TNCR,
  58. };
  59. static struct atmel_pdc_regs pdc_rx_reg = {
  60. .xpr = ATMEL_PDC_RPR,
  61. .xcr = ATMEL_PDC_RCR,
  62. .xnpr = ATMEL_PDC_RNPR,
  63. .xncr = ATMEL_PDC_RNCR,
  64. };
  65. /*
  66. * SSC & PDC status bits for transmit and receive.
  67. */
  68. static struct atmel_ssc_mask ssc_tx_mask = {
  69. .ssc_enable = SSC_BIT(CR_TXEN),
  70. .ssc_disable = SSC_BIT(CR_TXDIS),
  71. .ssc_endx = SSC_BIT(SR_ENDTX),
  72. .ssc_endbuf = SSC_BIT(SR_TXBUFE),
  73. .pdc_enable = ATMEL_PDC_TXTEN,
  74. .pdc_disable = ATMEL_PDC_TXTDIS,
  75. };
  76. static struct atmel_ssc_mask ssc_rx_mask = {
  77. .ssc_enable = SSC_BIT(CR_RXEN),
  78. .ssc_disable = SSC_BIT(CR_RXDIS),
  79. .ssc_endx = SSC_BIT(SR_ENDRX),
  80. .ssc_endbuf = SSC_BIT(SR_RXBUFF),
  81. .pdc_enable = ATMEL_PDC_RXTEN,
  82. .pdc_disable = ATMEL_PDC_RXTDIS,
  83. };
  84. /*
  85. * DMA parameters.
  86. */
  87. static struct atmel_pcm_dma_params ssc_dma_params[NUM_SSC_DEVICES][2] = {
  88. {{
  89. .name = "SSC0 PCM out",
  90. .pdc = &pdc_tx_reg,
  91. .mask = &ssc_tx_mask,
  92. },
  93. {
  94. .name = "SSC0 PCM in",
  95. .pdc = &pdc_rx_reg,
  96. .mask = &ssc_rx_mask,
  97. } },
  98. #if NUM_SSC_DEVICES == 3
  99. {{
  100. .name = "SSC1 PCM out",
  101. .pdc = &pdc_tx_reg,
  102. .mask = &ssc_tx_mask,
  103. },
  104. {
  105. .name = "SSC1 PCM in",
  106. .pdc = &pdc_rx_reg,
  107. .mask = &ssc_rx_mask,
  108. } },
  109. {{
  110. .name = "SSC2 PCM out",
  111. .pdc = &pdc_tx_reg,
  112. .mask = &ssc_tx_mask,
  113. },
  114. {
  115. .name = "SSC2 PCM in",
  116. .pdc = &pdc_rx_reg,
  117. .mask = &ssc_rx_mask,
  118. } },
  119. #endif
  120. };
  121. static struct atmel_ssc_info ssc_info[NUM_SSC_DEVICES] = {
  122. {
  123. .name = "ssc0",
  124. .lock = __SPIN_LOCK_UNLOCKED(ssc_info[0].lock),
  125. .dir_mask = SSC_DIR_MASK_UNUSED,
  126. .initialized = 0,
  127. },
  128. #if NUM_SSC_DEVICES == 3
  129. {
  130. .name = "ssc1",
  131. .lock = __SPIN_LOCK_UNLOCKED(ssc_info[1].lock),
  132. .dir_mask = SSC_DIR_MASK_UNUSED,
  133. .initialized = 0,
  134. },
  135. {
  136. .name = "ssc2",
  137. .lock = __SPIN_LOCK_UNLOCKED(ssc_info[2].lock),
  138. .dir_mask = SSC_DIR_MASK_UNUSED,
  139. .initialized = 0,
  140. },
  141. #endif
  142. };
  143. /*
  144. * SSC interrupt handler. Passes PDC interrupts to the DMA
  145. * interrupt handler in the PCM driver.
  146. */
  147. static irqreturn_t atmel_ssc_interrupt(int irq, void *dev_id)
  148. {
  149. struct atmel_ssc_info *ssc_p = dev_id;
  150. struct atmel_pcm_dma_params *dma_params;
  151. u32 ssc_sr;
  152. u32 ssc_substream_mask;
  153. int i;
  154. ssc_sr = (unsigned long)ssc_readl(ssc_p->ssc->regs, SR)
  155. & (unsigned long)ssc_readl(ssc_p->ssc->regs, IMR);
  156. /*
  157. * Loop through the substreams attached to this SSC. If
  158. * a DMA-related interrupt occurred on that substream, call
  159. * the DMA interrupt handler function, if one has been
  160. * registered in the dma_params structure by the PCM driver.
  161. */
  162. for (i = 0; i < ARRAY_SIZE(ssc_p->dma_params); i++) {
  163. dma_params = ssc_p->dma_params[i];
  164. if ((dma_params != NULL) &&
  165. (dma_params->dma_intr_handler != NULL)) {
  166. ssc_substream_mask = (dma_params->mask->ssc_endx |
  167. dma_params->mask->ssc_endbuf);
  168. if (ssc_sr & ssc_substream_mask) {
  169. dma_params->dma_intr_handler(ssc_sr,
  170. dma_params->
  171. substream);
  172. }
  173. }
  174. }
  175. return IRQ_HANDLED;
  176. }
  177. /*-------------------------------------------------------------------------*\
  178. * DAI functions
  179. \*-------------------------------------------------------------------------*/
  180. /*
  181. * Startup. Only that one substream allowed in each direction.
  182. */
  183. static int atmel_ssc_startup(struct snd_pcm_substream *substream,
  184. struct snd_soc_dai *dai)
  185. {
  186. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  187. int dir_mask;
  188. pr_debug("atmel_ssc_startup: SSC_SR=0x%u\n",
  189. ssc_readl(ssc_p->ssc->regs, SR));
  190. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  191. dir_mask = SSC_DIR_MASK_PLAYBACK;
  192. else
  193. dir_mask = SSC_DIR_MASK_CAPTURE;
  194. spin_lock_irq(&ssc_p->lock);
  195. if (ssc_p->dir_mask & dir_mask) {
  196. spin_unlock_irq(&ssc_p->lock);
  197. return -EBUSY;
  198. }
  199. ssc_p->dir_mask |= dir_mask;
  200. spin_unlock_irq(&ssc_p->lock);
  201. return 0;
  202. }
  203. /*
  204. * Shutdown. Clear DMA parameters and shutdown the SSC if there
  205. * are no other substreams open.
  206. */
  207. static void atmel_ssc_shutdown(struct snd_pcm_substream *substream,
  208. struct snd_soc_dai *dai)
  209. {
  210. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  211. struct atmel_pcm_dma_params *dma_params;
  212. int dir, dir_mask;
  213. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  214. dir = 0;
  215. else
  216. dir = 1;
  217. dma_params = ssc_p->dma_params[dir];
  218. if (dma_params != NULL) {
  219. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
  220. pr_debug("atmel_ssc_shutdown: %s disabled SSC_SR=0x%08x\n",
  221. (dir ? "receive" : "transmit"),
  222. ssc_readl(ssc_p->ssc->regs, SR));
  223. dma_params->ssc = NULL;
  224. dma_params->substream = NULL;
  225. ssc_p->dma_params[dir] = NULL;
  226. }
  227. dir_mask = 1 << dir;
  228. spin_lock_irq(&ssc_p->lock);
  229. ssc_p->dir_mask &= ~dir_mask;
  230. if (!ssc_p->dir_mask) {
  231. if (ssc_p->initialized) {
  232. /* Shutdown the SSC clock. */
  233. pr_debug("atmel_ssc_dau: Stopping clock\n");
  234. clk_disable(ssc_p->ssc->clk);
  235. free_irq(ssc_p->ssc->irq, ssc_p);
  236. ssc_p->initialized = 0;
  237. }
  238. /* Reset the SSC */
  239. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
  240. /* Clear the SSC dividers */
  241. ssc_p->cmr_div = ssc_p->tcmr_period = ssc_p->rcmr_period = 0;
  242. }
  243. spin_unlock_irq(&ssc_p->lock);
  244. }
  245. /*
  246. * Record the DAI format for use in hw_params().
  247. */
  248. static int atmel_ssc_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  249. unsigned int fmt)
  250. {
  251. struct atmel_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
  252. ssc_p->daifmt = fmt;
  253. return 0;
  254. }
  255. /*
  256. * Record SSC clock dividers for use in hw_params().
  257. */
  258. static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
  259. int div_id, int div)
  260. {
  261. struct atmel_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
  262. switch (div_id) {
  263. case ATMEL_SSC_CMR_DIV:
  264. /*
  265. * The same master clock divider is used for both
  266. * transmit and receive, so if a value has already
  267. * been set, it must match this value.
  268. */
  269. if (ssc_p->cmr_div == 0)
  270. ssc_p->cmr_div = div;
  271. else
  272. if (div != ssc_p->cmr_div)
  273. return -EBUSY;
  274. break;
  275. case ATMEL_SSC_TCMR_PERIOD:
  276. ssc_p->tcmr_period = div;
  277. break;
  278. case ATMEL_SSC_RCMR_PERIOD:
  279. ssc_p->rcmr_period = div;
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. return 0;
  285. }
  286. /*
  287. * Configure the SSC.
  288. */
  289. static int atmel_ssc_hw_params(struct snd_pcm_substream *substream,
  290. struct snd_pcm_hw_params *params,
  291. struct snd_soc_dai *dai)
  292. {
  293. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  294. int id = dai->id;
  295. struct atmel_ssc_info *ssc_p = &ssc_info[id];
  296. struct atmel_pcm_dma_params *dma_params;
  297. int dir, channels, bits;
  298. u32 tfmr, rfmr, tcmr, rcmr;
  299. int ret;
  300. /*
  301. * Currently, there is only one set of dma params for
  302. * each direction. If more are added, this code will
  303. * have to be changed to select the proper set.
  304. */
  305. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  306. dir = 0;
  307. else
  308. dir = 1;
  309. dma_params = &ssc_dma_params[id][dir];
  310. dma_params->ssc = ssc_p->ssc;
  311. dma_params->substream = substream;
  312. ssc_p->dma_params[dir] = dma_params;
  313. /*
  314. * The snd_soc_pcm_stream->dma_data field is only used to communicate
  315. * the appropriate DMA parameters to the pcm driver hw_params()
  316. * function. It should not be used for other purposes
  317. * as it is common to all substreams.
  318. */
  319. snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_params);
  320. channels = params_channels(params);
  321. /*
  322. * Determine sample size in bits and the PDC increment.
  323. */
  324. switch (params_format(params)) {
  325. case SNDRV_PCM_FORMAT_S8:
  326. bits = 8;
  327. dma_params->pdc_xfer_size = 1;
  328. break;
  329. case SNDRV_PCM_FORMAT_S16_LE:
  330. bits = 16;
  331. dma_params->pdc_xfer_size = 2;
  332. break;
  333. case SNDRV_PCM_FORMAT_S24_LE:
  334. bits = 24;
  335. dma_params->pdc_xfer_size = 4;
  336. break;
  337. case SNDRV_PCM_FORMAT_S32_LE:
  338. bits = 32;
  339. dma_params->pdc_xfer_size = 4;
  340. break;
  341. default:
  342. printk(KERN_WARNING "atmel_ssc_dai: unsupported PCM format");
  343. return -EINVAL;
  344. }
  345. /*
  346. * The SSC only supports up to 16-bit samples in I2S format, due
  347. * to the size of the Frame Mode Register FSLEN field.
  348. */
  349. if ((ssc_p->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_I2S
  350. && bits > 16) {
  351. printk(KERN_WARNING
  352. "atmel_ssc_dai: sample size %d "
  353. "is too large for I2S\n", bits);
  354. return -EINVAL;
  355. }
  356. /*
  357. * Compute SSC register settings.
  358. */
  359. switch (ssc_p->daifmt
  360. & (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
  361. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
  362. /*
  363. * I2S format, SSC provides BCLK and LRC clocks.
  364. *
  365. * The SSC transmit and receive clocks are generated
  366. * from the MCK divider, and the BCLK signal
  367. * is output on the SSC TK line.
  368. */
  369. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  370. | SSC_BF(RCMR_STTDLY, START_DELAY)
  371. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  372. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  373. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  374. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  375. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  376. | SSC_BF(RFMR_FSOS, SSC_FSOS_NEGATIVE)
  377. | SSC_BF(RFMR_FSLEN, (bits - 1))
  378. | SSC_BF(RFMR_DATNB, (channels - 1))
  379. | SSC_BIT(RFMR_MSBF)
  380. | SSC_BF(RFMR_LOOP, 0)
  381. | SSC_BF(RFMR_DATLEN, (bits - 1));
  382. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  383. | SSC_BF(TCMR_STTDLY, START_DELAY)
  384. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  385. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  386. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  387. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  388. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  389. | SSC_BF(TFMR_FSDEN, 0)
  390. | SSC_BF(TFMR_FSOS, SSC_FSOS_NEGATIVE)
  391. | SSC_BF(TFMR_FSLEN, (bits - 1))
  392. | SSC_BF(TFMR_DATNB, (channels - 1))
  393. | SSC_BIT(TFMR_MSBF)
  394. | SSC_BF(TFMR_DATDEF, 0)
  395. | SSC_BF(TFMR_DATLEN, (bits - 1));
  396. break;
  397. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
  398. /*
  399. * I2S format, CODEC supplies BCLK and LRC clocks.
  400. *
  401. * The SSC transmit clock is obtained from the BCLK signal on
  402. * on the TK line, and the SSC receive clock is
  403. * generated from the transmit clock.
  404. */
  405. rcmr = SSC_BF(RCMR_PERIOD, 0)
  406. | SSC_BF(RCMR_STTDLY, START_DELAY)
  407. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  408. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  409. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  410. | SSC_BF(RCMR_CKS, SSC_CKS_CLOCK);
  411. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  412. | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
  413. | SSC_BF(RFMR_FSLEN, 0)
  414. | SSC_BF(RFMR_DATNB, (channels - 1))
  415. | SSC_BIT(RFMR_MSBF)
  416. | SSC_BF(RFMR_LOOP, 0)
  417. | SSC_BF(RFMR_DATLEN, (bits - 1));
  418. tcmr = SSC_BF(TCMR_PERIOD, 0)
  419. | SSC_BF(TCMR_STTDLY, START_DELAY)
  420. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  421. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  422. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  423. | SSC_BF(TCMR_CKS, SSC_CKS_PIN);
  424. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  425. | SSC_BF(TFMR_FSDEN, 0)
  426. | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
  427. | SSC_BF(TFMR_FSLEN, 0)
  428. | SSC_BF(TFMR_DATNB, (channels - 1))
  429. | SSC_BIT(TFMR_MSBF)
  430. | SSC_BF(TFMR_DATDEF, 0)
  431. | SSC_BF(TFMR_DATLEN, (bits - 1));
  432. break;
  433. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
  434. /*
  435. * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
  436. *
  437. * The SSC transmit and receive clocks are generated from the
  438. * MCK divider, and the BCLK signal is output
  439. * on the SSC TK line.
  440. */
  441. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  442. | SSC_BF(RCMR_STTDLY, 1)
  443. | SSC_BF(RCMR_START, SSC_START_RISING_RF)
  444. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  445. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  446. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  447. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  448. | SSC_BF(RFMR_FSOS, SSC_FSOS_POSITIVE)
  449. | SSC_BF(RFMR_FSLEN, 0)
  450. | SSC_BF(RFMR_DATNB, (channels - 1))
  451. | SSC_BIT(RFMR_MSBF)
  452. | SSC_BF(RFMR_LOOP, 0)
  453. | SSC_BF(RFMR_DATLEN, (bits - 1));
  454. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  455. | SSC_BF(TCMR_STTDLY, 1)
  456. | SSC_BF(TCMR_START, SSC_START_RISING_RF)
  457. | SSC_BF(TCMR_CKI, SSC_CKI_RISING)
  458. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  459. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  460. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  461. | SSC_BF(TFMR_FSDEN, 0)
  462. | SSC_BF(TFMR_FSOS, SSC_FSOS_POSITIVE)
  463. | SSC_BF(TFMR_FSLEN, 0)
  464. | SSC_BF(TFMR_DATNB, (channels - 1))
  465. | SSC_BIT(TFMR_MSBF)
  466. | SSC_BF(TFMR_DATDEF, 0)
  467. | SSC_BF(TFMR_DATLEN, (bits - 1));
  468. break;
  469. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
  470. default:
  471. printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n",
  472. ssc_p->daifmt);
  473. return -EINVAL;
  474. }
  475. pr_debug("atmel_ssc_hw_params: "
  476. "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
  477. rcmr, rfmr, tcmr, tfmr);
  478. if (!ssc_p->initialized) {
  479. /* Enable PMC peripheral clock for this SSC */
  480. pr_debug("atmel_ssc_dai: Starting clock\n");
  481. clk_enable(ssc_p->ssc->clk);
  482. /* Reset the SSC and its PDC registers */
  483. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
  484. ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0);
  485. ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0);
  486. ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0);
  487. ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0);
  488. ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0);
  489. ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0);
  490. ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0);
  491. ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0);
  492. ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0,
  493. ssc_p->name, ssc_p);
  494. if (ret < 0) {
  495. printk(KERN_WARNING
  496. "atmel_ssc_dai: request_irq failure\n");
  497. pr_debug("Atmel_ssc_dai: Stoping clock\n");
  498. clk_disable(ssc_p->ssc->clk);
  499. return ret;
  500. }
  501. ssc_p->initialized = 1;
  502. }
  503. /* set SSC clock mode register */
  504. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->cmr_div);
  505. /* set receive clock mode and format */
  506. ssc_writel(ssc_p->ssc->regs, RCMR, rcmr);
  507. ssc_writel(ssc_p->ssc->regs, RFMR, rfmr);
  508. /* set transmit clock mode and format */
  509. ssc_writel(ssc_p->ssc->regs, TCMR, tcmr);
  510. ssc_writel(ssc_p->ssc->regs, TFMR, tfmr);
  511. pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
  512. return 0;
  513. }
  514. static int atmel_ssc_prepare(struct snd_pcm_substream *substream,
  515. struct snd_soc_dai *dai)
  516. {
  517. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  518. struct atmel_pcm_dma_params *dma_params;
  519. int dir;
  520. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  521. dir = 0;
  522. else
  523. dir = 1;
  524. dma_params = ssc_p->dma_params[dir];
  525. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable);
  526. pr_debug("%s enabled SSC_SR=0x%08x\n",
  527. dir ? "receive" : "transmit",
  528. ssc_readl(ssc_p->ssc->regs, SR));
  529. return 0;
  530. }
  531. #ifdef CONFIG_PM
  532. static int atmel_ssc_suspend(struct snd_soc_dai *cpu_dai)
  533. {
  534. struct atmel_ssc_info *ssc_p;
  535. if (!cpu_dai->active)
  536. return 0;
  537. ssc_p = &ssc_info[cpu_dai->id];
  538. /* Save the status register before disabling transmit and receive */
  539. ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR);
  540. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS));
  541. /* Save the current interrupt mask, then disable unmasked interrupts */
  542. ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR);
  543. ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr);
  544. ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR);
  545. ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR);
  546. ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR);
  547. ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR);
  548. ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR);
  549. return 0;
  550. }
  551. static int atmel_ssc_resume(struct snd_soc_dai *cpu_dai)
  552. {
  553. struct atmel_ssc_info *ssc_p;
  554. u32 cr;
  555. if (!cpu_dai->active)
  556. return 0;
  557. ssc_p = &ssc_info[cpu_dai->id];
  558. /* restore SSC register settings */
  559. ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr);
  560. ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr);
  561. ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr);
  562. ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr);
  563. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr);
  564. /* re-enable interrupts */
  565. ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr);
  566. /* Re-enable receive and transmit as appropriate */
  567. cr = 0;
  568. cr |=
  569. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0;
  570. cr |=
  571. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0;
  572. ssc_writel(ssc_p->ssc->regs, CR, cr);
  573. return 0;
  574. }
  575. #else /* CONFIG_PM */
  576. # define atmel_ssc_suspend NULL
  577. # define atmel_ssc_resume NULL
  578. #endif /* CONFIG_PM */
  579. static int atmel_ssc_probe(struct snd_soc_dai *dai)
  580. {
  581. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  582. int ret = 0;
  583. snd_soc_dai_set_drvdata(dai, ssc_p);
  584. /*
  585. * Request SSC device
  586. */
  587. ssc_p->ssc = ssc_request(dai->id);
  588. if (IS_ERR(ssc_p->ssc)) {
  589. printk(KERN_ERR "ASoC: Failed to request SSC %d\n", dai->id);
  590. ret = PTR_ERR(ssc_p->ssc);
  591. }
  592. return ret;
  593. }
  594. static int atmel_ssc_remove(struct snd_soc_dai *dai)
  595. {
  596. struct atmel_ssc_info *ssc_p = snd_soc_dai_get_drvdata(dai);
  597. ssc_free(ssc_p->ssc);
  598. return 0;
  599. }
  600. #define ATMEL_SSC_RATES (SNDRV_PCM_RATE_8000_96000)
  601. #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
  602. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  603. static const struct snd_soc_dai_ops atmel_ssc_dai_ops = {
  604. .startup = atmel_ssc_startup,
  605. .shutdown = atmel_ssc_shutdown,
  606. .prepare = atmel_ssc_prepare,
  607. .hw_params = atmel_ssc_hw_params,
  608. .set_fmt = atmel_ssc_set_dai_fmt,
  609. .set_clkdiv = atmel_ssc_set_dai_clkdiv,
  610. };
  611. static struct snd_soc_dai_driver atmel_ssc_dai[NUM_SSC_DEVICES] = {
  612. {
  613. .name = "atmel-ssc-dai.0",
  614. .probe = atmel_ssc_probe,
  615. .remove = atmel_ssc_remove,
  616. .suspend = atmel_ssc_suspend,
  617. .resume = atmel_ssc_resume,
  618. .playback = {
  619. .channels_min = 1,
  620. .channels_max = 2,
  621. .rates = ATMEL_SSC_RATES,
  622. .formats = ATMEL_SSC_FORMATS,},
  623. .capture = {
  624. .channels_min = 1,
  625. .channels_max = 2,
  626. .rates = ATMEL_SSC_RATES,
  627. .formats = ATMEL_SSC_FORMATS,},
  628. .ops = &atmel_ssc_dai_ops,
  629. },
  630. #if NUM_SSC_DEVICES == 3
  631. {
  632. .name = "atmel-ssc-dai.1",
  633. .probe = atmel_ssc_probe,
  634. .remove = atmel_ssc_remove,
  635. .suspend = atmel_ssc_suspend,
  636. .resume = atmel_ssc_resume,
  637. .playback = {
  638. .channels_min = 1,
  639. .channels_max = 2,
  640. .rates = ATMEL_SSC_RATES,
  641. .formats = ATMEL_SSC_FORMATS,},
  642. .capture = {
  643. .channels_min = 1,
  644. .channels_max = 2,
  645. .rates = ATMEL_SSC_RATES,
  646. .formats = ATMEL_SSC_FORMATS,},
  647. .ops = &atmel_ssc_dai_ops,
  648. },
  649. {
  650. .name = "atmel-ssc-dai.2",
  651. .probe = atmel_ssc_probe,
  652. .remove = atmel_ssc_remove,
  653. .suspend = atmel_ssc_suspend,
  654. .resume = atmel_ssc_resume,
  655. .playback = {
  656. .channels_min = 1,
  657. .channels_max = 2,
  658. .rates = ATMEL_SSC_RATES,
  659. .formats = ATMEL_SSC_FORMATS,},
  660. .capture = {
  661. .channels_min = 1,
  662. .channels_max = 2,
  663. .rates = ATMEL_SSC_RATES,
  664. .formats = ATMEL_SSC_FORMATS,},
  665. .ops = &atmel_ssc_dai_ops,
  666. },
  667. #endif
  668. };
  669. static __devinit int asoc_ssc_probe(struct platform_device *pdev)
  670. {
  671. BUG_ON(pdev->id < 0);
  672. BUG_ON(pdev->id >= ARRAY_SIZE(atmel_ssc_dai));
  673. return snd_soc_register_dai(&pdev->dev, &atmel_ssc_dai[pdev->id]);
  674. }
  675. static int __devexit asoc_ssc_remove(struct platform_device *pdev)
  676. {
  677. snd_soc_unregister_dai(&pdev->dev);
  678. return 0;
  679. }
  680. static struct platform_driver asoc_ssc_driver = {
  681. .driver = {
  682. .name = "atmel-ssc-dai",
  683. .owner = THIS_MODULE,
  684. },
  685. .probe = asoc_ssc_probe,
  686. .remove = __devexit_p(asoc_ssc_remove),
  687. };
  688. /**
  689. * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
  690. */
  691. int atmel_ssc_set_audio(int ssc_id)
  692. {
  693. struct ssc_device *ssc;
  694. static struct platform_device *dma_pdev;
  695. struct platform_device *ssc_pdev;
  696. int ret;
  697. if (ssc_id < 0 || ssc_id >= ARRAY_SIZE(atmel_ssc_dai))
  698. return -EINVAL;
  699. /* Allocate a dummy device for DMA if we don't have one already */
  700. if (!dma_pdev) {
  701. dma_pdev = platform_device_alloc("atmel-pcm-audio", -1);
  702. if (!dma_pdev)
  703. return -ENOMEM;
  704. ret = platform_device_add(dma_pdev);
  705. if (ret < 0) {
  706. platform_device_put(dma_pdev);
  707. dma_pdev = NULL;
  708. return ret;
  709. }
  710. }
  711. ssc_pdev = platform_device_alloc("atmel-ssc-dai", ssc_id);
  712. if (!ssc_pdev)
  713. return -ENOMEM;
  714. /* If we can grab the SSC briefly to parent the DAI device off it */
  715. ssc = ssc_request(ssc_id);
  716. if (IS_ERR(ssc))
  717. pr_warn("Unable to parent ASoC SSC DAI on SSC: %ld\n",
  718. PTR_ERR(ssc));
  719. else {
  720. ssc_pdev->dev.parent = &(ssc->pdev->dev);
  721. ssc_free(ssc);
  722. }
  723. ret = platform_device_add(ssc_pdev);
  724. if (ret < 0)
  725. platform_device_put(ssc_pdev);
  726. return ret;
  727. }
  728. EXPORT_SYMBOL_GPL(atmel_ssc_set_audio);
  729. module_platform_driver(asoc_ssc_driver);
  730. /* Module information */
  731. MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
  732. MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
  733. MODULE_LICENSE("GPL");