atmel_ssc_dai.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  260. struct atmel_pcm_dma_params *dma_params;
  261. int dir, dir_mask;
  262. int ret;
  263. pr_debug("atmel_ssc_startup: SSC_SR=0x%x\n",
  264. ssc_readl(ssc_p->ssc->regs, SR));
  265. /* Enable PMC peripheral clock for this SSC */
  266. pr_debug("atmel_ssc_dai: Starting clock\n");
  267. clk_enable(ssc_p->ssc->clk);
  268. ssc_p->mck_rate = clk_get_rate(ssc_p->ssc->clk);
  269. /* Reset the SSC unless initialized to keep it in a clean state */
  270. if (!ssc_p->initialized)
  271. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
  272. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  273. dir = 0;
  274. dir_mask = SSC_DIR_MASK_PLAYBACK;
  275. } else {
  276. dir = 1;
  277. dir_mask = SSC_DIR_MASK_CAPTURE;
  278. }
  279. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  280. SNDRV_PCM_HW_PARAM_RATE,
  281. atmel_ssc_hw_rule_rate,
  282. ssc_p,
  283. SNDRV_PCM_HW_PARAM_FRAME_BITS,
  284. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  285. if (ret < 0) {
  286. dev_err(dai->dev, "Failed to specify rate rule: %d\n", ret);
  287. return ret;
  288. }
  289. dma_params = &ssc_dma_params[dai->id][dir];
  290. dma_params->ssc = ssc_p->ssc;
  291. dma_params->substream = substream;
  292. ssc_p->dma_params[dir] = dma_params;
  293. snd_soc_dai_set_dma_data(dai, substream, dma_params);
  294. spin_lock_irq(&ssc_p->lock);
  295. if (ssc_p->dir_mask & dir_mask) {
  296. spin_unlock_irq(&ssc_p->lock);
  297. return -EBUSY;
  298. }
  299. ssc_p->dir_mask |= dir_mask;
  300. spin_unlock_irq(&ssc_p->lock);
  301. return 0;
  302. }
  303. /*
  304. * Shutdown. Clear DMA parameters and shutdown the SSC if there
  305. * are no other substreams open.
  306. */
  307. static void atmel_ssc_shutdown(struct snd_pcm_substream *substream,
  308. struct snd_soc_dai *dai)
  309. {
  310. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  311. struct atmel_pcm_dma_params *dma_params;
  312. int dir, dir_mask;
  313. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  314. dir = 0;
  315. else
  316. dir = 1;
  317. dma_params = ssc_p->dma_params[dir];
  318. if (dma_params != NULL) {
  319. dma_params->ssc = NULL;
  320. dma_params->substream = NULL;
  321. ssc_p->dma_params[dir] = NULL;
  322. }
  323. dir_mask = 1 << dir;
  324. spin_lock_irq(&ssc_p->lock);
  325. ssc_p->dir_mask &= ~dir_mask;
  326. if (!ssc_p->dir_mask) {
  327. if (ssc_p->initialized) {
  328. free_irq(ssc_p->ssc->irq, ssc_p);
  329. ssc_p->initialized = 0;
  330. }
  331. /* Reset the SSC */
  332. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
  333. /* Clear the SSC dividers */
  334. ssc_p->cmr_div = ssc_p->tcmr_period = ssc_p->rcmr_period = 0;
  335. }
  336. spin_unlock_irq(&ssc_p->lock);
  337. /* Shutdown the SSC clock. */
  338. pr_debug("atmel_ssc_dai: Stopping clock\n");
  339. clk_disable(ssc_p->ssc->clk);
  340. }
  341. /*
  342. * Record the DAI format for use in hw_params().
  343. */
  344. static int atmel_ssc_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  345. unsigned int fmt)
  346. {
  347. struct atmel_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
  348. ssc_p->daifmt = fmt;
  349. return 0;
  350. }
  351. /*
  352. * Record SSC clock dividers for use in hw_params().
  353. */
  354. static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
  355. int div_id, int div)
  356. {
  357. struct atmel_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
  358. switch (div_id) {
  359. case ATMEL_SSC_CMR_DIV:
  360. /*
  361. * The same master clock divider is used for both
  362. * transmit and receive, so if a value has already
  363. * been set, it must match this value.
  364. */
  365. if (ssc_p->dir_mask !=
  366. (SSC_DIR_MASK_PLAYBACK | SSC_DIR_MASK_CAPTURE))
  367. ssc_p->cmr_div = div;
  368. else if (ssc_p->cmr_div == 0)
  369. ssc_p->cmr_div = div;
  370. else
  371. if (div != ssc_p->cmr_div)
  372. return -EBUSY;
  373. break;
  374. case ATMEL_SSC_TCMR_PERIOD:
  375. ssc_p->tcmr_period = div;
  376. break;
  377. case ATMEL_SSC_RCMR_PERIOD:
  378. ssc_p->rcmr_period = div;
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }
  385. /*
  386. * Configure the SSC.
  387. */
  388. static int atmel_ssc_hw_params(struct snd_pcm_substream *substream,
  389. struct snd_pcm_hw_params *params,
  390. struct snd_soc_dai *dai)
  391. {
  392. int id = dai->id;
  393. struct atmel_ssc_info *ssc_p = &ssc_info[id];
  394. struct ssc_device *ssc = ssc_p->ssc;
  395. struct atmel_pcm_dma_params *dma_params;
  396. int dir, channels, bits;
  397. u32 tfmr, rfmr, tcmr, rcmr;
  398. int ret;
  399. int fslen, fslen_ext;
  400. /*
  401. * Currently, there is only one set of dma params for
  402. * each direction. If more are added, this code will
  403. * have to be changed to select the proper set.
  404. */
  405. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  406. dir = 0;
  407. else
  408. dir = 1;
  409. dma_params = ssc_p->dma_params[dir];
  410. channels = params_channels(params);
  411. /*
  412. * Determine sample size in bits and the PDC increment.
  413. */
  414. switch (params_format(params)) {
  415. case SNDRV_PCM_FORMAT_S8:
  416. bits = 8;
  417. dma_params->pdc_xfer_size = 1;
  418. break;
  419. case SNDRV_PCM_FORMAT_S16_LE:
  420. bits = 16;
  421. dma_params->pdc_xfer_size = 2;
  422. break;
  423. case SNDRV_PCM_FORMAT_S24_LE:
  424. bits = 24;
  425. dma_params->pdc_xfer_size = 4;
  426. break;
  427. case SNDRV_PCM_FORMAT_S32_LE:
  428. bits = 32;
  429. dma_params->pdc_xfer_size = 4;
  430. break;
  431. default:
  432. printk(KERN_WARNING "atmel_ssc_dai: unsupported PCM format");
  433. return -EINVAL;
  434. }
  435. /*
  436. * Compute SSC register settings.
  437. */
  438. switch (ssc_p->daifmt
  439. & (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
  440. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
  441. /*
  442. * I2S format, SSC provides BCLK and LRC clocks.
  443. *
  444. * The SSC transmit and receive clocks are generated
  445. * from the MCK divider, and the BCLK signal
  446. * is output on the SSC TK line.
  447. */
  448. if (bits > 16 && !ssc->pdata->has_fslen_ext) {
  449. dev_err(dai->dev,
  450. "sample size %d is too large for SSC device\n",
  451. bits);
  452. return -EINVAL;
  453. }
  454. fslen_ext = (bits - 1) / 16;
  455. fslen = (bits - 1) % 16;
  456. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  457. | SSC_BF(RCMR_STTDLY, START_DELAY)
  458. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  459. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  460. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  461. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  462. rfmr = SSC_BF(RFMR_FSLEN_EXT, fslen_ext)
  463. | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  464. | SSC_BF(RFMR_FSOS, SSC_FSOS_NEGATIVE)
  465. | SSC_BF(RFMR_FSLEN, fslen)
  466. | SSC_BF(RFMR_DATNB, (channels - 1))
  467. | SSC_BIT(RFMR_MSBF)
  468. | SSC_BF(RFMR_LOOP, 0)
  469. | SSC_BF(RFMR_DATLEN, (bits - 1));
  470. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  471. | SSC_BF(TCMR_STTDLY, START_DELAY)
  472. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  473. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  474. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  475. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  476. tfmr = SSC_BF(TFMR_FSLEN_EXT, fslen_ext)
  477. | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  478. | SSC_BF(TFMR_FSDEN, 0)
  479. | SSC_BF(TFMR_FSOS, SSC_FSOS_NEGATIVE)
  480. | SSC_BF(TFMR_FSLEN, fslen)
  481. | SSC_BF(TFMR_DATNB, (channels - 1))
  482. | SSC_BIT(TFMR_MSBF)
  483. | SSC_BF(TFMR_DATDEF, 0)
  484. | SSC_BF(TFMR_DATLEN, (bits - 1));
  485. break;
  486. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
  487. /* I2S format, CODEC supplies BCLK and LRC clocks. */
  488. rcmr = SSC_BF(RCMR_PERIOD, 0)
  489. | SSC_BF(RCMR_STTDLY, START_DELAY)
  490. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  491. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  492. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  493. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  494. SSC_CKS_PIN : SSC_CKS_CLOCK);
  495. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  496. | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
  497. | SSC_BF(RFMR_FSLEN, 0)
  498. | SSC_BF(RFMR_DATNB, (channels - 1))
  499. | SSC_BIT(RFMR_MSBF)
  500. | SSC_BF(RFMR_LOOP, 0)
  501. | SSC_BF(RFMR_DATLEN, (bits - 1));
  502. tcmr = SSC_BF(TCMR_PERIOD, 0)
  503. | SSC_BF(TCMR_STTDLY, START_DELAY)
  504. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  505. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  506. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  507. | SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ?
  508. SSC_CKS_CLOCK : SSC_CKS_PIN);
  509. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  510. | SSC_BF(TFMR_FSDEN, 0)
  511. | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
  512. | SSC_BF(TFMR_FSLEN, 0)
  513. | SSC_BF(TFMR_DATNB, (channels - 1))
  514. | SSC_BIT(TFMR_MSBF)
  515. | SSC_BF(TFMR_DATDEF, 0)
  516. | SSC_BF(TFMR_DATLEN, (bits - 1));
  517. break;
  518. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFS:
  519. /* I2S format, CODEC supplies BCLK, SSC supplies LRCLK. */
  520. if (bits > 16 && !ssc->pdata->has_fslen_ext) {
  521. dev_err(dai->dev,
  522. "sample size %d is too large for SSC device\n",
  523. bits);
  524. return -EINVAL;
  525. }
  526. fslen_ext = (bits - 1) / 16;
  527. fslen = (bits - 1) % 16;
  528. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  529. | SSC_BF(RCMR_STTDLY, START_DELAY)
  530. | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
  531. | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
  532. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  533. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  534. SSC_CKS_PIN : SSC_CKS_CLOCK);
  535. rfmr = SSC_BF(RFMR_FSLEN_EXT, fslen_ext)
  536. | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  537. | SSC_BF(RFMR_FSOS, SSC_FSOS_NEGATIVE)
  538. | SSC_BF(RFMR_FSLEN, fslen)
  539. | SSC_BF(RFMR_DATNB, (channels - 1))
  540. | SSC_BIT(RFMR_MSBF)
  541. | SSC_BF(RFMR_LOOP, 0)
  542. | SSC_BF(RFMR_DATLEN, (bits - 1));
  543. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  544. | SSC_BF(TCMR_STTDLY, START_DELAY)
  545. | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
  546. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  547. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  548. | SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ?
  549. SSC_CKS_CLOCK : SSC_CKS_PIN);
  550. tfmr = SSC_BF(TFMR_FSLEN_EXT, fslen_ext)
  551. | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_NEGATIVE)
  552. | SSC_BF(TFMR_FSDEN, 0)
  553. | SSC_BF(TFMR_FSOS, SSC_FSOS_NEGATIVE)
  554. | SSC_BF(TFMR_FSLEN, fslen)
  555. | SSC_BF(TFMR_DATNB, (channels - 1))
  556. | SSC_BIT(TFMR_MSBF)
  557. | SSC_BF(TFMR_DATDEF, 0)
  558. | SSC_BF(TFMR_DATLEN, (bits - 1));
  559. break;
  560. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
  561. /*
  562. * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
  563. *
  564. * The SSC transmit and receive clocks are generated from the
  565. * MCK divider, and the BCLK signal is output
  566. * on the SSC TK line.
  567. */
  568. rcmr = SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
  569. | SSC_BF(RCMR_STTDLY, 1)
  570. | SSC_BF(RCMR_START, SSC_START_RISING_RF)
  571. | SSC_BF(RCMR_CKI, SSC_CKI_FALLING)
  572. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  573. | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
  574. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  575. | SSC_BF(RFMR_FSOS, SSC_FSOS_POSITIVE)
  576. | SSC_BF(RFMR_FSLEN, 0)
  577. | SSC_BF(RFMR_DATNB, (channels - 1))
  578. | SSC_BIT(RFMR_MSBF)
  579. | SSC_BF(RFMR_LOOP, 0)
  580. | SSC_BF(RFMR_DATLEN, (bits - 1));
  581. tcmr = SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
  582. | SSC_BF(TCMR_STTDLY, 1)
  583. | SSC_BF(TCMR_START, SSC_START_RISING_RF)
  584. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  585. | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
  586. | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
  587. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  588. | SSC_BF(TFMR_FSDEN, 0)
  589. | SSC_BF(TFMR_FSOS, SSC_FSOS_POSITIVE)
  590. | SSC_BF(TFMR_FSLEN, 0)
  591. | SSC_BF(TFMR_DATNB, (channels - 1))
  592. | SSC_BIT(TFMR_MSBF)
  593. | SSC_BF(TFMR_DATDEF, 0)
  594. | SSC_BF(TFMR_DATLEN, (bits - 1));
  595. break;
  596. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
  597. /*
  598. * DSP/PCM Mode A format, CODEC supplies BCLK and LRC clocks.
  599. *
  600. * Data is transferred on first BCLK after LRC pulse rising
  601. * edge.If stereo, the right channel data is contiguous with
  602. * the left channel data.
  603. */
  604. rcmr = SSC_BF(RCMR_PERIOD, 0)
  605. | SSC_BF(RCMR_STTDLY, START_DELAY)
  606. | SSC_BF(RCMR_START, SSC_START_RISING_RF)
  607. | SSC_BF(RCMR_CKI, SSC_CKI_FALLING)
  608. | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
  609. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  610. SSC_CKS_PIN : SSC_CKS_CLOCK);
  611. rfmr = SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  612. | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
  613. | SSC_BF(RFMR_FSLEN, 0)
  614. | SSC_BF(RFMR_DATNB, (channels - 1))
  615. | SSC_BIT(RFMR_MSBF)
  616. | SSC_BF(RFMR_LOOP, 0)
  617. | SSC_BF(RFMR_DATLEN, (bits - 1));
  618. tcmr = SSC_BF(TCMR_PERIOD, 0)
  619. | SSC_BF(TCMR_STTDLY, START_DELAY)
  620. | SSC_BF(TCMR_START, SSC_START_RISING_RF)
  621. | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
  622. | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
  623. | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
  624. SSC_CKS_CLOCK : SSC_CKS_PIN);
  625. tfmr = SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
  626. | SSC_BF(TFMR_FSDEN, 0)
  627. | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
  628. | SSC_BF(TFMR_FSLEN, 0)
  629. | SSC_BF(TFMR_DATNB, (channels - 1))
  630. | SSC_BIT(TFMR_MSBF)
  631. | SSC_BF(TFMR_DATDEF, 0)
  632. | SSC_BF(TFMR_DATLEN, (bits - 1));
  633. break;
  634. default:
  635. printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n",
  636. ssc_p->daifmt);
  637. return -EINVAL;
  638. }
  639. pr_debug("atmel_ssc_hw_params: "
  640. "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
  641. rcmr, rfmr, tcmr, tfmr);
  642. if (!ssc_p->initialized) {
  643. if (!ssc_p->ssc->pdata->use_dma) {
  644. ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0);
  645. ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0);
  646. ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0);
  647. ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0);
  648. ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0);
  649. ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0);
  650. ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0);
  651. ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0);
  652. }
  653. ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0,
  654. ssc_p->name, ssc_p);
  655. if (ret < 0) {
  656. printk(KERN_WARNING
  657. "atmel_ssc_dai: request_irq failure\n");
  658. pr_debug("Atmel_ssc_dai: Stoping clock\n");
  659. clk_disable(ssc_p->ssc->clk);
  660. return ret;
  661. }
  662. ssc_p->initialized = 1;
  663. }
  664. /* set SSC clock mode register */
  665. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->cmr_div);
  666. /* set receive clock mode and format */
  667. ssc_writel(ssc_p->ssc->regs, RCMR, rcmr);
  668. ssc_writel(ssc_p->ssc->regs, RFMR, rfmr);
  669. /* set transmit clock mode and format */
  670. ssc_writel(ssc_p->ssc->regs, TCMR, tcmr);
  671. ssc_writel(ssc_p->ssc->regs, TFMR, tfmr);
  672. pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
  673. return 0;
  674. }
  675. static int atmel_ssc_prepare(struct snd_pcm_substream *substream,
  676. struct snd_soc_dai *dai)
  677. {
  678. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  679. struct atmel_pcm_dma_params *dma_params;
  680. int dir;
  681. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  682. dir = 0;
  683. else
  684. dir = 1;
  685. dma_params = ssc_p->dma_params[dir];
  686. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
  687. ssc_writel(ssc_p->ssc->regs, IDR, dma_params->mask->ssc_error);
  688. pr_debug("%s enabled SSC_SR=0x%08x\n",
  689. dir ? "receive" : "transmit",
  690. ssc_readl(ssc_p->ssc->regs, SR));
  691. return 0;
  692. }
  693. static int atmel_ssc_trigger(struct snd_pcm_substream *substream,
  694. int cmd, struct snd_soc_dai *dai)
  695. {
  696. struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
  697. struct atmel_pcm_dma_params *dma_params;
  698. int dir;
  699. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  700. dir = 0;
  701. else
  702. dir = 1;
  703. dma_params = ssc_p->dma_params[dir];
  704. switch (cmd) {
  705. case SNDRV_PCM_TRIGGER_START:
  706. case SNDRV_PCM_TRIGGER_RESUME:
  707. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  708. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable);
  709. break;
  710. default:
  711. ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
  712. break;
  713. }
  714. return 0;
  715. }
  716. #ifdef CONFIG_PM
  717. static int atmel_ssc_suspend(struct snd_soc_dai *cpu_dai)
  718. {
  719. struct atmel_ssc_info *ssc_p;
  720. if (!cpu_dai->active)
  721. return 0;
  722. ssc_p = &ssc_info[cpu_dai->id];
  723. /* Save the status register before disabling transmit and receive */
  724. ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR);
  725. ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS));
  726. /* Save the current interrupt mask, then disable unmasked interrupts */
  727. ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR);
  728. ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr);
  729. ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR);
  730. ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR);
  731. ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR);
  732. ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR);
  733. ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR);
  734. return 0;
  735. }
  736. static int atmel_ssc_resume(struct snd_soc_dai *cpu_dai)
  737. {
  738. struct atmel_ssc_info *ssc_p;
  739. u32 cr;
  740. if (!cpu_dai->active)
  741. return 0;
  742. ssc_p = &ssc_info[cpu_dai->id];
  743. /* restore SSC register settings */
  744. ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr);
  745. ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr);
  746. ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr);
  747. ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr);
  748. ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr);
  749. /* re-enable interrupts */
  750. ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr);
  751. /* Re-enable receive and transmit as appropriate */
  752. cr = 0;
  753. cr |=
  754. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0;
  755. cr |=
  756. (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0;
  757. ssc_writel(ssc_p->ssc->regs, CR, cr);
  758. return 0;
  759. }
  760. #else /* CONFIG_PM */
  761. # define atmel_ssc_suspend NULL
  762. # define atmel_ssc_resume NULL
  763. #endif /* CONFIG_PM */
  764. #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
  765. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  766. static const struct snd_soc_dai_ops atmel_ssc_dai_ops = {
  767. .startup = atmel_ssc_startup,
  768. .shutdown = atmel_ssc_shutdown,
  769. .prepare = atmel_ssc_prepare,
  770. .trigger = atmel_ssc_trigger,
  771. .hw_params = atmel_ssc_hw_params,
  772. .set_fmt = atmel_ssc_set_dai_fmt,
  773. .set_clkdiv = atmel_ssc_set_dai_clkdiv,
  774. };
  775. static struct snd_soc_dai_driver atmel_ssc_dai = {
  776. .suspend = atmel_ssc_suspend,
  777. .resume = atmel_ssc_resume,
  778. .playback = {
  779. .channels_min = 1,
  780. .channels_max = 2,
  781. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  782. .rate_min = 8000,
  783. .rate_max = 384000,
  784. .formats = ATMEL_SSC_FORMATS,},
  785. .capture = {
  786. .channels_min = 1,
  787. .channels_max = 2,
  788. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  789. .rate_min = 8000,
  790. .rate_max = 384000,
  791. .formats = ATMEL_SSC_FORMATS,},
  792. .ops = &atmel_ssc_dai_ops,
  793. };
  794. static const struct snd_soc_component_driver atmel_ssc_component = {
  795. .name = "atmel-ssc",
  796. };
  797. static int asoc_ssc_init(struct device *dev)
  798. {
  799. struct platform_device *pdev = to_platform_device(dev);
  800. struct ssc_device *ssc = platform_get_drvdata(pdev);
  801. int ret;
  802. ret = snd_soc_register_component(dev, &atmel_ssc_component,
  803. &atmel_ssc_dai, 1);
  804. if (ret) {
  805. dev_err(dev, "Could not register DAI: %d\n", ret);
  806. goto err;
  807. }
  808. if (ssc->pdata->use_dma)
  809. ret = atmel_pcm_dma_platform_register(dev);
  810. else
  811. ret = atmel_pcm_pdc_platform_register(dev);
  812. if (ret) {
  813. dev_err(dev, "Could not register PCM: %d\n", ret);
  814. goto err_unregister_dai;
  815. }
  816. return 0;
  817. err_unregister_dai:
  818. snd_soc_unregister_component(dev);
  819. err:
  820. return ret;
  821. }
  822. static void asoc_ssc_exit(struct device *dev)
  823. {
  824. struct platform_device *pdev = to_platform_device(dev);
  825. struct ssc_device *ssc = platform_get_drvdata(pdev);
  826. if (ssc->pdata->use_dma)
  827. atmel_pcm_dma_platform_unregister(dev);
  828. else
  829. atmel_pcm_pdc_platform_unregister(dev);
  830. snd_soc_unregister_component(dev);
  831. }
  832. /**
  833. * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
  834. */
  835. int atmel_ssc_set_audio(int ssc_id)
  836. {
  837. struct ssc_device *ssc;
  838. int ret;
  839. /* If we can grab the SSC briefly to parent the DAI device off it */
  840. ssc = ssc_request(ssc_id);
  841. if (IS_ERR(ssc)) {
  842. pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n",
  843. PTR_ERR(ssc));
  844. return PTR_ERR(ssc);
  845. } else {
  846. ssc_info[ssc_id].ssc = ssc;
  847. }
  848. ret = asoc_ssc_init(&ssc->pdev->dev);
  849. return ret;
  850. }
  851. EXPORT_SYMBOL_GPL(atmel_ssc_set_audio);
  852. void atmel_ssc_put_audio(int ssc_id)
  853. {
  854. struct ssc_device *ssc = ssc_info[ssc_id].ssc;
  855. asoc_ssc_exit(&ssc->pdev->dev);
  856. ssc_free(ssc);
  857. }
  858. EXPORT_SYMBOL_GPL(atmel_ssc_put_audio);
  859. /* Module information */
  860. MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
  861. MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
  862. MODULE_LICENSE("GPL");