atmel_ssc_dai.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 start_event;
  300. int ret;
  301. /*
  302. * Currently, there is only one set of dma params for
  303. * each direction. If more are added, this code will
  304. * have to be changed to select the proper set.
  305. */
  306. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  307. dir = 0;
  308. else
  309. dir = 1;
  310. dma_params = &ssc_dma_params[id][dir];
  311. dma_params->ssc = ssc_p->ssc;
  312. dma_params->substream = substream;
  313. ssc_p->dma_params[dir] = dma_params;
  314. /*
  315. * The snd_soc_pcm_stream->dma_data field is only used to communicate
  316. * the appropriate DMA parameters to the pcm driver hw_params()
  317. * function. It should not be used for other purposes
  318. * as it is common to all substreams.
  319. */
  320. snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_params);
  321. channels = params_channels(params);
  322. /*
  323. * Determine sample size in bits and the PDC increment.
  324. */
  325. switch (params_format(params)) {
  326. case SNDRV_PCM_FORMAT_S8:
  327. bits = 8;
  328. dma_params->pdc_xfer_size = 1;
  329. break;
  330. case SNDRV_PCM_FORMAT_S16_LE:
  331. bits = 16;
  332. dma_params->pdc_xfer_size = 2;
  333. break;
  334. case SNDRV_PCM_FORMAT_S24_LE:
  335. bits = 24;
  336. dma_params->pdc_xfer_size = 4;
  337. break;
  338. case SNDRV_PCM_FORMAT_S32_LE:
  339. bits = 32;
  340. dma_params->pdc_xfer_size = 4;
  341. break;
  342. default:
  343. printk(KERN_WARNING "atmel_ssc_dai: unsupported PCM format");
  344. return -EINVAL;
  345. }
  346. /*
  347. * The SSC only supports up to 16-bit samples in I2S format, due
  348. * to the size of the Frame Mode Register FSLEN field.
  349. */
  350. if ((ssc_p->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_I2S
  351. && bits > 16) {
  352. printk(KERN_WARNING
  353. "atmel_ssc_dai: sample size %d"
  354. "is too large for I2S\n", bits);
  355. return -EINVAL;
  356. }
  357. /*
  358. * Compute SSC register settings.
  359. */
  360. switch (ssc_p->daifmt
  361. & (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
  362. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
  363. /*
  364. * I2S format, SSC provides BCLK and LRC clocks.
  365. *
  366. * The SSC transmit and receive clocks are generated
  367. * from the MCK divider, and the BCLK signal
  368. * is output on the SSC TK line.
  369. */
  370. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  371. | SSC_BF(RCMR_STTDLY, START_DELAY)
  372. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  373. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  374. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  375. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  376. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  377. | SSC_BF(RFMR_FSOS, SSC_FSOS_NEGATIVE)
  378. | SSC_BF(RFMR_FSLEN, (bits - 1))
  379. | SSC_BF(RFMR_DATNB, (channels - 1))
  380. | SSC_BIT(RFMR_MSBF)
  381. | SSC_BF(RFMR_LOOP, 0)
  382. | SSC_BF(RFMR_DATLEN, (bits - 1));
  383. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  384. | SSC_BF(TCMR_STTDLY, START_DELAY)
  385. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  386. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  387. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  388. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  389. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  390. | SSC_BF(TFMR_FSDEN, 0)
  391. | SSC_BF(TFMR_FSOS, SSC_FSOS_NEGATIVE)
  392. | SSC_BF(TFMR_FSLEN, (bits - 1))
  393. | SSC_BF(TFMR_DATNB, (channels - 1))
  394. | SSC_BIT(TFMR_MSBF)
  395. | SSC_BF(TFMR_DATDEF, 0)
  396. | SSC_BF(TFMR_DATLEN, (bits - 1));
  397. break;
  398. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
  399. /*
  400. * I2S format, CODEC supplies BCLK and LRC clocks.
  401. *
  402. * The SSC transmit clock is obtained from the BCLK signal on
  403. * on the TK line, and the SSC receive clock is
  404. * generated from the transmit clock.
  405. *
  406. * For single channel data, one sample is transferred
  407. * on the falling edge of the LRC clock.
  408. * For two channel data, one sample is
  409. * transferred on both edges of the LRC clock.
  410. */
  411. start_event = ((channels == 1)
  412. ? SSC_START_FALLING_RF
  413. : SSC_START_EDGE_RF);
  414. rcmr = SSC_BF(RCMR_PERIOD, 0)
  415. | SSC_BF(RCMR_STTDLY, START_DELAY)
  416. | SSC_BF(RCMR_START, start_event)
  417. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  418. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  419. | SSC_BF(RCMR_CKS, SSC_CKS_CLOCK);
  420. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  421. | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
  422. | SSC_BF(RFMR_FSLEN, 0)
  423. | SSC_BF(RFMR_DATNB, 0)
  424. | SSC_BIT(RFMR_MSBF)
  425. | SSC_BF(RFMR_LOOP, 0)
  426. | SSC_BF(RFMR_DATLEN, (bits - 1));
  427. tcmr = SSC_BF(TCMR_PERIOD, 0)
  428. | SSC_BF(TCMR_STTDLY, START_DELAY)
  429. | SSC_BF(TCMR_START, start_event)
  430. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  431. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  432. | SSC_BF(TCMR_CKS, SSC_CKS_PIN);
  433. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  434. | SSC_BF(TFMR_FSDEN, 0)
  435. | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
  436. | SSC_BF(TFMR_FSLEN, 0)
  437. | SSC_BF(TFMR_DATNB, 0)
  438. | SSC_BIT(TFMR_MSBF)
  439. | SSC_BF(TFMR_DATDEF, 0)
  440. | SSC_BF(TFMR_DATLEN, (bits - 1));
  441. break;
  442. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
  443. /*
  444. * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
  445. *
  446. * The SSC transmit and receive clocks are generated from the
  447. * MCK divider, and the BCLK signal is output
  448. * on the SSC TK line.
  449. */
  450. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  451. | SSC_BF(RCMR_STTDLY, 1)
  452. | SSC_BF(RCMR_START, SSC_START_RISING_RF)
  453. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  454. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  455. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  456. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  457. | SSC_BF(RFMR_FSOS, SSC_FSOS_POSITIVE)
  458. | SSC_BF(RFMR_FSLEN, 0)
  459. | SSC_BF(RFMR_DATNB, (channels - 1))
  460. | SSC_BIT(RFMR_MSBF)
  461. | SSC_BF(RFMR_LOOP, 0)
  462. | SSC_BF(RFMR_DATLEN, (bits - 1));
  463. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  464. | SSC_BF(TCMR_STTDLY, 1)
  465. | SSC_BF(TCMR_START, SSC_START_RISING_RF)
  466. | SSC_BF(TCMR_CKI, SSC_CKI_RISING)
  467. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  468. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  469. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  470. | SSC_BF(TFMR_FSDEN, 0)
  471. | SSC_BF(TFMR_FSOS, SSC_FSOS_POSITIVE)
  472. | SSC_BF(TFMR_FSLEN, 0)
  473. | SSC_BF(TFMR_DATNB, (channels - 1))
  474. | SSC_BIT(TFMR_MSBF)
  475. | SSC_BF(TFMR_DATDEF, 0)
  476. | SSC_BF(TFMR_DATLEN, (bits - 1));
  477. break;
  478. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
  479. default:
  480. printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n",
  481. ssc_p->daifmt);
  482. return -EINVAL;
  483. }
  484. pr_debug("atmel_ssc_hw_params: "
  485. "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
  486. rcmr, rfmr, tcmr, tfmr);
  487. if (!ssc_p->initialized) {
  488. /* Enable PMC peripheral clock for this SSC */
  489. pr_debug("atmel_ssc_dai: Starting clock\n");
  490. clk_enable(ssc_p->ssc->clk);
  491. /* Reset the SSC and its PDC registers */
  492. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
  493. ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0);
  494. ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0);
  495. ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0);
  496. ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0);
  497. ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0);
  498. ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0);
  499. ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0);
  500. ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0);
  501. ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0,
  502. ssc_p->name, ssc_p);
  503. if (ret < 0) {
  504. printk(KERN_WARNING
  505. "atmel_ssc_dai: request_irq failure\n");
  506. pr_debug("Atmel_ssc_dai: Stoping clock\n");
  507. clk_disable(ssc_p->ssc->clk);
  508. return ret;
  509. }
  510. ssc_p->initialized = 1;
  511. }
  512. /* set SSC clock mode register */
  513. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->cmr_div);
  514. /* set receive clock mode and format */
  515. ssc_writel(ssc_p->ssc->regs, RCMR, rcmr);
  516. ssc_writel(ssc_p->ssc->regs, RFMR, rfmr);
  517. /* set transmit clock mode and format */
  518. ssc_writel(ssc_p->ssc->regs, TCMR, tcmr);
  519. ssc_writel(ssc_p->ssc->regs, TFMR, tfmr);
  520. pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
  521. return 0;
  522. }
  523. static int atmel_ssc_prepare(struct snd_pcm_substream *substream,
  524. struct snd_soc_dai *dai)
  525. {
  526. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  527. struct atmel_pcm_dma_params *dma_params;
  528. int dir;
  529. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  530. dir = 0;
  531. else
  532. dir = 1;
  533. dma_params = ssc_p->dma_params[dir];
  534. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable);
  535. pr_debug("%s enabled SSC_SR=0x%08x\n",
  536. dir ? "receive" : "transmit",
  537. ssc_readl(ssc_p->ssc->regs, SR));
  538. return 0;
  539. }
  540. #ifdef CONFIG_PM
  541. static int atmel_ssc_suspend(struct snd_soc_dai *cpu_dai)
  542. {
  543. struct atmel_ssc_info *ssc_p;
  544. if (!cpu_dai->active)
  545. return 0;
  546. ssc_p = &ssc_info[cpu_dai->id];
  547. /* Save the status register before disabling transmit and receive */
  548. ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR);
  549. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS));
  550. /* Save the current interrupt mask, then disable unmasked interrupts */
  551. ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR);
  552. ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr);
  553. ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR);
  554. ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR);
  555. ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR);
  556. ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR);
  557. ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR);
  558. return 0;
  559. }
  560. static int atmel_ssc_resume(struct snd_soc_dai *cpu_dai)
  561. {
  562. struct atmel_ssc_info *ssc_p;
  563. u32 cr;
  564. if (!cpu_dai->active)
  565. return 0;
  566. ssc_p = &ssc_info[cpu_dai->id];
  567. /* restore SSC register settings */
  568. ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr);
  569. ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr);
  570. ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr);
  571. ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr);
  572. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr);
  573. /* re-enable interrupts */
  574. ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr);
  575. /* Re-enable receive and transmit as appropriate */
  576. cr = 0;
  577. cr |=
  578. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0;
  579. cr |=
  580. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0;
  581. ssc_writel(ssc_p->ssc->regs, CR, cr);
  582. return 0;
  583. }
  584. #else /* CONFIG_PM */
  585. # define atmel_ssc_suspend NULL
  586. # define atmel_ssc_resume NULL
  587. #endif /* CONFIG_PM */
  588. static int atmel_ssc_probe(struct snd_soc_dai *dai)
  589. {
  590. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  591. int ret = 0;
  592. snd_soc_dai_set_drvdata(dai, ssc_p);
  593. /*
  594. * Request SSC device
  595. */
  596. ssc_p->ssc = ssc_request(dai->id);
  597. if (IS_ERR(ssc_p->ssc)) {
  598. printk(KERN_ERR "ASoC: Failed to request SSC %d\n", dai->id);
  599. ret = PTR_ERR(ssc_p->ssc);
  600. }
  601. return ret;
  602. }
  603. static int atmel_ssc_remove(struct snd_soc_dai *dai)
  604. {
  605. struct atmel_ssc_info *ssc_p = snd_soc_dai_get_drvdata(dai);
  606. ssc_free(ssc_p->ssc);
  607. return 0;
  608. }
  609. #define ATMEL_SSC_RATES (SNDRV_PCM_RATE_8000_96000)
  610. #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
  611. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  612. static struct snd_soc_dai_ops atmel_ssc_dai_ops = {
  613. .startup = atmel_ssc_startup,
  614. .shutdown = atmel_ssc_shutdown,
  615. .prepare = atmel_ssc_prepare,
  616. .hw_params = atmel_ssc_hw_params,
  617. .set_fmt = atmel_ssc_set_dai_fmt,
  618. .set_clkdiv = atmel_ssc_set_dai_clkdiv,
  619. };
  620. static struct snd_soc_dai_driver atmel_ssc_dai[NUM_SSC_DEVICES] = {
  621. {
  622. .name = "atmel-ssc-dai.0",
  623. .probe = atmel_ssc_probe,
  624. .remove = atmel_ssc_remove,
  625. .suspend = atmel_ssc_suspend,
  626. .resume = atmel_ssc_resume,
  627. .playback = {
  628. .channels_min = 1,
  629. .channels_max = 2,
  630. .rates = ATMEL_SSC_RATES,
  631. .formats = ATMEL_SSC_FORMATS,},
  632. .capture = {
  633. .channels_min = 1,
  634. .channels_max = 2,
  635. .rates = ATMEL_SSC_RATES,
  636. .formats = ATMEL_SSC_FORMATS,},
  637. .ops = &atmel_ssc_dai_ops,
  638. },
  639. #if NUM_SSC_DEVICES == 3
  640. {
  641. .name = "atmel-ssc-dai.1",
  642. .probe = atmel_ssc_probe,
  643. .remove = atmel_ssc_remove,
  644. .suspend = atmel_ssc_suspend,
  645. .resume = atmel_ssc_resume,
  646. .playback = {
  647. .channels_min = 1,
  648. .channels_max = 2,
  649. .rates = ATMEL_SSC_RATES,
  650. .formats = ATMEL_SSC_FORMATS,},
  651. .capture = {
  652. .channels_min = 1,
  653. .channels_max = 2,
  654. .rates = ATMEL_SSC_RATES,
  655. .formats = ATMEL_SSC_FORMATS,},
  656. .ops = &atmel_ssc_dai_ops,
  657. },
  658. {
  659. .name = "atmel-ssc-dai.2",
  660. .probe = atmel_ssc_probe,
  661. .remove = atmel_ssc_remove,
  662. .suspend = atmel_ssc_suspend,
  663. .resume = atmel_ssc_resume,
  664. .playback = {
  665. .channels_min = 1,
  666. .channels_max = 2,
  667. .rates = ATMEL_SSC_RATES,
  668. .formats = ATMEL_SSC_FORMATS,},
  669. .capture = {
  670. .channels_min = 1,
  671. .channels_max = 2,
  672. .rates = ATMEL_SSC_RATES,
  673. .formats = ATMEL_SSC_FORMATS,},
  674. .ops = &atmel_ssc_dai_ops,
  675. },
  676. #endif
  677. };
  678. static __devinit int asoc_ssc_probe(struct platform_device *pdev)
  679. {
  680. BUG_ON(pdev->id < 0);
  681. BUG_ON(pdev->id >= ARRAY_SIZE(atmel_ssc_dai));
  682. return snd_soc_register_dai(&pdev->dev, &atmel_ssc_dai[pdev->id]);
  683. }
  684. static int __devexit asoc_ssc_remove(struct platform_device *pdev)
  685. {
  686. snd_soc_unregister_dai(&pdev->dev);
  687. return 0;
  688. }
  689. static struct platform_driver asoc_ssc_driver = {
  690. .driver = {
  691. .name = "atmel-ssc-dai",
  692. .owner = THIS_MODULE,
  693. },
  694. .probe = asoc_ssc_probe,
  695. .remove = __devexit_p(asoc_ssc_remove),
  696. };
  697. /**
  698. * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
  699. */
  700. int atmel_ssc_set_audio(int ssc_id)
  701. {
  702. struct ssc_device *ssc;
  703. static struct platform_device *dma_pdev;
  704. struct platform_device *ssc_pdev;
  705. int ret;
  706. if (ssc_id < 0 || ssc_id >= ARRAY_SIZE(atmel_ssc_dai))
  707. return -EINVAL;
  708. /* Allocate a dummy device for DMA if we don't have one already */
  709. if (!dma_pdev) {
  710. dma_pdev = platform_device_alloc("atmel-pcm-audio", -1);
  711. if (!dma_pdev)
  712. return -ENOMEM;
  713. ret = platform_device_add(dma_pdev);
  714. if (ret < 0) {
  715. platform_device_put(dma_pdev);
  716. dma_pdev = NULL;
  717. return ret;
  718. }
  719. }
  720. ssc_pdev = platform_device_alloc("atmel-ssc-dai", ssc_id);
  721. if (!ssc_pdev) {
  722. ssc_free(ssc);
  723. return -ENOMEM;
  724. }
  725. /* If we can grab the SSC briefly to parent the DAI device off it */
  726. ssc = ssc_request(ssc_id);
  727. if (IS_ERR(ssc))
  728. pr_warn("Unable to parent ASoC SSC DAI on SSC: %ld\n",
  729. PTR_ERR(ssc));
  730. else {
  731. ssc_pdev->dev.parent = &(ssc->pdev->dev);
  732. ssc_free(ssc);
  733. }
  734. ret = platform_device_add(ssc_pdev);
  735. if (ret < 0)
  736. platform_device_put(ssc_pdev);
  737. return ret;
  738. }
  739. EXPORT_SYMBOL_GPL(atmel_ssc_set_audio);
  740. static int __init snd_atmel_ssc_init(void)
  741. {
  742. return platform_driver_register(&asoc_ssc_driver);
  743. }
  744. module_init(snd_atmel_ssc_init);
  745. static void __exit snd_atmel_ssc_exit(void)
  746. {
  747. platform_driver_unregister(&asoc_ssc_driver);
  748. }
  749. module_exit(snd_atmel_ssc_exit);
  750. /* Module information */
  751. MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
  752. MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
  753. MODULE_LICENSE("GPL");