fsl_ssi.c 22 KB

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