fsl_ssi.c 25 KB

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