fsl_ssi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007-2008 Freescale Semiconductor, Inc. This file is licensed
  7. * under the terms of the GNU General Public License version 2. This
  8. * program is licensed "as is" without any warranty of any kind, whether
  9. * express or implied.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/device.h>
  15. #include <linux/delay.h>
  16. #include <sound/core.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/initval.h>
  20. #include <sound/soc.h>
  21. #include <asm/immap_86xx.h>
  22. #include "fsl_ssi.h"
  23. /**
  24. * FSLSSI_I2S_RATES: sample rates supported by the I2S
  25. *
  26. * This driver currently only supports the SSI running in I2S slave mode,
  27. * which means the codec determines the sample rate. Therefore, we tell
  28. * ALSA that we support all rates and let the codec driver decide what rates
  29. * are really supported.
  30. */
  31. #define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
  32. SNDRV_PCM_RATE_CONTINUOUS)
  33. /**
  34. * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
  35. *
  36. * This driver currently only supports the SSI running in I2S slave mode.
  37. *
  38. * The SSI has a limitation in that the samples must be in the same byte
  39. * order as the host CPU. This is because when multiple bytes are written
  40. * to the STX register, the bytes and bits must be written in the same
  41. * order. The STX is a shift register, so all the bits need to be aligned
  42. * (bit-endianness must match byte-endianness). Processors typically write
  43. * the bits within a byte in the same order that the bytes of a word are
  44. * written in. So if the host CPU is big-endian, then only big-endian
  45. * samples will be written to STX properly.
  46. */
  47. #ifdef __BIG_ENDIAN
  48. #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
  49. SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
  50. SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
  51. #else
  52. #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
  53. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  54. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
  55. #endif
  56. /**
  57. * fsl_ssi_private: per-SSI private data
  58. *
  59. * @name: short name for this device ("SSI0", "SSI1", etc)
  60. * @ssi: pointer to the SSI's registers
  61. * @ssi_phys: physical address of the SSI registers
  62. * @irq: IRQ of this SSI
  63. * @first_stream: pointer to the stream that was opened first
  64. * @second_stream: pointer to second stream
  65. * @dev: struct device pointer
  66. * @playback: the number of playback streams opened
  67. * @capture: the number of capture streams opened
  68. * @asynchronous: 0=synchronous mode, 1=asynchronous mode
  69. * @cpu_dai: the CPU DAI for this device
  70. * @dev_attr: the sysfs device attribute structure
  71. * @stats: SSI statistics
  72. */
  73. struct fsl_ssi_private {
  74. char name[8];
  75. struct ccsr_ssi __iomem *ssi;
  76. dma_addr_t ssi_phys;
  77. unsigned int irq;
  78. struct snd_pcm_substream *first_stream;
  79. struct snd_pcm_substream *second_stream;
  80. struct device *dev;
  81. unsigned int playback;
  82. unsigned int capture;
  83. int asynchronous;
  84. struct snd_soc_dai cpu_dai;
  85. struct device_attribute dev_attr;
  86. struct {
  87. unsigned int rfrc;
  88. unsigned int tfrc;
  89. unsigned int cmdau;
  90. unsigned int cmddu;
  91. unsigned int rxt;
  92. unsigned int rdr1;
  93. unsigned int rdr0;
  94. unsigned int tde1;
  95. unsigned int tde0;
  96. unsigned int roe1;
  97. unsigned int roe0;
  98. unsigned int tue1;
  99. unsigned int tue0;
  100. unsigned int tfs;
  101. unsigned int rfs;
  102. unsigned int tls;
  103. unsigned int rls;
  104. unsigned int rff1;
  105. unsigned int rff0;
  106. unsigned int tfe1;
  107. unsigned int tfe0;
  108. } stats;
  109. };
  110. /**
  111. * fsl_ssi_isr: SSI interrupt handler
  112. *
  113. * Although it's possible to use the interrupt handler to send and receive
  114. * data to/from the SSI, we use the DMA instead. Programming is more
  115. * complicated, but the performance is much better.
  116. *
  117. * This interrupt handler is used only to gather statistics.
  118. *
  119. * @irq: IRQ of the SSI device
  120. * @dev_id: pointer to the ssi_private structure for this SSI device
  121. */
  122. static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
  123. {
  124. struct fsl_ssi_private *ssi_private = dev_id;
  125. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  126. irqreturn_t ret = IRQ_NONE;
  127. __be32 sisr;
  128. __be32 sisr2 = 0;
  129. /* We got an interrupt, so read the status register to see what we
  130. were interrupted for. We mask it with the Interrupt Enable register
  131. so that we only check for events that we're interested in.
  132. */
  133. sisr = in_be32(&ssi->sisr) & in_be32(&ssi->sier);
  134. if (sisr & CCSR_SSI_SISR_RFRC) {
  135. ssi_private->stats.rfrc++;
  136. sisr2 |= CCSR_SSI_SISR_RFRC;
  137. ret = IRQ_HANDLED;
  138. }
  139. if (sisr & CCSR_SSI_SISR_TFRC) {
  140. ssi_private->stats.tfrc++;
  141. sisr2 |= CCSR_SSI_SISR_TFRC;
  142. ret = IRQ_HANDLED;
  143. }
  144. if (sisr & CCSR_SSI_SISR_CMDAU) {
  145. ssi_private->stats.cmdau++;
  146. ret = IRQ_HANDLED;
  147. }
  148. if (sisr & CCSR_SSI_SISR_CMDDU) {
  149. ssi_private->stats.cmddu++;
  150. ret = IRQ_HANDLED;
  151. }
  152. if (sisr & CCSR_SSI_SISR_RXT) {
  153. ssi_private->stats.rxt++;
  154. ret = IRQ_HANDLED;
  155. }
  156. if (sisr & CCSR_SSI_SISR_RDR1) {
  157. ssi_private->stats.rdr1++;
  158. ret = IRQ_HANDLED;
  159. }
  160. if (sisr & CCSR_SSI_SISR_RDR0) {
  161. ssi_private->stats.rdr0++;
  162. ret = IRQ_HANDLED;
  163. }
  164. if (sisr & CCSR_SSI_SISR_TDE1) {
  165. ssi_private->stats.tde1++;
  166. ret = IRQ_HANDLED;
  167. }
  168. if (sisr & CCSR_SSI_SISR_TDE0) {
  169. ssi_private->stats.tde0++;
  170. ret = IRQ_HANDLED;
  171. }
  172. if (sisr & CCSR_SSI_SISR_ROE1) {
  173. ssi_private->stats.roe1++;
  174. sisr2 |= CCSR_SSI_SISR_ROE1;
  175. ret = IRQ_HANDLED;
  176. }
  177. if (sisr & CCSR_SSI_SISR_ROE0) {
  178. ssi_private->stats.roe0++;
  179. sisr2 |= CCSR_SSI_SISR_ROE0;
  180. ret = IRQ_HANDLED;
  181. }
  182. if (sisr & CCSR_SSI_SISR_TUE1) {
  183. ssi_private->stats.tue1++;
  184. sisr2 |= CCSR_SSI_SISR_TUE1;
  185. ret = IRQ_HANDLED;
  186. }
  187. if (sisr & CCSR_SSI_SISR_TUE0) {
  188. ssi_private->stats.tue0++;
  189. sisr2 |= CCSR_SSI_SISR_TUE0;
  190. ret = IRQ_HANDLED;
  191. }
  192. if (sisr & CCSR_SSI_SISR_TFS) {
  193. ssi_private->stats.tfs++;
  194. ret = IRQ_HANDLED;
  195. }
  196. if (sisr & CCSR_SSI_SISR_RFS) {
  197. ssi_private->stats.rfs++;
  198. ret = IRQ_HANDLED;
  199. }
  200. if (sisr & CCSR_SSI_SISR_TLS) {
  201. ssi_private->stats.tls++;
  202. ret = IRQ_HANDLED;
  203. }
  204. if (sisr & CCSR_SSI_SISR_RLS) {
  205. ssi_private->stats.rls++;
  206. ret = IRQ_HANDLED;
  207. }
  208. if (sisr & CCSR_SSI_SISR_RFF1) {
  209. ssi_private->stats.rff1++;
  210. ret = IRQ_HANDLED;
  211. }
  212. if (sisr & CCSR_SSI_SISR_RFF0) {
  213. ssi_private->stats.rff0++;
  214. ret = IRQ_HANDLED;
  215. }
  216. if (sisr & CCSR_SSI_SISR_TFE1) {
  217. ssi_private->stats.tfe1++;
  218. ret = IRQ_HANDLED;
  219. }
  220. if (sisr & CCSR_SSI_SISR_TFE0) {
  221. ssi_private->stats.tfe0++;
  222. ret = IRQ_HANDLED;
  223. }
  224. /* Clear the bits that we set */
  225. if (sisr2)
  226. out_be32(&ssi->sisr, sisr2);
  227. return ret;
  228. }
  229. /**
  230. * fsl_ssi_startup: create a new substream
  231. *
  232. * This is the first function called when a stream is opened.
  233. *
  234. * If this is the first stream open, then grab the IRQ and program most of
  235. * the SSI registers.
  236. */
  237. static int fsl_ssi_startup(struct snd_pcm_substream *substream,
  238. struct snd_soc_dai *dai)
  239. {
  240. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  241. struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
  242. /*
  243. * If this is the first stream opened, then request the IRQ
  244. * and initialize the SSI registers.
  245. */
  246. if (!ssi_private->playback && !ssi_private->capture) {
  247. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  248. int ret;
  249. ret = request_irq(ssi_private->irq, fsl_ssi_isr, 0,
  250. ssi_private->name, ssi_private);
  251. if (ret < 0) {
  252. dev_err(substream->pcm->card->dev,
  253. "could not claim irq %u\n", ssi_private->irq);
  254. return ret;
  255. }
  256. /*
  257. * Section 16.5 of the MPC8610 reference manual says that the
  258. * SSI needs to be disabled before updating the registers we set
  259. * here.
  260. */
  261. clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
  262. /*
  263. * Program the SSI into I2S Slave Non-Network Synchronous mode.
  264. * Also enable the transmit and receive FIFO.
  265. *
  266. * FIXME: Little-endian samples require a different shift dir
  267. */
  268. clrsetbits_be32(&ssi->scr,
  269. CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN,
  270. CCSR_SSI_SCR_TFR_CLK_DIS | CCSR_SSI_SCR_I2S_MODE_SLAVE
  271. | (ssi_private->asynchronous ? 0 : CCSR_SSI_SCR_SYN));
  272. out_be32(&ssi->stcr,
  273. CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 |
  274. CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS |
  275. CCSR_SSI_STCR_TSCKP);
  276. out_be32(&ssi->srcr,
  277. CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 |
  278. CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS |
  279. CCSR_SSI_SRCR_RSCKP);
  280. /*
  281. * The DC and PM bits are only used if the SSI is the clock
  282. * master.
  283. */
  284. /* 4. Enable the interrupts and DMA requests */
  285. out_be32(&ssi->sier,
  286. CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE |
  287. CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN |
  288. CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN |
  289. CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE |
  290. CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN);
  291. /*
  292. * Set the watermark for transmit FIFI 0 and receive FIFO 0. We
  293. * don't use FIFO 1. Since the SSI only supports stereo, the
  294. * watermark should never be an odd number.
  295. */
  296. out_be32(&ssi->sfcsr,
  297. CCSR_SSI_SFCSR_TFWM0(6) | CCSR_SSI_SFCSR_RFWM0(2));
  298. /*
  299. * We keep the SSI disabled because if we enable it, then the
  300. * DMA controller will start. It's not supposed to start until
  301. * the SCR.TE (or SCR.RE) bit is set, but it does anyway. The
  302. * DMA controller will transfer one "BWC" of data (i.e. the
  303. * amount of data that the MR.BWC bits are set to). The reason
  304. * this is bad is because at this point, the PCM driver has not
  305. * finished initializing the DMA controller.
  306. */
  307. }
  308. if (!ssi_private->first_stream)
  309. ssi_private->first_stream = substream;
  310. else {
  311. /* This is the second stream open, so we need to impose sample
  312. * rate and maybe sample size constraints. Note that this can
  313. * cause a race condition if the second stream is opened before
  314. * the first stream is fully initialized.
  315. *
  316. * We provide some protection by checking to make sure the first
  317. * stream is initialized, but it's not perfect. ALSA sometimes
  318. * re-initializes the driver with a different sample rate or
  319. * size. If the second stream is opened before the first stream
  320. * has received its final parameters, then the second stream may
  321. * be constrained to the wrong sample rate or size.
  322. *
  323. * FIXME: This code does not handle opening and closing streams
  324. * repeatedly. If you open two streams and then close the first
  325. * one, you may not be able to open another stream until you
  326. * close the second one as well.
  327. */
  328. struct snd_pcm_runtime *first_runtime =
  329. ssi_private->first_stream->runtime;
  330. if (!first_runtime->rate || !first_runtime->sample_bits) {
  331. dev_err(substream->pcm->card->dev,
  332. "set sample rate and size in %s stream first\n",
  333. substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  334. ? "capture" : "playback");
  335. return -EAGAIN;
  336. }
  337. snd_pcm_hw_constraint_minmax(substream->runtime,
  338. SNDRV_PCM_HW_PARAM_RATE,
  339. first_runtime->rate, first_runtime->rate);
  340. /* If we're in synchronous mode, then we need to constrain
  341. * the sample size as well. We don't support independent sample
  342. * rates in asynchronous mode.
  343. */
  344. if (!ssi_private->asynchronous)
  345. snd_pcm_hw_constraint_minmax(substream->runtime,
  346. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  347. first_runtime->sample_bits,
  348. first_runtime->sample_bits);
  349. ssi_private->second_stream = substream;
  350. }
  351. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  352. ssi_private->playback++;
  353. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  354. ssi_private->capture++;
  355. return 0;
  356. }
  357. /**
  358. * fsl_ssi_hw_params - program the sample size
  359. *
  360. * Most of the SSI registers have been programmed in the startup function,
  361. * but the word length must be programmed here. Unfortunately, programming
  362. * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
  363. * cause a problem with supporting simultaneous playback and capture. If
  364. * the SSI is already playing a stream, then that stream may be temporarily
  365. * stopped when you start capture.
  366. *
  367. * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
  368. * clock master.
  369. */
  370. static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
  371. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
  372. {
  373. struct fsl_ssi_private *ssi_private = cpu_dai->private_data;
  374. if (substream == ssi_private->first_stream) {
  375. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  376. unsigned int sample_size =
  377. snd_pcm_format_width(params_format(hw_params));
  378. u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
  379. /* The SSI should always be disabled at this points (SSIEN=0) */
  380. /* In synchronous mode, the SSI uses STCCR for capture */
  381. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
  382. !ssi_private->asynchronous)
  383. clrsetbits_be32(&ssi->stccr,
  384. CCSR_SSI_SxCCR_WL_MASK, wl);
  385. else
  386. clrsetbits_be32(&ssi->srccr,
  387. CCSR_SSI_SxCCR_WL_MASK, wl);
  388. }
  389. return 0;
  390. }
  391. /**
  392. * fsl_ssi_trigger: start and stop the DMA transfer.
  393. *
  394. * This function is called by ALSA to start, stop, pause, and resume the DMA
  395. * transfer of data.
  396. *
  397. * The DMA channel is in external master start and pause mode, which
  398. * means the SSI completely controls the flow of data.
  399. */
  400. static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
  401. struct snd_soc_dai *dai)
  402. {
  403. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  404. struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
  405. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  406. switch (cmd) {
  407. case SNDRV_PCM_TRIGGER_START:
  408. case SNDRV_PCM_TRIGGER_RESUME:
  409. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  410. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  411. clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
  412. setbits32(&ssi->scr,
  413. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE);
  414. } else {
  415. clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
  416. setbits32(&ssi->scr,
  417. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);
  418. /*
  419. * I think we need this delay to allow time for the SSI
  420. * to put data into its FIFO. Without it, ALSA starts
  421. * to complain about overruns.
  422. */
  423. mdelay(1);
  424. }
  425. break;
  426. case SNDRV_PCM_TRIGGER_STOP:
  427. case SNDRV_PCM_TRIGGER_SUSPEND:
  428. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  429. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  430. clrbits32(&ssi->scr, CCSR_SSI_SCR_TE);
  431. else
  432. clrbits32(&ssi->scr, CCSR_SSI_SCR_RE);
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. /**
  440. * fsl_ssi_shutdown: shutdown the SSI
  441. *
  442. * Shutdown the SSI if there are no other substreams open.
  443. */
  444. static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
  445. struct snd_soc_dai *dai)
  446. {
  447. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  448. struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
  449. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  450. ssi_private->playback--;
  451. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  452. ssi_private->capture--;
  453. if (ssi_private->first_stream == substream)
  454. ssi_private->first_stream = ssi_private->second_stream;
  455. ssi_private->second_stream = NULL;
  456. /*
  457. * If this is the last active substream, disable the SSI and release
  458. * the IRQ.
  459. */
  460. if (!ssi_private->playback && !ssi_private->capture) {
  461. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  462. clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
  463. free_irq(ssi_private->irq, ssi_private);
  464. }
  465. }
  466. /**
  467. * fsl_ssi_set_sysclk: set the clock frequency and direction
  468. *
  469. * This function is called by the machine driver to tell us what the clock
  470. * frequency and direction are.
  471. *
  472. * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN),
  473. * and we don't care about the frequency. Return an error if the direction
  474. * is not SND_SOC_CLOCK_IN.
  475. *
  476. * @clk_id: reserved, should be zero
  477. * @freq: the frequency of the given clock ID, currently ignored
  478. * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master)
  479. */
  480. static int fsl_ssi_set_sysclk(struct snd_soc_dai *cpu_dai,
  481. int clk_id, unsigned int freq, int dir)
  482. {
  483. return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL;
  484. }
  485. /**
  486. * fsl_ssi_set_fmt: set the serial format.
  487. *
  488. * This function is called by the machine driver to tell us what serial
  489. * format to use.
  490. *
  491. * Currently, we only support I2S mode. Return an error if the format is
  492. * not SND_SOC_DAIFMT_I2S.
  493. *
  494. * @format: one of SND_SOC_DAIFMT_xxx
  495. */
  496. static int fsl_ssi_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
  497. {
  498. return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL;
  499. }
  500. /**
  501. * fsl_ssi_dai_template: template CPU DAI for the SSI
  502. */
  503. static struct snd_soc_dai fsl_ssi_dai_template = {
  504. .playback = {
  505. /* The SSI does not support monaural audio. */
  506. .channels_min = 2,
  507. .channels_max = 2,
  508. .rates = FSLSSI_I2S_RATES,
  509. .formats = FSLSSI_I2S_FORMATS,
  510. },
  511. .capture = {
  512. .channels_min = 2,
  513. .channels_max = 2,
  514. .rates = FSLSSI_I2S_RATES,
  515. .formats = FSLSSI_I2S_FORMATS,
  516. },
  517. .ops = {
  518. .startup = fsl_ssi_startup,
  519. .hw_params = fsl_ssi_hw_params,
  520. .shutdown = fsl_ssi_shutdown,
  521. .trigger = fsl_ssi_trigger,
  522. .set_sysclk = fsl_ssi_set_sysclk,
  523. .set_fmt = fsl_ssi_set_fmt,
  524. },
  525. };
  526. /**
  527. * fsl_sysfs_ssi_show: display SSI statistics
  528. *
  529. * Display the statistics for the current SSI device.
  530. */
  531. static ssize_t fsl_sysfs_ssi_show(struct device *dev,
  532. struct device_attribute *attr, char *buf)
  533. {
  534. struct fsl_ssi_private *ssi_private =
  535. container_of(attr, struct fsl_ssi_private, dev_attr);
  536. ssize_t length;
  537. length = sprintf(buf, "rfrc=%u", ssi_private->stats.rfrc);
  538. length += sprintf(buf + length, "\ttfrc=%u", ssi_private->stats.tfrc);
  539. length += sprintf(buf + length, "\tcmdau=%u", ssi_private->stats.cmdau);
  540. length += sprintf(buf + length, "\tcmddu=%u", ssi_private->stats.cmddu);
  541. length += sprintf(buf + length, "\trxt=%u", ssi_private->stats.rxt);
  542. length += sprintf(buf + length, "\trdr1=%u", ssi_private->stats.rdr1);
  543. length += sprintf(buf + length, "\trdr0=%u", ssi_private->stats.rdr0);
  544. length += sprintf(buf + length, "\ttde1=%u", ssi_private->stats.tde1);
  545. length += sprintf(buf + length, "\ttde0=%u", ssi_private->stats.tde0);
  546. length += sprintf(buf + length, "\troe1=%u", ssi_private->stats.roe1);
  547. length += sprintf(buf + length, "\troe0=%u", ssi_private->stats.roe0);
  548. length += sprintf(buf + length, "\ttue1=%u", ssi_private->stats.tue1);
  549. length += sprintf(buf + length, "\ttue0=%u", ssi_private->stats.tue0);
  550. length += sprintf(buf + length, "\ttfs=%u", ssi_private->stats.tfs);
  551. length += sprintf(buf + length, "\trfs=%u", ssi_private->stats.rfs);
  552. length += sprintf(buf + length, "\ttls=%u", ssi_private->stats.tls);
  553. length += sprintf(buf + length, "\trls=%u", ssi_private->stats.rls);
  554. length += sprintf(buf + length, "\trff1=%u", ssi_private->stats.rff1);
  555. length += sprintf(buf + length, "\trff0=%u", ssi_private->stats.rff0);
  556. length += sprintf(buf + length, "\ttfe1=%u", ssi_private->stats.tfe1);
  557. length += sprintf(buf + length, "\ttfe0=%u\n", ssi_private->stats.tfe0);
  558. return length;
  559. }
  560. /**
  561. * fsl_ssi_create_dai: create a snd_soc_dai structure
  562. *
  563. * This function is called by the machine driver to create a snd_soc_dai
  564. * structure. The function creates an ssi_private object, which contains
  565. * the snd_soc_dai. It also creates the sysfs statistics device.
  566. */
  567. struct snd_soc_dai *fsl_ssi_create_dai(struct fsl_ssi_info *ssi_info)
  568. {
  569. struct snd_soc_dai *fsl_ssi_dai;
  570. struct fsl_ssi_private *ssi_private;
  571. int ret = 0;
  572. struct device_attribute *dev_attr;
  573. ssi_private = kzalloc(sizeof(struct fsl_ssi_private), GFP_KERNEL);
  574. if (!ssi_private) {
  575. dev_err(ssi_info->dev, "could not allocate DAI object\n");
  576. return NULL;
  577. }
  578. memcpy(&ssi_private->cpu_dai, &fsl_ssi_dai_template,
  579. sizeof(struct snd_soc_dai));
  580. fsl_ssi_dai = &ssi_private->cpu_dai;
  581. dev_attr = &ssi_private->dev_attr;
  582. sprintf(ssi_private->name, "ssi%u", (u8) ssi_info->id);
  583. ssi_private->ssi = ssi_info->ssi;
  584. ssi_private->ssi_phys = ssi_info->ssi_phys;
  585. ssi_private->irq = ssi_info->irq;
  586. ssi_private->dev = ssi_info->dev;
  587. ssi_private->asynchronous = ssi_info->asynchronous;
  588. ssi_private->dev->driver_data = fsl_ssi_dai;
  589. /* Initialize the the device_attribute structure */
  590. dev_attr->attr.name = "ssi-stats";
  591. dev_attr->attr.mode = S_IRUGO;
  592. dev_attr->show = fsl_sysfs_ssi_show;
  593. ret = device_create_file(ssi_private->dev, dev_attr);
  594. if (ret) {
  595. dev_err(ssi_info->dev, "could not create sysfs %s file\n",
  596. ssi_private->dev_attr.attr.name);
  597. kfree(fsl_ssi_dai);
  598. return NULL;
  599. }
  600. fsl_ssi_dai->private_data = ssi_private;
  601. fsl_ssi_dai->name = ssi_private->name;
  602. fsl_ssi_dai->id = ssi_info->id;
  603. fsl_ssi_dai->dev = ssi_info->dev;
  604. ret = snd_soc_register_dai(fsl_ssi_dai);
  605. if (ret != 0) {
  606. dev_err(ssi_info->dev, "failed to register DAI: %d\n", ret);
  607. kfree(fsl_ssi_dai);
  608. return NULL;
  609. }
  610. return fsl_ssi_dai;
  611. }
  612. EXPORT_SYMBOL_GPL(fsl_ssi_create_dai);
  613. /**
  614. * fsl_ssi_destroy_dai: destroy the snd_soc_dai object
  615. *
  616. * This function undoes the operations of fsl_ssi_create_dai()
  617. */
  618. void fsl_ssi_destroy_dai(struct snd_soc_dai *fsl_ssi_dai)
  619. {
  620. struct fsl_ssi_private *ssi_private =
  621. container_of(fsl_ssi_dai, struct fsl_ssi_private, cpu_dai);
  622. device_remove_file(ssi_private->dev, &ssi_private->dev_attr);
  623. snd_soc_unregister_dai(&ssi_private->cpu_dai);
  624. kfree(ssi_private);
  625. }
  626. EXPORT_SYMBOL_GPL(fsl_ssi_destroy_dai);
  627. static int __init fsl_ssi_init(void)
  628. {
  629. printk(KERN_INFO "Freescale Synchronous Serial Interface (SSI) ASoC Driver\n");
  630. return 0;
  631. }
  632. module_init(fsl_ssi_init);
  633. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  634. MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
  635. MODULE_LICENSE("GPL");