fsl_ssi.c 20 KB

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