fsl_ssi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007-2010 Freescale Semiconductor, Inc.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/clk.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/initval.h>
  27. #include <sound/soc.h>
  28. #include <sound/dmaengine_pcm.h>
  29. #include "fsl_ssi.h"
  30. #include "imx-pcm.h"
  31. #ifdef PPC
  32. #define read_ssi(addr) in_be32(addr)
  33. #define write_ssi(val, addr) out_be32(addr, val)
  34. #define write_ssi_mask(addr, clear, set) clrsetbits_be32(addr, clear, set)
  35. #elif defined ARM
  36. #define read_ssi(addr) readl(addr)
  37. #define write_ssi(val, addr) writel(val, addr)
  38. /*
  39. * FIXME: Proper locking should be added at write_ssi_mask caller level
  40. * to ensure this register read/modify/write sequence is race free.
  41. */
  42. static inline void write_ssi_mask(u32 __iomem *addr, u32 clear, u32 set)
  43. {
  44. u32 val = readl(addr);
  45. val = (val & ~clear) | set;
  46. writel(val, addr);
  47. }
  48. #endif
  49. /**
  50. * FSLSSI_I2S_RATES: sample rates supported by the I2S
  51. *
  52. * This driver currently only supports the SSI running in I2S slave mode,
  53. * which means the codec determines the sample rate. Therefore, we tell
  54. * ALSA that we support all rates and let the codec driver decide what rates
  55. * are really supported.
  56. */
  57. #define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
  58. SNDRV_PCM_RATE_CONTINUOUS)
  59. /**
  60. * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
  61. *
  62. * This driver currently only supports the SSI running in I2S slave mode.
  63. *
  64. * The SSI has a limitation in that the samples must be in the same byte
  65. * order as the host CPU. This is because when multiple bytes are written
  66. * to the STX register, the bytes and bits must be written in the same
  67. * order. The STX is a shift register, so all the bits need to be aligned
  68. * (bit-endianness must match byte-endianness). Processors typically write
  69. * the bits within a byte in the same order that the bytes of a word are
  70. * written in. So if the host CPU is big-endian, then only big-endian
  71. * samples will be written to STX properly.
  72. */
  73. #ifdef __BIG_ENDIAN
  74. #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
  75. SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
  76. SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
  77. #else
  78. #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
  79. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  80. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
  81. #endif
  82. /* SIER bitflag of interrupts to enable */
  83. #define SIER_FLAGS (CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | \
  84. CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | \
  85. CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | \
  86. CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | \
  87. CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN)
  88. /**
  89. * fsl_ssi_private: per-SSI private data
  90. *
  91. * @ssi: pointer to the SSI's registers
  92. * @ssi_phys: physical address of the SSI registers
  93. * @irq: IRQ of this SSI
  94. * @first_stream: pointer to the stream that was opened first
  95. * @second_stream: pointer to second stream
  96. * @playback: the number of playback streams opened
  97. * @capture: the number of capture streams opened
  98. * @cpu_dai: the CPU DAI for this device
  99. * @dev_attr: the sysfs device attribute structure
  100. * @stats: SSI statistics
  101. * @name: name for this device
  102. */
  103. struct fsl_ssi_private {
  104. struct ccsr_ssi __iomem *ssi;
  105. dma_addr_t ssi_phys;
  106. unsigned int irq;
  107. struct snd_pcm_substream *first_stream;
  108. struct snd_pcm_substream *second_stream;
  109. unsigned int fifo_depth;
  110. struct snd_soc_dai_driver cpu_dai_drv;
  111. struct device_attribute dev_attr;
  112. struct platform_device *pdev;
  113. bool new_binding;
  114. bool ssi_on_imx;
  115. struct clk *clk;
  116. struct snd_dmaengine_dai_dma_data dma_params_tx;
  117. struct snd_dmaengine_dai_dma_data dma_params_rx;
  118. struct imx_dma_data filter_data_tx;
  119. struct imx_dma_data filter_data_rx;
  120. struct {
  121. unsigned int rfrc;
  122. unsigned int tfrc;
  123. unsigned int cmdau;
  124. unsigned int cmddu;
  125. unsigned int rxt;
  126. unsigned int rdr1;
  127. unsigned int rdr0;
  128. unsigned int tde1;
  129. unsigned int tde0;
  130. unsigned int roe1;
  131. unsigned int roe0;
  132. unsigned int tue1;
  133. unsigned int tue0;
  134. unsigned int tfs;
  135. unsigned int rfs;
  136. unsigned int tls;
  137. unsigned int rls;
  138. unsigned int rff1;
  139. unsigned int rff0;
  140. unsigned int tfe1;
  141. unsigned int tfe0;
  142. } stats;
  143. char name[1];
  144. };
  145. /**
  146. * fsl_ssi_isr: SSI interrupt handler
  147. *
  148. * Although it's possible to use the interrupt handler to send and receive
  149. * data to/from the SSI, we use the DMA instead. Programming is more
  150. * complicated, but the performance is much better.
  151. *
  152. * This interrupt handler is used only to gather statistics.
  153. *
  154. * @irq: IRQ of the SSI device
  155. * @dev_id: pointer to the ssi_private structure for this SSI device
  156. */
  157. static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
  158. {
  159. struct fsl_ssi_private *ssi_private = dev_id;
  160. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  161. irqreturn_t ret = IRQ_NONE;
  162. __be32 sisr;
  163. __be32 sisr2 = 0;
  164. /* We got an interrupt, so read the status register to see what we
  165. were interrupted for. We mask it with the Interrupt Enable register
  166. so that we only check for events that we're interested in.
  167. */
  168. sisr = read_ssi(&ssi->sisr) & SIER_FLAGS;
  169. if (sisr & CCSR_SSI_SISR_RFRC) {
  170. ssi_private->stats.rfrc++;
  171. sisr2 |= CCSR_SSI_SISR_RFRC;
  172. ret = IRQ_HANDLED;
  173. }
  174. if (sisr & CCSR_SSI_SISR_TFRC) {
  175. ssi_private->stats.tfrc++;
  176. sisr2 |= CCSR_SSI_SISR_TFRC;
  177. ret = IRQ_HANDLED;
  178. }
  179. if (sisr & CCSR_SSI_SISR_CMDAU) {
  180. ssi_private->stats.cmdau++;
  181. ret = IRQ_HANDLED;
  182. }
  183. if (sisr & CCSR_SSI_SISR_CMDDU) {
  184. ssi_private->stats.cmddu++;
  185. ret = IRQ_HANDLED;
  186. }
  187. if (sisr & CCSR_SSI_SISR_RXT) {
  188. ssi_private->stats.rxt++;
  189. ret = IRQ_HANDLED;
  190. }
  191. if (sisr & CCSR_SSI_SISR_RDR1) {
  192. ssi_private->stats.rdr1++;
  193. ret = IRQ_HANDLED;
  194. }
  195. if (sisr & CCSR_SSI_SISR_RDR0) {
  196. ssi_private->stats.rdr0++;
  197. ret = IRQ_HANDLED;
  198. }
  199. if (sisr & CCSR_SSI_SISR_TDE1) {
  200. ssi_private->stats.tde1++;
  201. ret = IRQ_HANDLED;
  202. }
  203. if (sisr & CCSR_SSI_SISR_TDE0) {
  204. ssi_private->stats.tde0++;
  205. ret = IRQ_HANDLED;
  206. }
  207. if (sisr & CCSR_SSI_SISR_ROE1) {
  208. ssi_private->stats.roe1++;
  209. sisr2 |= CCSR_SSI_SISR_ROE1;
  210. ret = IRQ_HANDLED;
  211. }
  212. if (sisr & CCSR_SSI_SISR_ROE0) {
  213. ssi_private->stats.roe0++;
  214. sisr2 |= CCSR_SSI_SISR_ROE0;
  215. ret = IRQ_HANDLED;
  216. }
  217. if (sisr & CCSR_SSI_SISR_TUE1) {
  218. ssi_private->stats.tue1++;
  219. sisr2 |= CCSR_SSI_SISR_TUE1;
  220. ret = IRQ_HANDLED;
  221. }
  222. if (sisr & CCSR_SSI_SISR_TUE0) {
  223. ssi_private->stats.tue0++;
  224. sisr2 |= CCSR_SSI_SISR_TUE0;
  225. ret = IRQ_HANDLED;
  226. }
  227. if (sisr & CCSR_SSI_SISR_TFS) {
  228. ssi_private->stats.tfs++;
  229. ret = IRQ_HANDLED;
  230. }
  231. if (sisr & CCSR_SSI_SISR_RFS) {
  232. ssi_private->stats.rfs++;
  233. ret = IRQ_HANDLED;
  234. }
  235. if (sisr & CCSR_SSI_SISR_TLS) {
  236. ssi_private->stats.tls++;
  237. ret = IRQ_HANDLED;
  238. }
  239. if (sisr & CCSR_SSI_SISR_RLS) {
  240. ssi_private->stats.rls++;
  241. ret = IRQ_HANDLED;
  242. }
  243. if (sisr & CCSR_SSI_SISR_RFF1) {
  244. ssi_private->stats.rff1++;
  245. ret = IRQ_HANDLED;
  246. }
  247. if (sisr & CCSR_SSI_SISR_RFF0) {
  248. ssi_private->stats.rff0++;
  249. ret = IRQ_HANDLED;
  250. }
  251. if (sisr & CCSR_SSI_SISR_TFE1) {
  252. ssi_private->stats.tfe1++;
  253. ret = IRQ_HANDLED;
  254. }
  255. if (sisr & CCSR_SSI_SISR_TFE0) {
  256. ssi_private->stats.tfe0++;
  257. ret = IRQ_HANDLED;
  258. }
  259. /* Clear the bits that we set */
  260. if (sisr2)
  261. write_ssi(sisr2, &ssi->sisr);
  262. return ret;
  263. }
  264. /**
  265. * fsl_ssi_startup: create a new substream
  266. *
  267. * This is the first function called when a stream is opened.
  268. *
  269. * If this is the first stream open, then grab the IRQ and program most of
  270. * the SSI registers.
  271. */
  272. static int fsl_ssi_startup(struct snd_pcm_substream *substream,
  273. struct snd_soc_dai *dai)
  274. {
  275. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  276. struct fsl_ssi_private *ssi_private =
  277. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  278. int synchronous = ssi_private->cpu_dai_drv.symmetric_rates;
  279. /*
  280. * If this is the first stream opened, then request the IRQ
  281. * and initialize the SSI registers.
  282. */
  283. if (!ssi_private->first_stream) {
  284. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  285. ssi_private->first_stream = substream;
  286. /*
  287. * Section 16.5 of the MPC8610 reference manual says that the
  288. * SSI needs to be disabled before updating the registers we set
  289. * here.
  290. */
  291. write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0);
  292. /*
  293. * Program the SSI into I2S Slave Non-Network Synchronous mode.
  294. * Also enable the transmit and receive FIFO.
  295. *
  296. * FIXME: Little-endian samples require a different shift dir
  297. */
  298. write_ssi_mask(&ssi->scr,
  299. CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN,
  300. CCSR_SSI_SCR_TFR_CLK_DIS | CCSR_SSI_SCR_I2S_MODE_SLAVE
  301. | (synchronous ? CCSR_SSI_SCR_SYN : 0));
  302. write_ssi(CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 |
  303. CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS |
  304. CCSR_SSI_STCR_TSCKP, &ssi->stcr);
  305. write_ssi(CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 |
  306. CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS |
  307. CCSR_SSI_SRCR_RSCKP, &ssi->srcr);
  308. /*
  309. * The DC and PM bits are only used if the SSI is the clock
  310. * master.
  311. */
  312. /* Enable the interrupts and DMA requests */
  313. write_ssi(SIER_FLAGS, &ssi->sier);
  314. /*
  315. * Set the watermark for transmit FIFI 0 and receive FIFO 0. We
  316. * don't use FIFO 1. We program the transmit water to signal a
  317. * DMA transfer if there are only two (or fewer) elements left
  318. * in the FIFO. Two elements equals one frame (left channel,
  319. * right channel). This value, however, depends on the depth of
  320. * the transmit buffer.
  321. *
  322. * We program the receive FIFO to notify us if at least two
  323. * elements (one frame) have been written to the FIFO. We could
  324. * make this value larger (and maybe we should), but this way
  325. * data will be written to memory as soon as it's available.
  326. */
  327. write_ssi(CCSR_SSI_SFCSR_TFWM0(ssi_private->fifo_depth - 2) |
  328. CCSR_SSI_SFCSR_RFWM0(ssi_private->fifo_depth - 2),
  329. &ssi->sfcsr);
  330. /*
  331. * We keep the SSI disabled because if we enable it, then the
  332. * DMA controller will start. It's not supposed to start until
  333. * the SCR.TE (or SCR.RE) bit is set, but it does anyway. The
  334. * DMA controller will transfer one "BWC" of data (i.e. the
  335. * amount of data that the MR.BWC bits are set to). The reason
  336. * this is bad is because at this point, the PCM driver has not
  337. * finished initializing the DMA controller.
  338. */
  339. } else {
  340. if (synchronous) {
  341. struct snd_pcm_runtime *first_runtime =
  342. ssi_private->first_stream->runtime;
  343. /*
  344. * This is the second stream open, and we're in
  345. * synchronous mode, so we need to impose sample
  346. * sample size constraints. This is because STCCR is
  347. * used for playback and capture in synchronous mode,
  348. * so there's no way to specify different word
  349. * lengths.
  350. *
  351. * Note that this can cause a race condition if the
  352. * second stream is opened before the first stream is
  353. * fully initialized. We provide some protection by
  354. * checking to make sure the first stream is
  355. * initialized, but it's not perfect. ALSA sometimes
  356. * re-initializes the driver with a different sample
  357. * rate or size. If the second stream is opened
  358. * before the first stream has received its final
  359. * parameters, then the second stream may be
  360. * constrained to the wrong sample rate or size.
  361. */
  362. if (!first_runtime->sample_bits) {
  363. dev_err(substream->pcm->card->dev,
  364. "set sample size in %s stream first\n",
  365. substream->stream ==
  366. SNDRV_PCM_STREAM_PLAYBACK
  367. ? "capture" : "playback");
  368. return -EAGAIN;
  369. }
  370. snd_pcm_hw_constraint_minmax(substream->runtime,
  371. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  372. first_runtime->sample_bits,
  373. first_runtime->sample_bits);
  374. }
  375. ssi_private->second_stream = substream;
  376. }
  377. return 0;
  378. }
  379. /**
  380. * fsl_ssi_hw_params - program the sample size
  381. *
  382. * Most of the SSI registers have been programmed in the startup function,
  383. * but the word length must be programmed here. Unfortunately, programming
  384. * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
  385. * cause a problem with supporting simultaneous playback and capture. If
  386. * the SSI is already playing a stream, then that stream may be temporarily
  387. * stopped when you start capture.
  388. *
  389. * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
  390. * clock master.
  391. */
  392. static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
  393. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
  394. {
  395. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
  396. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  397. unsigned int sample_size =
  398. snd_pcm_format_width(params_format(hw_params));
  399. u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
  400. int enabled = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
  401. /*
  402. * If we're in synchronous mode, and the SSI is already enabled,
  403. * then STCCR is already set properly.
  404. */
  405. if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
  406. return 0;
  407. /*
  408. * FIXME: The documentation says that SxCCR[WL] should not be
  409. * modified while the SSI is enabled. The only time this can
  410. * happen is if we're trying to do simultaneous playback and
  411. * capture in asynchronous mode. Unfortunately, I have been enable
  412. * to get that to work at all on the P1022DS. Therefore, we don't
  413. * bother to disable/enable the SSI when setting SxCCR[WL], because
  414. * the SSI will stop anyway. Maybe one day, this will get fixed.
  415. */
  416. /* In synchronous mode, the SSI uses STCCR for capture */
  417. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
  418. ssi_private->cpu_dai_drv.symmetric_rates)
  419. write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_WL_MASK, wl);
  420. else
  421. write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_WL_MASK, wl);
  422. return 0;
  423. }
  424. /**
  425. * fsl_ssi_trigger: start and stop the DMA transfer.
  426. *
  427. * This function is called by ALSA to start, stop, pause, and resume the DMA
  428. * transfer of data.
  429. *
  430. * The DMA channel is in external master start and pause mode, which
  431. * means the SSI completely controls the flow of data.
  432. */
  433. static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
  434. struct snd_soc_dai *dai)
  435. {
  436. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  437. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  438. struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
  439. switch (cmd) {
  440. case SNDRV_PCM_TRIGGER_START:
  441. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  442. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  443. write_ssi_mask(&ssi->scr, 0,
  444. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE);
  445. else
  446. write_ssi_mask(&ssi->scr, 0,
  447. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);
  448. break;
  449. case SNDRV_PCM_TRIGGER_STOP:
  450. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  451. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  452. write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_TE, 0);
  453. else
  454. write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_RE, 0);
  455. if ((read_ssi(&ssi->scr) & (CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE)) == 0)
  456. write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0);
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. return 0;
  462. }
  463. /**
  464. * fsl_ssi_shutdown: shutdown the SSI
  465. *
  466. * Shutdown the SSI if there are no other substreams open.
  467. */
  468. static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
  469. struct snd_soc_dai *dai)
  470. {
  471. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  472. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  473. if (ssi_private->first_stream == substream)
  474. ssi_private->first_stream = ssi_private->second_stream;
  475. ssi_private->second_stream = NULL;
  476. }
  477. static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
  478. {
  479. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
  480. if (ssi_private->ssi_on_imx) {
  481. dai->playback_dma_data = &ssi_private->dma_params_tx;
  482. dai->capture_dma_data = &ssi_private->dma_params_rx;
  483. }
  484. return 0;
  485. }
  486. static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
  487. .startup = fsl_ssi_startup,
  488. .hw_params = fsl_ssi_hw_params,
  489. .shutdown = fsl_ssi_shutdown,
  490. .trigger = fsl_ssi_trigger,
  491. };
  492. /* Template for the CPU dai driver structure */
  493. static struct snd_soc_dai_driver fsl_ssi_dai_template = {
  494. .probe = fsl_ssi_dai_probe,
  495. .playback = {
  496. /* The SSI does not support monaural audio. */
  497. .channels_min = 2,
  498. .channels_max = 2,
  499. .rates = FSLSSI_I2S_RATES,
  500. .formats = FSLSSI_I2S_FORMATS,
  501. },
  502. .capture = {
  503. .channels_min = 2,
  504. .channels_max = 2,
  505. .rates = FSLSSI_I2S_RATES,
  506. .formats = FSLSSI_I2S_FORMATS,
  507. },
  508. .ops = &fsl_ssi_dai_ops,
  509. };
  510. static const struct snd_soc_component_driver fsl_ssi_component = {
  511. .name = "fsl-ssi",
  512. };
  513. /* Show the statistics of a flag only if its interrupt is enabled. The
  514. * compiler will optimze this code to a no-op if the interrupt is not
  515. * enabled.
  516. */
  517. #define SIER_SHOW(flag, name) \
  518. do { \
  519. if (SIER_FLAGS & CCSR_SSI_SIER_##flag) \
  520. length += sprintf(buf + length, #name "=%u\n", \
  521. ssi_private->stats.name); \
  522. } while (0)
  523. /**
  524. * fsl_sysfs_ssi_show: display SSI statistics
  525. *
  526. * Display the statistics for the current SSI device. To avoid confusion,
  527. * we only show those counts that are enabled.
  528. */
  529. static ssize_t fsl_sysfs_ssi_show(struct device *dev,
  530. struct device_attribute *attr, char *buf)
  531. {
  532. struct fsl_ssi_private *ssi_private =
  533. container_of(attr, struct fsl_ssi_private, dev_attr);
  534. ssize_t length = 0;
  535. SIER_SHOW(RFRC_EN, rfrc);
  536. SIER_SHOW(TFRC_EN, tfrc);
  537. SIER_SHOW(CMDAU_EN, cmdau);
  538. SIER_SHOW(CMDDU_EN, cmddu);
  539. SIER_SHOW(RXT_EN, rxt);
  540. SIER_SHOW(RDR1_EN, rdr1);
  541. SIER_SHOW(RDR0_EN, rdr0);
  542. SIER_SHOW(TDE1_EN, tde1);
  543. SIER_SHOW(TDE0_EN, tde0);
  544. SIER_SHOW(ROE1_EN, roe1);
  545. SIER_SHOW(ROE0_EN, roe0);
  546. SIER_SHOW(TUE1_EN, tue1);
  547. SIER_SHOW(TUE0_EN, tue0);
  548. SIER_SHOW(TFS_EN, tfs);
  549. SIER_SHOW(RFS_EN, rfs);
  550. SIER_SHOW(TLS_EN, tls);
  551. SIER_SHOW(RLS_EN, rls);
  552. SIER_SHOW(RFF1_EN, rff1);
  553. SIER_SHOW(RFF0_EN, rff0);
  554. SIER_SHOW(TFE1_EN, tfe1);
  555. SIER_SHOW(TFE0_EN, tfe0);
  556. return length;
  557. }
  558. /**
  559. * Make every character in a string lower-case
  560. */
  561. static void make_lowercase(char *s)
  562. {
  563. char *p = s;
  564. char c;
  565. while ((c = *p)) {
  566. if ((c >= 'A') && (c <= 'Z'))
  567. *p = c + ('a' - 'A');
  568. p++;
  569. }
  570. }
  571. static int fsl_ssi_probe(struct platform_device *pdev)
  572. {
  573. struct fsl_ssi_private *ssi_private;
  574. int ret = 0;
  575. struct device_attribute *dev_attr = NULL;
  576. struct device_node *np = pdev->dev.of_node;
  577. const char *p, *sprop;
  578. const uint32_t *iprop;
  579. struct resource res;
  580. char name[64];
  581. bool shared;
  582. /* SSIs that are not connected on the board should have a
  583. * status = "disabled"
  584. * property in their device tree nodes.
  585. */
  586. if (!of_device_is_available(np))
  587. return -ENODEV;
  588. /* We only support the SSI in "I2S Slave" mode */
  589. sprop = of_get_property(np, "fsl,mode", NULL);
  590. if (!sprop || strcmp(sprop, "i2s-slave")) {
  591. dev_notice(&pdev->dev, "mode %s is unsupported\n", sprop);
  592. return -ENODEV;
  593. }
  594. /* The DAI name is the last part of the full name of the node. */
  595. p = strrchr(np->full_name, '/') + 1;
  596. ssi_private = kzalloc(sizeof(struct fsl_ssi_private) + strlen(p),
  597. GFP_KERNEL);
  598. if (!ssi_private) {
  599. dev_err(&pdev->dev, "could not allocate DAI object\n");
  600. return -ENOMEM;
  601. }
  602. strcpy(ssi_private->name, p);
  603. /* Initialize this copy of the CPU DAI driver structure */
  604. memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
  605. sizeof(fsl_ssi_dai_template));
  606. ssi_private->cpu_dai_drv.name = ssi_private->name;
  607. /* Get the addresses and IRQ */
  608. ret = of_address_to_resource(np, 0, &res);
  609. if (ret) {
  610. dev_err(&pdev->dev, "could not determine device resources\n");
  611. goto error_kmalloc;
  612. }
  613. ssi_private->ssi = of_iomap(np, 0);
  614. if (!ssi_private->ssi) {
  615. dev_err(&pdev->dev, "could not map device resources\n");
  616. ret = -ENOMEM;
  617. goto error_kmalloc;
  618. }
  619. ssi_private->ssi_phys = res.start;
  620. ssi_private->irq = irq_of_parse_and_map(np, 0);
  621. if (ssi_private->irq == NO_IRQ) {
  622. dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
  623. ret = -ENXIO;
  624. goto error_iomap;
  625. }
  626. /* The 'name' should not have any slashes in it. */
  627. ret = request_irq(ssi_private->irq, fsl_ssi_isr, 0, ssi_private->name,
  628. ssi_private);
  629. if (ret < 0) {
  630. dev_err(&pdev->dev, "could not claim irq %u\n", ssi_private->irq);
  631. goto error_irqmap;
  632. }
  633. /* Are the RX and the TX clocks locked? */
  634. if (!of_find_property(np, "fsl,ssi-asynchronous", NULL))
  635. ssi_private->cpu_dai_drv.symmetric_rates = 1;
  636. /* Determine the FIFO depth. */
  637. iprop = of_get_property(np, "fsl,fifo-depth", NULL);
  638. if (iprop)
  639. ssi_private->fifo_depth = be32_to_cpup(iprop);
  640. else
  641. /* Older 8610 DTs didn't have the fifo-depth property */
  642. ssi_private->fifo_depth = 8;
  643. if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx21-ssi")) {
  644. u32 dma_events[2];
  645. ssi_private->ssi_on_imx = true;
  646. ssi_private->clk = clk_get(&pdev->dev, NULL);
  647. if (IS_ERR(ssi_private->clk)) {
  648. ret = PTR_ERR(ssi_private->clk);
  649. dev_err(&pdev->dev, "could not get clock: %d\n", ret);
  650. goto error_irq;
  651. }
  652. clk_prepare_enable(ssi_private->clk);
  653. /*
  654. * We have burstsize be "fifo_depth - 2" to match the SSI
  655. * watermark setting in fsl_ssi_startup().
  656. */
  657. ssi_private->dma_params_tx.maxburst =
  658. ssi_private->fifo_depth - 2;
  659. ssi_private->dma_params_rx.maxburst =
  660. ssi_private->fifo_depth - 2;
  661. ssi_private->dma_params_tx.addr =
  662. ssi_private->ssi_phys + offsetof(struct ccsr_ssi, stx0);
  663. ssi_private->dma_params_rx.addr =
  664. ssi_private->ssi_phys + offsetof(struct ccsr_ssi, srx0);
  665. ssi_private->dma_params_tx.filter_data =
  666. &ssi_private->filter_data_tx;
  667. ssi_private->dma_params_rx.filter_data =
  668. &ssi_private->filter_data_rx;
  669. /*
  670. * TODO: This is a temporary solution and should be changed
  671. * to use generic DMA binding later when the helplers get in.
  672. */
  673. ret = of_property_read_u32_array(pdev->dev.of_node,
  674. "fsl,ssi-dma-events", dma_events, 2);
  675. if (ret) {
  676. dev_err(&pdev->dev, "could not get dma events\n");
  677. goto error_clk;
  678. }
  679. shared = of_device_is_compatible(of_get_parent(np),
  680. "fsl,spba-bus");
  681. imx_pcm_dma_params_init_data(&ssi_private->filter_data_tx,
  682. dma_events[0], shared);
  683. imx_pcm_dma_params_init_data(&ssi_private->filter_data_rx,
  684. dma_events[1], shared);
  685. }
  686. /* Initialize the the device_attribute structure */
  687. dev_attr = &ssi_private->dev_attr;
  688. sysfs_attr_init(&dev_attr->attr);
  689. dev_attr->attr.name = "statistics";
  690. dev_attr->attr.mode = S_IRUGO;
  691. dev_attr->show = fsl_sysfs_ssi_show;
  692. ret = device_create_file(&pdev->dev, dev_attr);
  693. if (ret) {
  694. dev_err(&pdev->dev, "could not create sysfs %s file\n",
  695. ssi_private->dev_attr.attr.name);
  696. goto error_irq;
  697. }
  698. /* Register with ASoC */
  699. dev_set_drvdata(&pdev->dev, ssi_private);
  700. ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
  701. &ssi_private->cpu_dai_drv, 1);
  702. if (ret) {
  703. dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
  704. goto error_dev;
  705. }
  706. if (ssi_private->ssi_on_imx) {
  707. ret = imx_pcm_dma_init(pdev);
  708. if (ret)
  709. goto error_dev;
  710. }
  711. /*
  712. * If codec-handle property is missing from SSI node, we assume
  713. * that the machine driver uses new binding which does not require
  714. * SSI driver to trigger machine driver's probe.
  715. */
  716. if (!of_get_property(np, "codec-handle", NULL)) {
  717. ssi_private->new_binding = true;
  718. goto done;
  719. }
  720. /* Trigger the machine driver's probe function. The platform driver
  721. * name of the machine driver is taken from /compatible property of the
  722. * device tree. We also pass the address of the CPU DAI driver
  723. * structure.
  724. */
  725. sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
  726. /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
  727. p = strrchr(sprop, ',');
  728. if (p)
  729. sprop = p + 1;
  730. snprintf(name, sizeof(name), "snd-soc-%s", sprop);
  731. make_lowercase(name);
  732. ssi_private->pdev =
  733. platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
  734. if (IS_ERR(ssi_private->pdev)) {
  735. ret = PTR_ERR(ssi_private->pdev);
  736. dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
  737. goto error_dai;
  738. }
  739. done:
  740. return 0;
  741. error_dai:
  742. if (ssi_private->ssi_on_imx)
  743. imx_pcm_dma_exit(pdev);
  744. snd_soc_unregister_component(&pdev->dev);
  745. error_dev:
  746. dev_set_drvdata(&pdev->dev, NULL);
  747. device_remove_file(&pdev->dev, dev_attr);
  748. error_clk:
  749. if (ssi_private->ssi_on_imx) {
  750. clk_disable_unprepare(ssi_private->clk);
  751. clk_put(ssi_private->clk);
  752. }
  753. error_irq:
  754. free_irq(ssi_private->irq, ssi_private);
  755. error_irqmap:
  756. irq_dispose_mapping(ssi_private->irq);
  757. error_iomap:
  758. iounmap(ssi_private->ssi);
  759. error_kmalloc:
  760. kfree(ssi_private);
  761. return ret;
  762. }
  763. static int fsl_ssi_remove(struct platform_device *pdev)
  764. {
  765. struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
  766. if (!ssi_private->new_binding)
  767. platform_device_unregister(ssi_private->pdev);
  768. if (ssi_private->ssi_on_imx) {
  769. imx_pcm_dma_exit(pdev);
  770. clk_disable_unprepare(ssi_private->clk);
  771. clk_put(ssi_private->clk);
  772. }
  773. snd_soc_unregister_component(&pdev->dev);
  774. device_remove_file(&pdev->dev, &ssi_private->dev_attr);
  775. free_irq(ssi_private->irq, ssi_private);
  776. irq_dispose_mapping(ssi_private->irq);
  777. kfree(ssi_private);
  778. dev_set_drvdata(&pdev->dev, NULL);
  779. return 0;
  780. }
  781. static const struct of_device_id fsl_ssi_ids[] = {
  782. { .compatible = "fsl,mpc8610-ssi", },
  783. { .compatible = "fsl,imx21-ssi", },
  784. {}
  785. };
  786. MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
  787. static struct platform_driver fsl_ssi_driver = {
  788. .driver = {
  789. .name = "fsl-ssi-dai",
  790. .owner = THIS_MODULE,
  791. .of_match_table = fsl_ssi_ids,
  792. },
  793. .probe = fsl_ssi_probe,
  794. .remove = fsl_ssi_remove,
  795. };
  796. module_platform_driver(fsl_ssi_driver);
  797. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  798. MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
  799. MODULE_LICENSE("GPL v2");