atmel_ssc_dai.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 "atmel-pcm.h"
  43. #include "atmel_ssc_dai.h"
  44. #define NUM_SSC_DEVICES 3
  45. /*
  46. * SSC PDC registers required by the PCM DMA engine.
  47. */
  48. static struct atmel_pdc_regs pdc_tx_reg = {
  49. .xpr = ATMEL_PDC_TPR,
  50. .xcr = ATMEL_PDC_TCR,
  51. .xnpr = ATMEL_PDC_TNPR,
  52. .xncr = ATMEL_PDC_TNCR,
  53. };
  54. static struct atmel_pdc_regs pdc_rx_reg = {
  55. .xpr = ATMEL_PDC_RPR,
  56. .xcr = ATMEL_PDC_RCR,
  57. .xnpr = ATMEL_PDC_RNPR,
  58. .xncr = ATMEL_PDC_RNCR,
  59. };
  60. /*
  61. * SSC & PDC status bits for transmit and receive.
  62. */
  63. static struct atmel_ssc_mask ssc_tx_mask = {
  64. .ssc_enable = SSC_BIT(CR_TXEN),
  65. .ssc_disable = SSC_BIT(CR_TXDIS),
  66. .ssc_endx = SSC_BIT(SR_ENDTX),
  67. .ssc_endbuf = SSC_BIT(SR_TXBUFE),
  68. .ssc_error = SSC_BIT(SR_OVRUN),
  69. .pdc_enable = ATMEL_PDC_TXTEN,
  70. .pdc_disable = ATMEL_PDC_TXTDIS,
  71. };
  72. static struct atmel_ssc_mask ssc_rx_mask = {
  73. .ssc_enable = SSC_BIT(CR_RXEN),
  74. .ssc_disable = SSC_BIT(CR_RXDIS),
  75. .ssc_endx = SSC_BIT(SR_ENDRX),
  76. .ssc_endbuf = SSC_BIT(SR_RXBUFF),
  77. .ssc_error = SSC_BIT(SR_OVRUN),
  78. .pdc_enable = ATMEL_PDC_RXTEN,
  79. .pdc_disable = ATMEL_PDC_RXTDIS,
  80. };
  81. /*
  82. * DMA parameters.
  83. */
  84. static struct atmel_pcm_dma_params ssc_dma_params[NUM_SSC_DEVICES][2] = {
  85. {{
  86. .name = "SSC0 PCM out",
  87. .pdc = &pdc_tx_reg,
  88. .mask = &ssc_tx_mask,
  89. },
  90. {
  91. .name = "SSC0 PCM in",
  92. .pdc = &pdc_rx_reg,
  93. .mask = &ssc_rx_mask,
  94. } },
  95. {{
  96. .name = "SSC1 PCM out",
  97. .pdc = &pdc_tx_reg,
  98. .mask = &ssc_tx_mask,
  99. },
  100. {
  101. .name = "SSC1 PCM in",
  102. .pdc = &pdc_rx_reg,
  103. .mask = &ssc_rx_mask,
  104. } },
  105. {{
  106. .name = "SSC2 PCM out",
  107. .pdc = &pdc_tx_reg,
  108. .mask = &ssc_tx_mask,
  109. },
  110. {
  111. .name = "SSC2 PCM in",
  112. .pdc = &pdc_rx_reg,
  113. .mask = &ssc_rx_mask,
  114. } },
  115. };
  116. static struct atmel_ssc_info ssc_info[NUM_SSC_DEVICES] = {
  117. {
  118. .name = "ssc0",
  119. .lock = __SPIN_LOCK_UNLOCKED(ssc_info[0].lock),
  120. .dir_mask = SSC_DIR_MASK_UNUSED,
  121. .initialized = 0,
  122. },
  123. {
  124. .name = "ssc1",
  125. .lock = __SPIN_LOCK_UNLOCKED(ssc_info[1].lock),
  126. .dir_mask = SSC_DIR_MASK_UNUSED,
  127. .initialized = 0,
  128. },
  129. {
  130. .name = "ssc2",
  131. .lock = __SPIN_LOCK_UNLOCKED(ssc_info[2].lock),
  132. .dir_mask = SSC_DIR_MASK_UNUSED,
  133. .initialized = 0,
  134. },
  135. };
  136. /*
  137. * SSC interrupt handler. Passes PDC interrupts to the DMA
  138. * interrupt handler in the PCM driver.
  139. */
  140. static irqreturn_t atmel_ssc_interrupt(int irq, void *dev_id)
  141. {
  142. struct atmel_ssc_info *ssc_p = dev_id;
  143. struct atmel_pcm_dma_params *dma_params;
  144. u32 ssc_sr;
  145. u32 ssc_substream_mask;
  146. int i;
  147. ssc_sr = (unsigned long)ssc_readl(ssc_p->ssc->regs, SR)
  148. & (unsigned long)ssc_readl(ssc_p->ssc->regs, IMR);
  149. /*
  150. * Loop through the substreams attached to this SSC. If
  151. * a DMA-related interrupt occurred on that substream, call
  152. * the DMA interrupt handler function, if one has been
  153. * registered in the dma_params structure by the PCM driver.
  154. */
  155. for (i = 0; i < ARRAY_SIZE(ssc_p->dma_params); i++) {
  156. dma_params = ssc_p->dma_params[i];
  157. if ((dma_params != NULL) &&
  158. (dma_params->dma_intr_handler != NULL)) {
  159. ssc_substream_mask = (dma_params->mask->ssc_endx |
  160. dma_params->mask->ssc_endbuf);
  161. if (ssc_sr & ssc_substream_mask) {
  162. dma_params->dma_intr_handler(ssc_sr,
  163. dma_params->
  164. substream);
  165. }
  166. }
  167. }
  168. return IRQ_HANDLED;
  169. }
  170. /*
  171. * When the bit clock is input, limit the maximum rate according to the
  172. * Serial Clock Ratio Considerations section from the SSC documentation:
  173. *
  174. * The Transmitter and the Receiver can be programmed to operate
  175. * with the clock signals provided on either the TK or RK pins.
  176. * This allows the SSC to support many slave-mode data transfers.
  177. * In this case, the maximum clock speed allowed on the RK pin is:
  178. * - Peripheral clock divided by 2 if Receiver Frame Synchro is input
  179. * - Peripheral clock divided by 3 if Receiver Frame Synchro is output
  180. * In addition, the maximum clock speed allowed on the TK pin is:
  181. * - Peripheral clock divided by 6 if Transmit Frame Synchro is input
  182. * - Peripheral clock divided by 2 if Transmit Frame Synchro is output
  183. *
  184. * When the bit clock is output, limit the rate according to the
  185. * SSC divider restrictions.
  186. */
  187. static int atmel_ssc_hw_rule_rate(struct snd_pcm_hw_params *params,
  188. struct snd_pcm_hw_rule *rule)
  189. {
  190. struct atmel_ssc_info *ssc_p = rule->private;
  191. struct ssc_device *ssc = ssc_p->ssc;
  192. struct snd_interval *i = hw_param_interval(params, rule->var);
  193. struct snd_interval t;
  194. struct snd_ratnum r = {
  195. .den_min = 1,
  196. .den_max = 4095,
  197. .den_step = 1,
  198. };
  199. unsigned int num = 0, den = 0;
  200. int frame_size;
  201. int mck_div = 2;
  202. int ret;
  203. frame_size = snd_soc_params_to_frame_size(params);
  204. if (frame_size < 0)
  205. return frame_size;
  206. switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) {
  207. case SND_SOC_DAIFMT_CBM_CFS:
  208. if ((ssc_p->dir_mask & SSC_DIR_MASK_CAPTURE)
  209. && ssc->clk_from_rk_pin)
  210. /* Receiver Frame Synchro (i.e. capture)
  211. * is output (format is _CFS) and the RK pin
  212. * is used for input (format is _CBM_).
  213. */
  214. mck_div = 3;
  215. break;
  216. case SND_SOC_DAIFMT_CBM_CFM:
  217. if ((ssc_p->dir_mask & SSC_DIR_MASK_PLAYBACK)
  218. && !ssc->clk_from_rk_pin)
  219. /* Transmit Frame Synchro (i.e. playback)
  220. * is input (format is _CFM) and the TK pin
  221. * is used for input (format _CBM_ but not
  222. * using the RK pin).
  223. */
  224. mck_div = 6;
  225. break;
  226. }
  227. switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) {
  228. case SND_SOC_DAIFMT_CBS_CFS:
  229. r.num = ssc_p->mck_rate / mck_div / frame_size;
  230. ret = snd_interval_ratnum(i, 1, &r, &num, &den);
  231. if (ret >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  232. params->rate_num = num;
  233. params->rate_den = den;
  234. }
  235. break;
  236. case SND_SOC_DAIFMT_CBM_CFS:
  237. case SND_SOC_DAIFMT_CBM_CFM:
  238. t.min = 8000;
  239. t.max = ssc_p->mck_rate / mck_div / frame_size;
  240. t.openmin = t.openmax = 0;
  241. t.integer = 0;
  242. ret = snd_interval_refine(i, &t);
  243. break;
  244. default:
  245. ret = -EINVAL;
  246. break;
  247. }
  248. return ret;
  249. }
  250. /*-------------------------------------------------------------------------*\
  251. * DAI functions
  252. \*-------------------------------------------------------------------------*/
  253. /*
  254. * Startup. Only that one substream allowed in each direction.
  255. */
  256. static int atmel_ssc_startup(struct snd_pcm_substream *substream,
  257. struct snd_soc_dai *dai)
  258. {
  259. struct platform_device *pdev = to_platform_device(dai->dev);
  260. struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
  261. struct atmel_pcm_dma_params *dma_params;
  262. int dir, dir_mask;
  263. int ret;
  264. pr_debug("atmel_ssc_startup: SSC_SR=0x%x\n",
  265. ssc_readl(ssc_p->ssc->regs, SR));
  266. /* Enable PMC peripheral clock for this SSC */
  267. pr_debug("atmel_ssc_dai: Starting clock\n");
  268. clk_enable(ssc_p->ssc->clk);
  269. ssc_p->mck_rate = clk_get_rate(ssc_p->ssc->clk);
  270. /* Reset the SSC unless initialized to keep it in a clean state */
  271. if (!ssc_p->initialized)
  272. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
  273. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  274. dir = 0;
  275. dir_mask = SSC_DIR_MASK_PLAYBACK;
  276. } else {
  277. dir = 1;
  278. dir_mask = SSC_DIR_MASK_CAPTURE;
  279. }
  280. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  281. SNDRV_PCM_HW_PARAM_RATE,
  282. atmel_ssc_hw_rule_rate,
  283. ssc_p,
  284. SNDRV_PCM_HW_PARAM_FRAME_BITS,
  285. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  286. if (ret < 0) {
  287. dev_err(dai->dev, "Failed to specify rate rule: %d\n", ret);
  288. return ret;
  289. }
  290. dma_params = &ssc_dma_params[pdev->id][dir];
  291. dma_params->ssc = ssc_p->ssc;
  292. dma_params->substream = substream;
  293. ssc_p->dma_params[dir] = dma_params;
  294. snd_soc_dai_set_dma_data(dai, substream, dma_params);
  295. spin_lock_irq(&ssc_p->lock);
  296. if (ssc_p->dir_mask & dir_mask) {
  297. spin_unlock_irq(&ssc_p->lock);
  298. return -EBUSY;
  299. }
  300. ssc_p->dir_mask |= dir_mask;
  301. spin_unlock_irq(&ssc_p->lock);
  302. return 0;
  303. }
  304. /*
  305. * Shutdown. Clear DMA parameters and shutdown the SSC if there
  306. * are no other substreams open.
  307. */
  308. static void atmel_ssc_shutdown(struct snd_pcm_substream *substream,
  309. struct snd_soc_dai *dai)
  310. {
  311. struct platform_device *pdev = to_platform_device(dai->dev);
  312. struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
  313. struct atmel_pcm_dma_params *dma_params;
  314. int dir, dir_mask;
  315. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  316. dir = 0;
  317. else
  318. dir = 1;
  319. dma_params = ssc_p->dma_params[dir];
  320. if (dma_params != NULL) {
  321. dma_params->ssc = NULL;
  322. dma_params->substream = NULL;
  323. ssc_p->dma_params[dir] = NULL;
  324. }
  325. dir_mask = 1 << dir;
  326. spin_lock_irq(&ssc_p->lock);
  327. ssc_p->dir_mask &= ~dir_mask;
  328. if (!ssc_p->dir_mask) {
  329. if (ssc_p->initialized) {
  330. free_irq(ssc_p->ssc->irq, ssc_p);
  331. ssc_p->initialized = 0;
  332. }
  333. /* Reset the SSC */
  334. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
  335. /* Clear the SSC dividers */
  336. ssc_p->cmr_div = ssc_p->tcmr_period = ssc_p->rcmr_period = 0;
  337. }
  338. spin_unlock_irq(&ssc_p->lock);
  339. /* Shutdown the SSC clock. */
  340. pr_debug("atmel_ssc_dai: Stopping clock\n");
  341. clk_disable(ssc_p->ssc->clk);
  342. }
  343. /*
  344. * Record the DAI format for use in hw_params().
  345. */
  346. static int atmel_ssc_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  347. unsigned int fmt)
  348. {
  349. struct platform_device *pdev = to_platform_device(cpu_dai->dev);
  350. struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
  351. ssc_p->daifmt = fmt;
  352. return 0;
  353. }
  354. /*
  355. * Record SSC clock dividers for use in hw_params().
  356. */
  357. static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
  358. int div_id, int div)
  359. {
  360. struct platform_device *pdev = to_platform_device(cpu_dai->dev);
  361. struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
  362. switch (div_id) {
  363. case ATMEL_SSC_CMR_DIV:
  364. /*
  365. * The same master clock divider is used for both
  366. * transmit and receive, so if a value has already
  367. * been set, it must match this value.
  368. */
  369. if (ssc_p->dir_mask !=
  370. (SSC_DIR_MASK_PLAYBACK | SSC_DIR_MASK_CAPTURE))
  371. ssc_p->cmr_div = div;
  372. else if (ssc_p->cmr_div == 0)
  373. ssc_p->cmr_div = div;
  374. else
  375. if (div != ssc_p->cmr_div)
  376. return -EBUSY;
  377. break;
  378. case ATMEL_SSC_TCMR_PERIOD:
  379. ssc_p->tcmr_period = div;
  380. break;
  381. case ATMEL_SSC_RCMR_PERIOD:
  382. ssc_p->rcmr_period = div;
  383. break;
  384. default:
  385. return -EINVAL;
  386. }
  387. return 0;
  388. }
  389. /*
  390. * Configure the SSC.
  391. */
  392. static int atmel_ssc_hw_params(struct snd_pcm_substream *substream,
  393. struct snd_pcm_hw_params *params,
  394. struct snd_soc_dai *dai)
  395. {
  396. struct platform_device *pdev = to_platform_device(dai->dev);
  397. int id = pdev->id;
  398. struct atmel_ssc_info *ssc_p = &ssc_info[id];
  399. struct ssc_device *ssc = ssc_p->ssc;
  400. struct atmel_pcm_dma_params *dma_params;
  401. int dir, channels, bits;
  402. u32 tfmr, rfmr, tcmr, rcmr;
  403. int ret;
  404. int fslen, fslen_ext;
  405. /*
  406. * Currently, there is only one set of dma params for
  407. * each direction. If more are added, this code will
  408. * have to be changed to select the proper set.
  409. */
  410. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  411. dir = 0;
  412. else
  413. dir = 1;
  414. dma_params = ssc_p->dma_params[dir];
  415. channels = params_channels(params);
  416. /*
  417. * Determine sample size in bits and the PDC increment.
  418. */
  419. switch (params_format(params)) {
  420. case SNDRV_PCM_FORMAT_S8:
  421. bits = 8;
  422. dma_params->pdc_xfer_size = 1;
  423. break;
  424. case SNDRV_PCM_FORMAT_S16_LE:
  425. bits = 16;
  426. dma_params->pdc_xfer_size = 2;
  427. break;
  428. case SNDRV_PCM_FORMAT_S24_LE:
  429. bits = 24;
  430. dma_params->pdc_xfer_size = 4;
  431. break;
  432. case SNDRV_PCM_FORMAT_S32_LE:
  433. bits = 32;
  434. dma_params->pdc_xfer_size = 4;
  435. break;
  436. default:
  437. printk(KERN_WARNING "atmel_ssc_dai: unsupported PCM format");
  438. return -EINVAL;
  439. }
  440. /*
  441. * Compute SSC register settings.
  442. */
  443. switch (ssc_p->daifmt
  444. & (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
  445. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
  446. /*
  447. * I2S format, SSC provides BCLK and LRC clocks.
  448. *
  449. * The SSC transmit and receive clocks are generated
  450. * from the MCK divider, and the BCLK signal
  451. * is output on the SSC TK line.
  452. */
  453. if (bits > 16 && !ssc->pdata->has_fslen_ext) {
  454. dev_err(dai->dev,
  455. "sample size %d is too large for SSC device\n",
  456. bits);
  457. return -EINVAL;
  458. }
  459. fslen_ext = (bits - 1) / 16;
  460. fslen = (bits - 1) % 16;
  461. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  462. | SSC_BF(RCMR_STTDLY, START_DELAY)
  463. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  464. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  465. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  466. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  467. rfmr = SSC_BF(RFMR_FSLEN_EXT, fslen_ext)
  468. | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  469. | SSC_BF(RFMR_FSOS, SSC_FSOS_NEGATIVE)
  470. | SSC_BF(RFMR_FSLEN, fslen)
  471. | SSC_BF(RFMR_DATNB, (channels - 1))
  472. | SSC_BIT(RFMR_MSBF)
  473. | SSC_BF(RFMR_LOOP, 0)
  474. | SSC_BF(RFMR_DATLEN, (bits - 1));
  475. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  476. | SSC_BF(TCMR_STTDLY, START_DELAY)
  477. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  478. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  479. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  480. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  481. tfmr = SSC_BF(TFMR_FSLEN_EXT, fslen_ext)
  482. | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  483. | SSC_BF(TFMR_FSDEN, 0)
  484. | SSC_BF(TFMR_FSOS, SSC_FSOS_NEGATIVE)
  485. | SSC_BF(TFMR_FSLEN, fslen)
  486. | SSC_BF(TFMR_DATNB, (channels - 1))
  487. | SSC_BIT(TFMR_MSBF)
  488. | SSC_BF(TFMR_DATDEF, 0)
  489. | SSC_BF(TFMR_DATLEN, (bits - 1));
  490. break;
  491. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
  492. /* I2S format, CODEC supplies BCLK and LRC clocks. */
  493. rcmr = SSC_BF(RCMR_PERIOD, 0)
  494. | SSC_BF(RCMR_STTDLY, START_DELAY)
  495. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  496. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  497. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  498. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  499. SSC_CKS_PIN : SSC_CKS_CLOCK);
  500. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  501. | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
  502. | SSC_BF(RFMR_FSLEN, 0)
  503. | SSC_BF(RFMR_DATNB, (channels - 1))
  504. | SSC_BIT(RFMR_MSBF)
  505. | SSC_BF(RFMR_LOOP, 0)
  506. | SSC_BF(RFMR_DATLEN, (bits - 1));
  507. tcmr = SSC_BF(TCMR_PERIOD, 0)
  508. | SSC_BF(TCMR_STTDLY, START_DELAY)
  509. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  510. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  511. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  512. | SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ?
  513. SSC_CKS_CLOCK : SSC_CKS_PIN);
  514. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  515. | SSC_BF(TFMR_FSDEN, 0)
  516. | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
  517. | SSC_BF(TFMR_FSLEN, 0)
  518. | SSC_BF(TFMR_DATNB, (channels - 1))
  519. | SSC_BIT(TFMR_MSBF)
  520. | SSC_BF(TFMR_DATDEF, 0)
  521. | SSC_BF(TFMR_DATLEN, (bits - 1));
  522. break;
  523. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFS:
  524. /* I2S format, CODEC supplies BCLK, SSC supplies LRCLK. */
  525. if (bits > 16 && !ssc->pdata->has_fslen_ext) {
  526. dev_err(dai->dev,
  527. "sample size %d is too large for SSC device\n",
  528. bits);
  529. return -EINVAL;
  530. }
  531. fslen_ext = (bits - 1) / 16;
  532. fslen = (bits - 1) % 16;
  533. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  534. | SSC_BF(RCMR_STTDLY, START_DELAY)
  535. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  536. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  537. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  538. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  539. SSC_CKS_PIN : SSC_CKS_CLOCK);
  540. rfmr = SSC_BF(RFMR_FSLEN_EXT, fslen_ext)
  541. | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  542. | SSC_BF(RFMR_FSOS, SSC_FSOS_NEGATIVE)
  543. | SSC_BF(RFMR_FSLEN, fslen)
  544. | SSC_BF(RFMR_DATNB, (channels - 1))
  545. | SSC_BIT(RFMR_MSBF)
  546. | SSC_BF(RFMR_LOOP, 0)
  547. | SSC_BF(RFMR_DATLEN, (bits - 1));
  548. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  549. | SSC_BF(TCMR_STTDLY, START_DELAY)
  550. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  551. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  552. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  553. | SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ?
  554. SSC_CKS_CLOCK : SSC_CKS_PIN);
  555. tfmr = SSC_BF(TFMR_FSLEN_EXT, fslen_ext)
  556. | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_NEGATIVE)
  557. | SSC_BF(TFMR_FSDEN, 0)
  558. | SSC_BF(TFMR_FSOS, SSC_FSOS_NEGATIVE)
  559. | SSC_BF(TFMR_FSLEN, fslen)
  560. | SSC_BF(TFMR_DATNB, (channels - 1))
  561. | SSC_BIT(TFMR_MSBF)
  562. | SSC_BF(TFMR_DATDEF, 0)
  563. | SSC_BF(TFMR_DATLEN, (bits - 1));
  564. break;
  565. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
  566. /*
  567. * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
  568. *
  569. * The SSC transmit and receive clocks are generated from the
  570. * MCK divider, and the BCLK signal is output
  571. * on the SSC TK line.
  572. */
  573. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  574. | SSC_BF(RCMR_STTDLY, 1)
  575. | SSC_BF(RCMR_START, SSC_START_RISING_RF)
  576. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  577. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  578. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  579. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  580. | SSC_BF(RFMR_FSOS, SSC_FSOS_POSITIVE)
  581. | SSC_BF(RFMR_FSLEN, 0)
  582. | SSC_BF(RFMR_DATNB, (channels - 1))
  583. | SSC_BIT(RFMR_MSBF)
  584. | SSC_BF(RFMR_LOOP, 0)
  585. | SSC_BF(RFMR_DATLEN, (bits - 1));
  586. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  587. | SSC_BF(TCMR_STTDLY, 1)
  588. | SSC_BF(TCMR_START, SSC_START_RISING_RF)
  589. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  590. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  591. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  592. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  593. | SSC_BF(TFMR_FSDEN, 0)
  594. | SSC_BF(TFMR_FSOS, SSC_FSOS_POSITIVE)
  595. | SSC_BF(TFMR_FSLEN, 0)
  596. | SSC_BF(TFMR_DATNB, (channels - 1))
  597. | SSC_BIT(TFMR_MSBF)
  598. | SSC_BF(TFMR_DATDEF, 0)
  599. | SSC_BF(TFMR_DATLEN, (bits - 1));
  600. break;
  601. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
  602. /*
  603. * DSP/PCM Mode A format, CODEC supplies BCLK and LRC clocks.
  604. *
  605. * Data is transferred on first BCLK after LRC pulse rising
  606. * edge.If stereo, the right channel data is contiguous with
  607. * the left channel data.
  608. */
  609. rcmr = SSC_BF(RCMR_PERIOD, 0)
  610. | SSC_BF(RCMR_STTDLY, START_DELAY)
  611. | SSC_BF(RCMR_START, SSC_START_RISING_RF)
  612. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  613. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  614. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  615. SSC_CKS_PIN : SSC_CKS_CLOCK);
  616. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  617. | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
  618. | SSC_BF(RFMR_FSLEN, 0)
  619. | SSC_BF(RFMR_DATNB, (channels - 1))
  620. | SSC_BIT(RFMR_MSBF)
  621. | SSC_BF(RFMR_LOOP, 0)
  622. | SSC_BF(RFMR_DATLEN, (bits - 1));
  623. tcmr = SSC_BF(TCMR_PERIOD, 0)
  624. | SSC_BF(TCMR_STTDLY, START_DELAY)
  625. | SSC_BF(TCMR_START, SSC_START_RISING_RF)
  626. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  627. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  628. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  629. SSC_CKS_CLOCK : SSC_CKS_PIN);
  630. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  631. | SSC_BF(TFMR_FSDEN, 0)
  632. | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
  633. | SSC_BF(TFMR_FSLEN, 0)
  634. | SSC_BF(TFMR_DATNB, (channels - 1))
  635. | SSC_BIT(TFMR_MSBF)
  636. | SSC_BF(TFMR_DATDEF, 0)
  637. | SSC_BF(TFMR_DATLEN, (bits - 1));
  638. break;
  639. default:
  640. printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n",
  641. ssc_p->daifmt);
  642. return -EINVAL;
  643. }
  644. pr_debug("atmel_ssc_hw_params: "
  645. "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
  646. rcmr, rfmr, tcmr, tfmr);
  647. if (!ssc_p->initialized) {
  648. if (!ssc_p->ssc->pdata->use_dma) {
  649. ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0);
  650. ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0);
  651. ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0);
  652. ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0);
  653. ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0);
  654. ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0);
  655. ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0);
  656. ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0);
  657. }
  658. ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0,
  659. ssc_p->name, ssc_p);
  660. if (ret < 0) {
  661. printk(KERN_WARNING
  662. "atmel_ssc_dai: request_irq failure\n");
  663. pr_debug("Atmel_ssc_dai: Stoping clock\n");
  664. clk_disable(ssc_p->ssc->clk);
  665. return ret;
  666. }
  667. ssc_p->initialized = 1;
  668. }
  669. /* set SSC clock mode register */
  670. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->cmr_div);
  671. /* set receive clock mode and format */
  672. ssc_writel(ssc_p->ssc->regs, RCMR, rcmr);
  673. ssc_writel(ssc_p->ssc->regs, RFMR, rfmr);
  674. /* set transmit clock mode and format */
  675. ssc_writel(ssc_p->ssc->regs, TCMR, tcmr);
  676. ssc_writel(ssc_p->ssc->regs, TFMR, tfmr);
  677. pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
  678. return 0;
  679. }
  680. static int atmel_ssc_prepare(struct snd_pcm_substream *substream,
  681. struct snd_soc_dai *dai)
  682. {
  683. struct platform_device *pdev = to_platform_device(dai->dev);
  684. struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
  685. struct atmel_pcm_dma_params *dma_params;
  686. int dir;
  687. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  688. dir = 0;
  689. else
  690. dir = 1;
  691. dma_params = ssc_p->dma_params[dir];
  692. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
  693. ssc_writel(ssc_p->ssc->regs, IDR, dma_params->mask->ssc_error);
  694. pr_debug("%s enabled SSC_SR=0x%08x\n",
  695. dir ? "receive" : "transmit",
  696. ssc_readl(ssc_p->ssc->regs, SR));
  697. return 0;
  698. }
  699. static int atmel_ssc_trigger(struct snd_pcm_substream *substream,
  700. int cmd, struct snd_soc_dai *dai)
  701. {
  702. struct platform_device *pdev = to_platform_device(dai->dev);
  703. struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
  704. struct atmel_pcm_dma_params *dma_params;
  705. int dir;
  706. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  707. dir = 0;
  708. else
  709. dir = 1;
  710. dma_params = ssc_p->dma_params[dir];
  711. switch (cmd) {
  712. case SNDRV_PCM_TRIGGER_START:
  713. case SNDRV_PCM_TRIGGER_RESUME:
  714. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  715. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable);
  716. break;
  717. default:
  718. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
  719. break;
  720. }
  721. return 0;
  722. }
  723. #ifdef CONFIG_PM
  724. static int atmel_ssc_suspend(struct snd_soc_dai *cpu_dai)
  725. {
  726. struct atmel_ssc_info *ssc_p;
  727. struct platform_device *pdev = to_platform_device(cpu_dai->dev);
  728. if (!cpu_dai->active)
  729. return 0;
  730. ssc_p = &ssc_info[pdev->id];
  731. /* Save the status register before disabling transmit and receive */
  732. ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR);
  733. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS));
  734. /* Save the current interrupt mask, then disable unmasked interrupts */
  735. ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR);
  736. ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr);
  737. ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR);
  738. ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR);
  739. ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR);
  740. ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR);
  741. ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR);
  742. return 0;
  743. }
  744. static int atmel_ssc_resume(struct snd_soc_dai *cpu_dai)
  745. {
  746. struct atmel_ssc_info *ssc_p;
  747. struct platform_device *pdev = to_platform_device(cpu_dai->dev);
  748. u32 cr;
  749. if (!cpu_dai->active)
  750. return 0;
  751. ssc_p = &ssc_info[pdev->id];
  752. /* restore SSC register settings */
  753. ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr);
  754. ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr);
  755. ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr);
  756. ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr);
  757. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr);
  758. /* re-enable interrupts */
  759. ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr);
  760. /* Re-enable receive and transmit as appropriate */
  761. cr = 0;
  762. cr |=
  763. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0;
  764. cr |=
  765. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0;
  766. ssc_writel(ssc_p->ssc->regs, CR, cr);
  767. return 0;
  768. }
  769. #else /* CONFIG_PM */
  770. # define atmel_ssc_suspend NULL
  771. # define atmel_ssc_resume NULL
  772. #endif /* CONFIG_PM */
  773. #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
  774. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  775. static const struct snd_soc_dai_ops atmel_ssc_dai_ops = {
  776. .startup = atmel_ssc_startup,
  777. .shutdown = atmel_ssc_shutdown,
  778. .prepare = atmel_ssc_prepare,
  779. .trigger = atmel_ssc_trigger,
  780. .hw_params = atmel_ssc_hw_params,
  781. .set_fmt = atmel_ssc_set_dai_fmt,
  782. .set_clkdiv = atmel_ssc_set_dai_clkdiv,
  783. };
  784. static struct snd_soc_dai_driver atmel_ssc_dai = {
  785. .suspend = atmel_ssc_suspend,
  786. .resume = atmel_ssc_resume,
  787. .playback = {
  788. .channels_min = 1,
  789. .channels_max = 2,
  790. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  791. .rate_min = 8000,
  792. .rate_max = 384000,
  793. .formats = ATMEL_SSC_FORMATS,},
  794. .capture = {
  795. .channels_min = 1,
  796. .channels_max = 2,
  797. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  798. .rate_min = 8000,
  799. .rate_max = 384000,
  800. .formats = ATMEL_SSC_FORMATS,},
  801. .ops = &atmel_ssc_dai_ops,
  802. };
  803. static const struct snd_soc_component_driver atmel_ssc_component = {
  804. .name = "atmel-ssc",
  805. };
  806. static int asoc_ssc_init(struct device *dev)
  807. {
  808. struct platform_device *pdev = to_platform_device(dev);
  809. struct ssc_device *ssc = platform_get_drvdata(pdev);
  810. int ret;
  811. ret = snd_soc_register_component(dev, &atmel_ssc_component,
  812. &atmel_ssc_dai, 1);
  813. if (ret) {
  814. dev_err(dev, "Could not register DAI: %d\n", ret);
  815. goto err;
  816. }
  817. if (ssc->pdata->use_dma)
  818. ret = atmel_pcm_dma_platform_register(dev);
  819. else
  820. ret = atmel_pcm_pdc_platform_register(dev);
  821. if (ret) {
  822. dev_err(dev, "Could not register PCM: %d\n", ret);
  823. goto err_unregister_dai;
  824. }
  825. return 0;
  826. err_unregister_dai:
  827. snd_soc_unregister_component(dev);
  828. err:
  829. return ret;
  830. }
  831. static void asoc_ssc_exit(struct device *dev)
  832. {
  833. struct platform_device *pdev = to_platform_device(dev);
  834. struct ssc_device *ssc = platform_get_drvdata(pdev);
  835. if (ssc->pdata->use_dma)
  836. atmel_pcm_dma_platform_unregister(dev);
  837. else
  838. atmel_pcm_pdc_platform_unregister(dev);
  839. snd_soc_unregister_component(dev);
  840. }
  841. /**
  842. * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
  843. */
  844. int atmel_ssc_set_audio(int ssc_id)
  845. {
  846. struct ssc_device *ssc;
  847. int ret;
  848. /* If we can grab the SSC briefly to parent the DAI device off it */
  849. ssc = ssc_request(ssc_id);
  850. if (IS_ERR(ssc)) {
  851. pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n",
  852. PTR_ERR(ssc));
  853. return PTR_ERR(ssc);
  854. } else {
  855. ssc_info[ssc_id].ssc = ssc;
  856. }
  857. ret = asoc_ssc_init(&ssc->pdev->dev);
  858. return ret;
  859. }
  860. EXPORT_SYMBOL_GPL(atmel_ssc_set_audio);
  861. void atmel_ssc_put_audio(int ssc_id)
  862. {
  863. struct ssc_device *ssc = ssc_info[ssc_id].ssc;
  864. asoc_ssc_exit(&ssc->pdev->dev);
  865. ssc_free(ssc);
  866. }
  867. EXPORT_SYMBOL_GPL(atmel_ssc_put_audio);
  868. /* Module information */
  869. MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
  870. MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
  871. MODULE_LICENSE("GPL");