fsl_ssi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. /* SIER bitflag of interrupts to enable */
  57. #define SIER_FLAGS (CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | \
  58. CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | \
  59. CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | \
  60. CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | \
  61. CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN)
  62. /**
  63. * fsl_ssi_private: per-SSI private data
  64. *
  65. * @name: short name for this device ("SSI0", "SSI1", etc)
  66. * @ssi: pointer to the SSI's registers
  67. * @ssi_phys: physical address of the SSI registers
  68. * @irq: IRQ of this SSI
  69. * @first_stream: pointer to the stream that was opened first
  70. * @second_stream: pointer to second stream
  71. * @dev: struct device pointer
  72. * @playback: the number of playback streams opened
  73. * @capture: the number of capture streams opened
  74. * @asynchronous: 0=synchronous mode, 1=asynchronous mode
  75. * @cpu_dai: the CPU DAI for this device
  76. * @dev_attr: the sysfs device attribute structure
  77. * @stats: SSI statistics
  78. */
  79. struct fsl_ssi_private {
  80. char name[8];
  81. struct ccsr_ssi __iomem *ssi;
  82. dma_addr_t ssi_phys;
  83. unsigned int irq;
  84. struct snd_pcm_substream *first_stream;
  85. struct snd_pcm_substream *second_stream;
  86. struct device *dev;
  87. unsigned int playback;
  88. unsigned int capture;
  89. int asynchronous;
  90. struct snd_soc_dai cpu_dai;
  91. struct device_attribute dev_attr;
  92. struct {
  93. unsigned int rfrc;
  94. unsigned int tfrc;
  95. unsigned int cmdau;
  96. unsigned int cmddu;
  97. unsigned int rxt;
  98. unsigned int rdr1;
  99. unsigned int rdr0;
  100. unsigned int tde1;
  101. unsigned int tde0;
  102. unsigned int roe1;
  103. unsigned int roe0;
  104. unsigned int tue1;
  105. unsigned int tue0;
  106. unsigned int tfs;
  107. unsigned int rfs;
  108. unsigned int tls;
  109. unsigned int rls;
  110. unsigned int rff1;
  111. unsigned int rff0;
  112. unsigned int tfe1;
  113. unsigned int tfe0;
  114. } stats;
  115. };
  116. /**
  117. * fsl_ssi_isr: SSI interrupt handler
  118. *
  119. * Although it's possible to use the interrupt handler to send and receive
  120. * data to/from the SSI, we use the DMA instead. Programming is more
  121. * complicated, but the performance is much better.
  122. *
  123. * This interrupt handler is used only to gather statistics.
  124. *
  125. * @irq: IRQ of the SSI device
  126. * @dev_id: pointer to the ssi_private structure for this SSI device
  127. */
  128. static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
  129. {
  130. struct fsl_ssi_private *ssi_private = dev_id;
  131. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  132. irqreturn_t ret = IRQ_NONE;
  133. __be32 sisr;
  134. __be32 sisr2 = 0;
  135. /* We got an interrupt, so read the status register to see what we
  136. were interrupted for. We mask it with the Interrupt Enable register
  137. so that we only check for events that we're interested in.
  138. */
  139. sisr = in_be32(&ssi->sisr) & SIER_FLAGS;
  140. if (sisr & CCSR_SSI_SISR_RFRC) {
  141. ssi_private->stats.rfrc++;
  142. sisr2 |= CCSR_SSI_SISR_RFRC;
  143. ret = IRQ_HANDLED;
  144. }
  145. if (sisr & CCSR_SSI_SISR_TFRC) {
  146. ssi_private->stats.tfrc++;
  147. sisr2 |= CCSR_SSI_SISR_TFRC;
  148. ret = IRQ_HANDLED;
  149. }
  150. if (sisr & CCSR_SSI_SISR_CMDAU) {
  151. ssi_private->stats.cmdau++;
  152. ret = IRQ_HANDLED;
  153. }
  154. if (sisr & CCSR_SSI_SISR_CMDDU) {
  155. ssi_private->stats.cmddu++;
  156. ret = IRQ_HANDLED;
  157. }
  158. if (sisr & CCSR_SSI_SISR_RXT) {
  159. ssi_private->stats.rxt++;
  160. ret = IRQ_HANDLED;
  161. }
  162. if (sisr & CCSR_SSI_SISR_RDR1) {
  163. ssi_private->stats.rdr1++;
  164. ret = IRQ_HANDLED;
  165. }
  166. if (sisr & CCSR_SSI_SISR_RDR0) {
  167. ssi_private->stats.rdr0++;
  168. ret = IRQ_HANDLED;
  169. }
  170. if (sisr & CCSR_SSI_SISR_TDE1) {
  171. ssi_private->stats.tde1++;
  172. ret = IRQ_HANDLED;
  173. }
  174. if (sisr & CCSR_SSI_SISR_TDE0) {
  175. ssi_private->stats.tde0++;
  176. ret = IRQ_HANDLED;
  177. }
  178. if (sisr & CCSR_SSI_SISR_ROE1) {
  179. ssi_private->stats.roe1++;
  180. sisr2 |= CCSR_SSI_SISR_ROE1;
  181. ret = IRQ_HANDLED;
  182. }
  183. if (sisr & CCSR_SSI_SISR_ROE0) {
  184. ssi_private->stats.roe0++;
  185. sisr2 |= CCSR_SSI_SISR_ROE0;
  186. ret = IRQ_HANDLED;
  187. }
  188. if (sisr & CCSR_SSI_SISR_TUE1) {
  189. ssi_private->stats.tue1++;
  190. sisr2 |= CCSR_SSI_SISR_TUE1;
  191. ret = IRQ_HANDLED;
  192. }
  193. if (sisr & CCSR_SSI_SISR_TUE0) {
  194. ssi_private->stats.tue0++;
  195. sisr2 |= CCSR_SSI_SISR_TUE0;
  196. ret = IRQ_HANDLED;
  197. }
  198. if (sisr & CCSR_SSI_SISR_TFS) {
  199. ssi_private->stats.tfs++;
  200. ret = IRQ_HANDLED;
  201. }
  202. if (sisr & CCSR_SSI_SISR_RFS) {
  203. ssi_private->stats.rfs++;
  204. ret = IRQ_HANDLED;
  205. }
  206. if (sisr & CCSR_SSI_SISR_TLS) {
  207. ssi_private->stats.tls++;
  208. ret = IRQ_HANDLED;
  209. }
  210. if (sisr & CCSR_SSI_SISR_RLS) {
  211. ssi_private->stats.rls++;
  212. ret = IRQ_HANDLED;
  213. }
  214. if (sisr & CCSR_SSI_SISR_RFF1) {
  215. ssi_private->stats.rff1++;
  216. ret = IRQ_HANDLED;
  217. }
  218. if (sisr & CCSR_SSI_SISR_RFF0) {
  219. ssi_private->stats.rff0++;
  220. ret = IRQ_HANDLED;
  221. }
  222. if (sisr & CCSR_SSI_SISR_TFE1) {
  223. ssi_private->stats.tfe1++;
  224. ret = IRQ_HANDLED;
  225. }
  226. if (sisr & CCSR_SSI_SISR_TFE0) {
  227. ssi_private->stats.tfe0++;
  228. ret = IRQ_HANDLED;
  229. }
  230. /* Clear the bits that we set */
  231. if (sisr2)
  232. out_be32(&ssi->sisr, sisr2);
  233. return ret;
  234. }
  235. /**
  236. * fsl_ssi_startup: create a new substream
  237. *
  238. * This is the first function called when a stream is opened.
  239. *
  240. * If this is the first stream open, then grab the IRQ and program most of
  241. * the SSI registers.
  242. */
  243. static int fsl_ssi_startup(struct snd_pcm_substream *substream,
  244. struct snd_soc_dai *dai)
  245. {
  246. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  247. struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
  248. /*
  249. * If this is the first stream opened, then request the IRQ
  250. * and initialize the SSI registers.
  251. */
  252. if (!ssi_private->playback && !ssi_private->capture) {
  253. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  254. int ret;
  255. ret = request_irq(ssi_private->irq, fsl_ssi_isr, 0,
  256. ssi_private->name, ssi_private);
  257. if (ret < 0) {
  258. dev_err(substream->pcm->card->dev,
  259. "could not claim irq %u\n", ssi_private->irq);
  260. return ret;
  261. }
  262. /*
  263. * Section 16.5 of the MPC8610 reference manual says that the
  264. * SSI needs to be disabled before updating the registers we set
  265. * here.
  266. */
  267. clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
  268. /*
  269. * Program the SSI into I2S Slave Non-Network Synchronous mode.
  270. * Also enable the transmit and receive FIFO.
  271. *
  272. * FIXME: Little-endian samples require a different shift dir
  273. */
  274. clrsetbits_be32(&ssi->scr,
  275. CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN,
  276. CCSR_SSI_SCR_TFR_CLK_DIS | CCSR_SSI_SCR_I2S_MODE_SLAVE
  277. | (ssi_private->asynchronous ? 0 : CCSR_SSI_SCR_SYN));
  278. out_be32(&ssi->stcr,
  279. CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 |
  280. CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS |
  281. CCSR_SSI_STCR_TSCKP);
  282. out_be32(&ssi->srcr,
  283. CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 |
  284. CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS |
  285. CCSR_SSI_SRCR_RSCKP);
  286. /*
  287. * The DC and PM bits are only used if the SSI is the clock
  288. * master.
  289. */
  290. /* 4. Enable the interrupts and DMA requests */
  291. out_be32(&ssi->sier, SIER_FLAGS);
  292. /*
  293. * Set the watermark for transmit FIFI 0 and receive FIFO 0. We
  294. * don't use FIFO 1. Since the SSI only supports stereo, the
  295. * watermark should never be an odd number.
  296. */
  297. out_be32(&ssi->sfcsr,
  298. CCSR_SSI_SFCSR_TFWM0(6) | CCSR_SSI_SFCSR_RFWM0(2));
  299. /*
  300. * We keep the SSI disabled because if we enable it, then the
  301. * DMA controller will start. It's not supposed to start until
  302. * the SCR.TE (or SCR.RE) bit is set, but it does anyway. The
  303. * DMA controller will transfer one "BWC" of data (i.e. the
  304. * amount of data that the MR.BWC bits are set to). The reason
  305. * this is bad is because at this point, the PCM driver has not
  306. * finished initializing the DMA controller.
  307. */
  308. }
  309. if (!ssi_private->first_stream)
  310. ssi_private->first_stream = substream;
  311. else {
  312. /* This is the second stream open, so we need to impose sample
  313. * rate and maybe sample size constraints. Note that this can
  314. * cause a race condition if the second stream is opened before
  315. * the first stream is fully initialized.
  316. *
  317. * We provide some protection by checking to make sure the first
  318. * stream is initialized, but it's not perfect. ALSA sometimes
  319. * re-initializes the driver with a different sample rate or
  320. * size. If the second stream is opened before the first stream
  321. * has received its final parameters, then the second stream may
  322. * be constrained to the wrong sample rate or size.
  323. *
  324. * FIXME: This code does not handle opening and closing streams
  325. * repeatedly. If you open two streams and then close the first
  326. * one, you may not be able to open another stream until you
  327. * close the second one as well.
  328. */
  329. struct snd_pcm_runtime *first_runtime =
  330. ssi_private->first_stream->runtime;
  331. if (!first_runtime->sample_bits) {
  332. dev_err(substream->pcm->card->dev,
  333. "set sample size in %s stream first\n",
  334. substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  335. ? "capture" : "playback");
  336. return -EAGAIN;
  337. }
  338. /* If we're in synchronous mode, then we need to constrain
  339. * the sample size as well. We don't support independent sample
  340. * rates in asynchronous mode.
  341. */
  342. if (!ssi_private->asynchronous)
  343. snd_pcm_hw_constraint_minmax(substream->runtime,
  344. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  345. first_runtime->sample_bits,
  346. first_runtime->sample_bits);
  347. ssi_private->second_stream = substream;
  348. }
  349. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  350. ssi_private->playback++;
  351. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  352. ssi_private->capture++;
  353. return 0;
  354. }
  355. /**
  356. * fsl_ssi_hw_params - program the sample size
  357. *
  358. * Most of the SSI registers have been programmed in the startup function,
  359. * but the word length must be programmed here. Unfortunately, programming
  360. * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
  361. * cause a problem with supporting simultaneous playback and capture. If
  362. * the SSI is already playing a stream, then that stream may be temporarily
  363. * stopped when you start capture.
  364. *
  365. * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
  366. * clock master.
  367. */
  368. static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
  369. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
  370. {
  371. struct fsl_ssi_private *ssi_private = cpu_dai->private_data;
  372. if (substream == ssi_private->first_stream) {
  373. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  374. unsigned int sample_size =
  375. snd_pcm_format_width(params_format(hw_params));
  376. u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
  377. /* The SSI should always be disabled at this points (SSIEN=0) */
  378. /* In synchronous mode, the SSI uses STCCR for capture */
  379. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
  380. !ssi_private->asynchronous)
  381. clrsetbits_be32(&ssi->stccr,
  382. CCSR_SSI_SxCCR_WL_MASK, wl);
  383. else
  384. clrsetbits_be32(&ssi->srccr,
  385. CCSR_SSI_SxCCR_WL_MASK, wl);
  386. }
  387. return 0;
  388. }
  389. /**
  390. * fsl_ssi_trigger: start and stop the DMA transfer.
  391. *
  392. * This function is called by ALSA to start, stop, pause, and resume the DMA
  393. * transfer of data.
  394. *
  395. * The DMA channel is in external master start and pause mode, which
  396. * means the SSI completely controls the flow of data.
  397. */
  398. static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
  399. struct snd_soc_dai *dai)
  400. {
  401. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  402. struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
  403. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  404. switch (cmd) {
  405. case SNDRV_PCM_TRIGGER_START:
  406. clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
  407. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  408. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  409. setbits32(&ssi->scr,
  410. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE);
  411. else
  412. setbits32(&ssi->scr,
  413. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);
  414. break;
  415. case SNDRV_PCM_TRIGGER_STOP:
  416. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  417. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  418. clrbits32(&ssi->scr, CCSR_SSI_SCR_TE);
  419. else
  420. clrbits32(&ssi->scr, CCSR_SSI_SCR_RE);
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. return 0;
  426. }
  427. /**
  428. * fsl_ssi_shutdown: shutdown the SSI
  429. *
  430. * Shutdown the SSI if there are no other substreams open.
  431. */
  432. static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
  433. struct snd_soc_dai *dai)
  434. {
  435. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  436. struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
  437. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  438. ssi_private->playback--;
  439. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  440. ssi_private->capture--;
  441. if (ssi_private->first_stream == substream)
  442. ssi_private->first_stream = ssi_private->second_stream;
  443. ssi_private->second_stream = NULL;
  444. /*
  445. * If this is the last active substream, disable the SSI and release
  446. * the IRQ.
  447. */
  448. if (!ssi_private->playback && !ssi_private->capture) {
  449. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  450. clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
  451. free_irq(ssi_private->irq, ssi_private);
  452. }
  453. }
  454. /**
  455. * fsl_ssi_set_sysclk: set the clock frequency and direction
  456. *
  457. * This function is called by the machine driver to tell us what the clock
  458. * frequency and direction are.
  459. *
  460. * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN),
  461. * and we don't care about the frequency. Return an error if the direction
  462. * is not SND_SOC_CLOCK_IN.
  463. *
  464. * @clk_id: reserved, should be zero
  465. * @freq: the frequency of the given clock ID, currently ignored
  466. * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master)
  467. */
  468. static int fsl_ssi_set_sysclk(struct snd_soc_dai *cpu_dai,
  469. int clk_id, unsigned int freq, int dir)
  470. {
  471. return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL;
  472. }
  473. /**
  474. * fsl_ssi_set_fmt: set the serial format.
  475. *
  476. * This function is called by the machine driver to tell us what serial
  477. * format to use.
  478. *
  479. * Currently, we only support I2S mode. Return an error if the format is
  480. * not SND_SOC_DAIFMT_I2S.
  481. *
  482. * @format: one of SND_SOC_DAIFMT_xxx
  483. */
  484. static int fsl_ssi_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
  485. {
  486. return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL;
  487. }
  488. /**
  489. * fsl_ssi_dai_template: template CPU DAI for the SSI
  490. */
  491. static struct snd_soc_dai_ops fsl_ssi_dai_ops = {
  492. .startup = fsl_ssi_startup,
  493. .hw_params = fsl_ssi_hw_params,
  494. .shutdown = fsl_ssi_shutdown,
  495. .trigger = fsl_ssi_trigger,
  496. .set_sysclk = fsl_ssi_set_sysclk,
  497. .set_fmt = fsl_ssi_set_fmt,
  498. };
  499. static struct snd_soc_dai fsl_ssi_dai_template = {
  500. .playback = {
  501. /* The SSI does not support monaural audio. */
  502. .channels_min = 2,
  503. .channels_max = 2,
  504. .rates = FSLSSI_I2S_RATES,
  505. .formats = FSLSSI_I2S_FORMATS,
  506. },
  507. .capture = {
  508. .channels_min = 2,
  509. .channels_max = 2,
  510. .rates = FSLSSI_I2S_RATES,
  511. .formats = FSLSSI_I2S_FORMATS,
  512. },
  513. .ops = &fsl_ssi_dai_ops,
  514. };
  515. /* Show the statistics of a flag only if its interrupt is enabled. The
  516. * compiler will optimze this code to a no-op if the interrupt is not
  517. * enabled.
  518. */
  519. #define SIER_SHOW(flag, name) \
  520. do { \
  521. if (SIER_FLAGS & CCSR_SSI_SIER_##flag) \
  522. length += sprintf(buf + length, #name "=%u\n", \
  523. ssi_private->stats.name); \
  524. } while (0)
  525. /**
  526. * fsl_sysfs_ssi_show: display SSI statistics
  527. *
  528. * Display the statistics for the current SSI device. To avoid confusion,
  529. * we only show those counts that are enabled.
  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 = 0;
  537. SIER_SHOW(RFRC_EN, rfrc);
  538. SIER_SHOW(TFRC_EN, tfrc);
  539. SIER_SHOW(CMDAU_EN, cmdau);
  540. SIER_SHOW(CMDDU_EN, cmddu);
  541. SIER_SHOW(RXT_EN, rxt);
  542. SIER_SHOW(RDR1_EN, rdr1);
  543. SIER_SHOW(RDR0_EN, rdr0);
  544. SIER_SHOW(TDE1_EN, tde1);
  545. SIER_SHOW(TDE0_EN, tde0);
  546. SIER_SHOW(ROE1_EN, roe1);
  547. SIER_SHOW(ROE0_EN, roe0);
  548. SIER_SHOW(TUE1_EN, tue1);
  549. SIER_SHOW(TUE0_EN, tue0);
  550. SIER_SHOW(TFS_EN, tfs);
  551. SIER_SHOW(RFS_EN, rfs);
  552. SIER_SHOW(TLS_EN, tls);
  553. SIER_SHOW(RLS_EN, rls);
  554. SIER_SHOW(RFF1_EN, rff1);
  555. SIER_SHOW(RFF0_EN, rff0);
  556. SIER_SHOW(TFE1_EN, tfe1);
  557. SIER_SHOW(TFE0_EN, 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. dev_set_drvdata(ssi_private->dev, 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. fsl_ssi_dai->symmetric_rates = 1;
  605. ret = snd_soc_register_dai(fsl_ssi_dai);
  606. if (ret != 0) {
  607. dev_err(ssi_info->dev, "failed to register DAI: %d\n", ret);
  608. kfree(fsl_ssi_dai);
  609. return NULL;
  610. }
  611. return fsl_ssi_dai;
  612. }
  613. EXPORT_SYMBOL_GPL(fsl_ssi_create_dai);
  614. /**
  615. * fsl_ssi_destroy_dai: destroy the snd_soc_dai object
  616. *
  617. * This function undoes the operations of fsl_ssi_create_dai()
  618. */
  619. void fsl_ssi_destroy_dai(struct snd_soc_dai *fsl_ssi_dai)
  620. {
  621. struct fsl_ssi_private *ssi_private =
  622. container_of(fsl_ssi_dai, struct fsl_ssi_private, cpu_dai);
  623. device_remove_file(ssi_private->dev, &ssi_private->dev_attr);
  624. snd_soc_unregister_dai(&ssi_private->cpu_dai);
  625. kfree(ssi_private);
  626. }
  627. EXPORT_SYMBOL_GPL(fsl_ssi_destroy_dai);
  628. static int __init fsl_ssi_init(void)
  629. {
  630. printk(KERN_INFO "Freescale Synchronous Serial Interface (SSI) ASoC Driver\n");
  631. return 0;
  632. }
  633. module_init(fsl_ssi_init);
  634. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  635. MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
  636. MODULE_LICENSE("GPL");