fsl_ssi.c 25 KB

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