fsl_ssi.c 22 KB

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