fsl_ssi.c 28 KB

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