fsl_ssi.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. /*
  2. * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007-2010 Freescale Semiconductor, Inc.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. *
  12. *
  13. * Some notes why imx-pcm-fiq is used instead of DMA on some boards:
  14. *
  15. * The i.MX SSI core has some nasty limitations in AC97 mode. While most
  16. * sane processor vendors have a FIFO per AC97 slot, the i.MX has only
  17. * one FIFO which combines all valid receive slots. We cannot even select
  18. * which slots we want to receive. The WM9712 with which this driver
  19. * was developed with always sends GPIO status data in slot 12 which
  20. * we receive in our (PCM-) data stream. The only chance we have is to
  21. * manually skip this data in the FIQ handler. With sampling rates different
  22. * from 48000Hz not every frame has valid receive data, so the ratio
  23. * between pcm data and GPIO status data changes. Our FIQ handler is not
  24. * able to handle this, hence this driver only works with 48000Hz sampling
  25. * rate.
  26. * Reading and writing AC97 registers is another challenge. The core
  27. * provides us status bits when the read register is updated with *another*
  28. * value. When we read the same register two times (and the register still
  29. * contains the same value) these status bits are not set. We work
  30. * around this by not polling these bits but only wait a fixed delay.
  31. */
  32. #include <linux/init.h>
  33. #include <linux/io.h>
  34. #include <linux/module.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/clk.h>
  37. #include <linux/device.h>
  38. #include <linux/delay.h>
  39. #include <linux/slab.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/of.h>
  42. #include <linux/of_address.h>
  43. #include <linux/of_irq.h>
  44. #include <linux/of_platform.h>
  45. #include <sound/core.h>
  46. #include <sound/pcm.h>
  47. #include <sound/pcm_params.h>
  48. #include <sound/initval.h>
  49. #include <sound/soc.h>
  50. #include <sound/dmaengine_pcm.h>
  51. #include "fsl_ssi.h"
  52. #include "imx-pcm.h"
  53. /**
  54. * FSLSSI_I2S_RATES: sample rates supported by the I2S
  55. *
  56. * This driver currently only supports the SSI running in I2S slave mode,
  57. * which means the codec determines the sample rate. Therefore, we tell
  58. * ALSA that we support all rates and let the codec driver decide what rates
  59. * are really supported.
  60. */
  61. #define FSLSSI_I2S_RATES SNDRV_PCM_RATE_CONTINUOUS
  62. /**
  63. * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
  64. *
  65. * This driver currently only supports the SSI running in I2S slave mode.
  66. *
  67. * The SSI has a limitation in that the samples must be in the same byte
  68. * order as the host CPU. This is because when multiple bytes are written
  69. * to the STX register, the bytes and bits must be written in the same
  70. * order. The STX is a shift register, so all the bits need to be aligned
  71. * (bit-endianness must match byte-endianness). Processors typically write
  72. * the bits within a byte in the same order that the bytes of a word are
  73. * written in. So if the host CPU is big-endian, then only big-endian
  74. * samples will be written to STX properly.
  75. */
  76. #ifdef __BIG_ENDIAN
  77. #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
  78. SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
  79. SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
  80. #else
  81. #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
  82. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  83. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
  84. #endif
  85. #define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
  86. CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
  87. CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
  88. #define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
  89. CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
  90. CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
  91. enum fsl_ssi_type {
  92. FSL_SSI_MCP8610,
  93. FSL_SSI_MX21,
  94. FSL_SSI_MX35,
  95. FSL_SSI_MX51,
  96. };
  97. struct fsl_ssi_reg_val {
  98. u32 sier;
  99. u32 srcr;
  100. u32 stcr;
  101. u32 scr;
  102. };
  103. struct fsl_ssi_rxtx_reg_val {
  104. struct fsl_ssi_reg_val rx;
  105. struct fsl_ssi_reg_val tx;
  106. };
  107. static const struct regmap_config fsl_ssi_regconfig = {
  108. .max_register = CCSR_SSI_SACCDIS,
  109. .reg_bits = 32,
  110. .val_bits = 32,
  111. .reg_stride = 4,
  112. .val_format_endian = REGMAP_ENDIAN_NATIVE,
  113. };
  114. struct fsl_ssi_soc_data {
  115. bool imx;
  116. bool offline_config;
  117. u32 sisr_write_mask;
  118. };
  119. /**
  120. * fsl_ssi_private: per-SSI private data
  121. *
  122. * @reg: Pointer to the regmap registers
  123. * @irq: IRQ of this SSI
  124. * @cpu_dai_drv: CPU DAI driver for this device
  125. *
  126. * @dai_fmt: DAI configuration this device is currently used with
  127. * @i2s_mode: i2s and network mode configuration of the device. Is used to
  128. * switch between normal and i2s/network mode
  129. * mode depending on the number of channels
  130. * @use_dma: DMA is used or FIQ with stream filter
  131. * @use_dual_fifo: DMA with support for both FIFOs used
  132. * @fifo_deph: Depth of the SSI FIFOs
  133. * @rxtx_reg_val: Specific register settings for receive/transmit configuration
  134. *
  135. * @clk: SSI clock
  136. * @baudclk: SSI baud clock for master mode
  137. * @baudclk_streams: Active streams that are using baudclk
  138. * @bitclk_freq: bitclock frequency set by .set_dai_sysclk
  139. *
  140. * @dma_params_tx: DMA transmit parameters
  141. * @dma_params_rx: DMA receive parameters
  142. * @ssi_phys: physical address of the SSI registers
  143. *
  144. * @fiq_params: FIQ stream filtering parameters
  145. *
  146. * @pdev: Pointer to pdev used for deprecated fsl-ssi sound card
  147. *
  148. * @dbg_stats: Debugging statistics
  149. *
  150. * @soc: SoC specifc data
  151. */
  152. struct fsl_ssi_private {
  153. struct regmap *regs;
  154. unsigned int irq;
  155. struct snd_soc_dai_driver cpu_dai_drv;
  156. unsigned int dai_fmt;
  157. u8 i2s_mode;
  158. bool use_dma;
  159. bool use_dual_fifo;
  160. bool has_ipg_clk_name;
  161. unsigned int fifo_depth;
  162. struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
  163. struct clk *clk;
  164. struct clk *baudclk;
  165. unsigned int baudclk_streams;
  166. unsigned int bitclk_freq;
  167. /* DMA params */
  168. struct snd_dmaengine_dai_dma_data dma_params_tx;
  169. struct snd_dmaengine_dai_dma_data dma_params_rx;
  170. dma_addr_t ssi_phys;
  171. /* params for non-dma FIQ stream filtered mode */
  172. struct imx_pcm_fiq_params fiq_params;
  173. /* Used when using fsl-ssi as sound-card. This is only used by ppc and
  174. * should be replaced with simple-sound-card. */
  175. struct platform_device *pdev;
  176. struct fsl_ssi_dbg dbg_stats;
  177. const struct fsl_ssi_soc_data *soc;
  178. };
  179. /*
  180. * imx51 and later SoCs have a slightly different IP that allows the
  181. * SSI configuration while the SSI unit is running.
  182. *
  183. * More important, it is necessary on those SoCs to configure the
  184. * sperate TX/RX DMA bits just before starting the stream
  185. * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi
  186. * sends any DMA requests to the SDMA unit, otherwise it is not defined
  187. * how the SDMA unit handles the DMA request.
  188. *
  189. * SDMA units are present on devices starting at imx35 but the imx35
  190. * reference manual states that the DMA bits should not be changed
  191. * while the SSI unit is running (SSIEN). So we support the necessary
  192. * online configuration of fsl-ssi starting at imx51.
  193. */
  194. static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
  195. .imx = false,
  196. .offline_config = true,
  197. .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
  198. CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
  199. CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
  200. };
  201. static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
  202. .imx = true,
  203. .offline_config = true,
  204. .sisr_write_mask = 0,
  205. };
  206. static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
  207. .imx = true,
  208. .offline_config = true,
  209. .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
  210. CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
  211. CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
  212. };
  213. static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
  214. .imx = true,
  215. .offline_config = false,
  216. .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
  217. CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
  218. };
  219. static const struct of_device_id fsl_ssi_ids[] = {
  220. { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
  221. { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
  222. { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
  223. { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
  224. {}
  225. };
  226. MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
  227. static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private)
  228. {
  229. return !!(ssi_private->dai_fmt & SND_SOC_DAIFMT_AC97);
  230. }
  231. static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private)
  232. {
  233. return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
  234. SND_SOC_DAIFMT_CBS_CFS;
  235. }
  236. static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi_private *ssi_private)
  237. {
  238. return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
  239. SND_SOC_DAIFMT_CBM_CFS;
  240. }
  241. /**
  242. * fsl_ssi_isr: SSI interrupt handler
  243. *
  244. * Although it's possible to use the interrupt handler to send and receive
  245. * data to/from the SSI, we use the DMA instead. Programming is more
  246. * complicated, but the performance is much better.
  247. *
  248. * This interrupt handler is used only to gather statistics.
  249. *
  250. * @irq: IRQ of the SSI device
  251. * @dev_id: pointer to the ssi_private structure for this SSI device
  252. */
  253. static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
  254. {
  255. struct fsl_ssi_private *ssi_private = dev_id;
  256. struct regmap *regs = ssi_private->regs;
  257. __be32 sisr;
  258. __be32 sisr2;
  259. /* We got an interrupt, so read the status register to see what we
  260. were interrupted for. We mask it with the Interrupt Enable register
  261. so that we only check for events that we're interested in.
  262. */
  263. regmap_read(regs, CCSR_SSI_SISR, &sisr);
  264. sisr2 = sisr & ssi_private->soc->sisr_write_mask;
  265. /* Clear the bits that we set */
  266. if (sisr2)
  267. regmap_write(regs, CCSR_SSI_SISR, sisr2);
  268. fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
  269. return IRQ_HANDLED;
  270. }
  271. /*
  272. * Enable/Disable all rx/tx config flags at once.
  273. */
  274. static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
  275. bool enable)
  276. {
  277. struct regmap *regs = ssi_private->regs;
  278. struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
  279. if (enable) {
  280. regmap_update_bits(regs, CCSR_SSI_SIER,
  281. vals->rx.sier | vals->tx.sier,
  282. vals->rx.sier | vals->tx.sier);
  283. regmap_update_bits(regs, CCSR_SSI_SRCR,
  284. vals->rx.srcr | vals->tx.srcr,
  285. vals->rx.srcr | vals->tx.srcr);
  286. regmap_update_bits(regs, CCSR_SSI_STCR,
  287. vals->rx.stcr | vals->tx.stcr,
  288. vals->rx.stcr | vals->tx.stcr);
  289. } else {
  290. regmap_update_bits(regs, CCSR_SSI_SRCR,
  291. vals->rx.srcr | vals->tx.srcr, 0);
  292. regmap_update_bits(regs, CCSR_SSI_STCR,
  293. vals->rx.stcr | vals->tx.stcr, 0);
  294. regmap_update_bits(regs, CCSR_SSI_SIER,
  295. vals->rx.sier | vals->tx.sier, 0);
  296. }
  297. }
  298. /*
  299. * Calculate the bits that have to be disabled for the current stream that is
  300. * getting disabled. This keeps the bits enabled that are necessary for the
  301. * second stream to work if 'stream_active' is true.
  302. *
  303. * Detailed calculation:
  304. * These are the values that need to be active after disabling. For non-active
  305. * second stream, this is 0:
  306. * vals_stream * !!stream_active
  307. *
  308. * The following computes the overall differences between the setup for the
  309. * to-disable stream and the active stream, a simple XOR:
  310. * vals_disable ^ (vals_stream * !!(stream_active))
  311. *
  312. * The full expression adds a mask on all values we care about
  313. */
  314. #define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
  315. ((vals_disable) & \
  316. ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
  317. /*
  318. * Enable/Disable a ssi configuration. You have to pass either
  319. * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
  320. */
  321. static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
  322. struct fsl_ssi_reg_val *vals)
  323. {
  324. struct regmap *regs = ssi_private->regs;
  325. struct fsl_ssi_reg_val *avals;
  326. int nr_active_streams;
  327. u32 scr_val;
  328. int keep_active;
  329. regmap_read(regs, CCSR_SSI_SCR, &scr_val);
  330. nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
  331. !!(scr_val & CCSR_SSI_SCR_RE);
  332. if (nr_active_streams - 1 > 0)
  333. keep_active = 1;
  334. else
  335. keep_active = 0;
  336. /* Find the other direction values rx or tx which we do not want to
  337. * modify */
  338. if (&ssi_private->rxtx_reg_val.rx == vals)
  339. avals = &ssi_private->rxtx_reg_val.tx;
  340. else
  341. avals = &ssi_private->rxtx_reg_val.rx;
  342. /* If vals should be disabled, start with disabling the unit */
  343. if (!enable) {
  344. u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
  345. keep_active);
  346. regmap_update_bits(regs, CCSR_SSI_SCR, scr, 0);
  347. }
  348. /*
  349. * We are running on a SoC which does not support online SSI
  350. * reconfiguration, so we have to enable all necessary flags at once
  351. * even if we do not use them later (capture and playback configuration)
  352. */
  353. if (ssi_private->soc->offline_config) {
  354. if ((enable && !nr_active_streams) ||
  355. (!enable && !keep_active))
  356. fsl_ssi_rxtx_config(ssi_private, enable);
  357. goto config_done;
  358. }
  359. /*
  360. * Configure single direction units while the SSI unit is running
  361. * (online configuration)
  362. */
  363. if (enable) {
  364. regmap_update_bits(regs, CCSR_SSI_SIER, vals->sier, vals->sier);
  365. regmap_update_bits(regs, CCSR_SSI_SRCR, vals->srcr, vals->srcr);
  366. regmap_update_bits(regs, CCSR_SSI_STCR, vals->stcr, vals->stcr);
  367. } else {
  368. u32 sier;
  369. u32 srcr;
  370. u32 stcr;
  371. /*
  372. * Disabling the necessary flags for one of rx/tx while the
  373. * other stream is active is a little bit more difficult. We
  374. * have to disable only those flags that differ between both
  375. * streams (rx XOR tx) and that are set in the stream that is
  376. * disabled now. Otherwise we could alter flags of the other
  377. * stream
  378. */
  379. /* These assignments are simply vals without bits set in avals*/
  380. sier = fsl_ssi_disable_val(vals->sier, avals->sier,
  381. keep_active);
  382. srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
  383. keep_active);
  384. stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
  385. keep_active);
  386. regmap_update_bits(regs, CCSR_SSI_SRCR, srcr, 0);
  387. regmap_update_bits(regs, CCSR_SSI_STCR, stcr, 0);
  388. regmap_update_bits(regs, CCSR_SSI_SIER, sier, 0);
  389. }
  390. config_done:
  391. /* Enabling of subunits is done after configuration */
  392. if (enable)
  393. regmap_update_bits(regs, CCSR_SSI_SCR, vals->scr, vals->scr);
  394. }
  395. static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
  396. {
  397. fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
  398. }
  399. static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
  400. {
  401. fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
  402. }
  403. /*
  404. * Setup rx/tx register values used to enable/disable the streams. These will
  405. * be used later in fsl_ssi_config to setup the streams without the need to
  406. * check for all different SSI modes.
  407. */
  408. static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
  409. {
  410. struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
  411. reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
  412. reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
  413. reg->rx.scr = 0;
  414. reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
  415. reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
  416. reg->tx.scr = 0;
  417. if (!fsl_ssi_is_ac97(ssi_private)) {
  418. reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
  419. reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN;
  420. reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
  421. reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN;
  422. }
  423. if (ssi_private->use_dma) {
  424. reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
  425. reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
  426. } else {
  427. reg->rx.sier |= CCSR_SSI_SIER_RIE;
  428. reg->tx.sier |= CCSR_SSI_SIER_TIE;
  429. }
  430. reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
  431. reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
  432. }
  433. static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
  434. {
  435. struct regmap *regs = ssi_private->regs;
  436. /*
  437. * Setup the clock control register
  438. */
  439. regmap_write(regs, CCSR_SSI_STCCR,
  440. CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
  441. regmap_write(regs, CCSR_SSI_SRCCR,
  442. CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
  443. /*
  444. * Enable AC97 mode and startup the SSI
  445. */
  446. regmap_write(regs, CCSR_SSI_SACNT,
  447. CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV);
  448. regmap_write(regs, CCSR_SSI_SACCDIS, 0xff);
  449. regmap_write(regs, CCSR_SSI_SACCEN, 0x300);
  450. /*
  451. * Enable SSI, Transmit and Receive. AC97 has to communicate with the
  452. * codec before a stream is started.
  453. */
  454. regmap_update_bits(regs, CCSR_SSI_SCR,
  455. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE,
  456. CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
  457. regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_WAIT(3));
  458. }
  459. /**
  460. * fsl_ssi_startup: create a new substream
  461. *
  462. * This is the first function called when a stream is opened.
  463. *
  464. * If this is the first stream open, then grab the IRQ and program most of
  465. * the SSI registers.
  466. */
  467. static int fsl_ssi_startup(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 =
  472. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  473. int ret;
  474. ret = clk_prepare_enable(ssi_private->clk);
  475. if (ret)
  476. return ret;
  477. /* When using dual fifo mode, it is safer to ensure an even period
  478. * size. If appearing to an odd number while DMA always starts its
  479. * task from fifo0, fifo1 would be neglected at the end of each
  480. * period. But SSI would still access fifo1 with an invalid data.
  481. */
  482. if (ssi_private->use_dual_fifo)
  483. snd_pcm_hw_constraint_step(substream->runtime, 0,
  484. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
  485. return 0;
  486. }
  487. /**
  488. * fsl_ssi_shutdown: shutdown the SSI
  489. *
  490. */
  491. static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
  492. struct snd_soc_dai *dai)
  493. {
  494. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  495. struct fsl_ssi_private *ssi_private =
  496. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  497. clk_disable_unprepare(ssi_private->clk);
  498. }
  499. /**
  500. * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock
  501. *
  502. * Note: This function can be only called when using SSI as DAI master
  503. *
  504. * Quick instruction for parameters:
  505. * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
  506. * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
  507. */
  508. static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
  509. struct snd_soc_dai *cpu_dai,
  510. struct snd_pcm_hw_params *hw_params)
  511. {
  512. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
  513. struct regmap *regs = ssi_private->regs;
  514. int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
  515. u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
  516. unsigned long clkrate, baudrate, tmprate;
  517. u64 sub, savesub = 100000;
  518. unsigned int freq;
  519. bool baudclk_is_used;
  520. /* Prefer the explicitly set bitclock frequency */
  521. if (ssi_private->bitclk_freq)
  522. freq = ssi_private->bitclk_freq;
  523. else
  524. freq = params_channels(hw_params) * 32 * params_rate(hw_params);
  525. /* Don't apply it to any non-baudclk circumstance */
  526. if (IS_ERR(ssi_private->baudclk))
  527. return -EINVAL;
  528. baudclk_is_used = ssi_private->baudclk_streams & ~(BIT(substream->stream));
  529. /* It should be already enough to divide clock by setting pm alone */
  530. psr = 0;
  531. div2 = 0;
  532. factor = (div2 + 1) * (7 * psr + 1) * 2;
  533. for (i = 0; i < 255; i++) {
  534. /* The bclk rate must be smaller than 1/5 sysclk rate */
  535. if (factor * (i + 1) < 5)
  536. continue;
  537. tmprate = freq * factor * (i + 2);
  538. if (baudclk_is_used)
  539. clkrate = clk_get_rate(ssi_private->baudclk);
  540. else
  541. clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
  542. clkrate /= factor;
  543. afreq = clkrate / (i + 1);
  544. if (freq == afreq)
  545. sub = 0;
  546. else if (freq / afreq == 1)
  547. sub = freq - afreq;
  548. else if (afreq / freq == 1)
  549. sub = afreq - freq;
  550. else
  551. continue;
  552. /* Calculate the fraction */
  553. sub *= 100000;
  554. do_div(sub, freq);
  555. if (sub < savesub) {
  556. baudrate = tmprate;
  557. savesub = sub;
  558. pm = i;
  559. }
  560. /* We are lucky */
  561. if (savesub == 0)
  562. break;
  563. }
  564. /* No proper pm found if it is still remaining the initial value */
  565. if (pm == 999) {
  566. dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
  567. return -EINVAL;
  568. }
  569. stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
  570. (psr ? CCSR_SSI_SxCCR_PSR : 0);
  571. mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
  572. CCSR_SSI_SxCCR_PSR;
  573. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous)
  574. regmap_update_bits(regs, CCSR_SSI_STCCR, mask, stccr);
  575. else
  576. regmap_update_bits(regs, CCSR_SSI_SRCCR, mask, stccr);
  577. if (!baudclk_is_used) {
  578. ret = clk_set_rate(ssi_private->baudclk, baudrate);
  579. if (ret) {
  580. dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
  581. return -EINVAL;
  582. }
  583. }
  584. return 0;
  585. }
  586. static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  587. int clk_id, unsigned int freq, int dir)
  588. {
  589. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
  590. ssi_private->bitclk_freq = freq;
  591. return 0;
  592. }
  593. /**
  594. * fsl_ssi_hw_params - program the sample size
  595. *
  596. * Most of the SSI registers have been programmed in the startup function,
  597. * but the word length must be programmed here. Unfortunately, programming
  598. * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
  599. * cause a problem with supporting simultaneous playback and capture. If
  600. * the SSI is already playing a stream, then that stream may be temporarily
  601. * stopped when you start capture.
  602. *
  603. * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
  604. * clock master.
  605. */
  606. static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
  607. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
  608. {
  609. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
  610. struct regmap *regs = ssi_private->regs;
  611. unsigned int channels = params_channels(hw_params);
  612. unsigned int sample_size =
  613. snd_pcm_format_width(params_format(hw_params));
  614. u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
  615. int ret;
  616. u32 scr_val;
  617. int enabled;
  618. regmap_read(regs, CCSR_SSI_SCR, &scr_val);
  619. enabled = scr_val & CCSR_SSI_SCR_SSIEN;
  620. /*
  621. * If we're in synchronous mode, and the SSI is already enabled,
  622. * then STCCR is already set properly.
  623. */
  624. if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
  625. return 0;
  626. if (fsl_ssi_is_i2s_master(ssi_private)) {
  627. ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params);
  628. if (ret)
  629. return ret;
  630. /* Do not enable the clock if it is already enabled */
  631. if (!(ssi_private->baudclk_streams & BIT(substream->stream))) {
  632. ret = clk_prepare_enable(ssi_private->baudclk);
  633. if (ret)
  634. return ret;
  635. ssi_private->baudclk_streams |= BIT(substream->stream);
  636. }
  637. }
  638. if (!fsl_ssi_is_ac97(ssi_private)) {
  639. u8 i2smode;
  640. /*
  641. * Switch to normal net mode in order to have a frame sync
  642. * signal every 32 bits instead of 16 bits
  643. */
  644. if (fsl_ssi_is_i2s_cbm_cfs(ssi_private) && sample_size == 16)
  645. i2smode = CCSR_SSI_SCR_I2S_MODE_NORMAL |
  646. CCSR_SSI_SCR_NET;
  647. else
  648. i2smode = ssi_private->i2s_mode;
  649. regmap_update_bits(regs, CCSR_SSI_SCR,
  650. CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
  651. channels == 1 ? 0 : i2smode);
  652. }
  653. /*
  654. * FIXME: The documentation says that SxCCR[WL] should not be
  655. * modified while the SSI is enabled. The only time this can
  656. * happen is if we're trying to do simultaneous playback and
  657. * capture in asynchronous mode. Unfortunately, I have been enable
  658. * to get that to work at all on the P1022DS. Therefore, we don't
  659. * bother to disable/enable the SSI when setting SxCCR[WL], because
  660. * the SSI will stop anyway. Maybe one day, this will get fixed.
  661. */
  662. /* In synchronous mode, the SSI uses STCCR for capture */
  663. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
  664. ssi_private->cpu_dai_drv.symmetric_rates)
  665. regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_WL_MASK,
  666. wl);
  667. else
  668. regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_WL_MASK,
  669. wl);
  670. return 0;
  671. }
  672. static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
  673. struct snd_soc_dai *cpu_dai)
  674. {
  675. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  676. struct fsl_ssi_private *ssi_private =
  677. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  678. if (fsl_ssi_is_i2s_master(ssi_private) &&
  679. ssi_private->baudclk_streams & BIT(substream->stream)) {
  680. clk_disable_unprepare(ssi_private->baudclk);
  681. ssi_private->baudclk_streams &= ~BIT(substream->stream);
  682. }
  683. return 0;
  684. }
  685. static int _fsl_ssi_set_dai_fmt(struct device *dev,
  686. struct fsl_ssi_private *ssi_private,
  687. unsigned int fmt)
  688. {
  689. struct regmap *regs = ssi_private->regs;
  690. u32 strcr = 0, stcr, srcr, scr, mask;
  691. u8 wm;
  692. ssi_private->dai_fmt = fmt;
  693. if (fsl_ssi_is_i2s_master(ssi_private) && IS_ERR(ssi_private->baudclk)) {
  694. dev_err(dev, "baudclk is missing which is necessary for master mode\n");
  695. return -EINVAL;
  696. }
  697. fsl_ssi_setup_reg_vals(ssi_private);
  698. regmap_read(regs, CCSR_SSI_SCR, &scr);
  699. scr &= ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
  700. scr |= CCSR_SSI_SCR_SYNC_TX_FS;
  701. mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
  702. CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
  703. CCSR_SSI_STCR_TEFS;
  704. regmap_read(regs, CCSR_SSI_STCR, &stcr);
  705. regmap_read(regs, CCSR_SSI_SRCR, &srcr);
  706. stcr &= ~mask;
  707. srcr &= ~mask;
  708. ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
  709. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  710. case SND_SOC_DAIFMT_I2S:
  711. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  712. case SND_SOC_DAIFMT_CBM_CFS:
  713. case SND_SOC_DAIFMT_CBS_CFS:
  714. ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
  715. regmap_update_bits(regs, CCSR_SSI_STCCR,
  716. CCSR_SSI_SxCCR_DC_MASK,
  717. CCSR_SSI_SxCCR_DC(2));
  718. regmap_update_bits(regs, CCSR_SSI_SRCCR,
  719. CCSR_SSI_SxCCR_DC_MASK,
  720. CCSR_SSI_SxCCR_DC(2));
  721. break;
  722. case SND_SOC_DAIFMT_CBM_CFM:
  723. ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
  724. break;
  725. default:
  726. return -EINVAL;
  727. }
  728. /* Data on rising edge of bclk, frame low, 1clk before data */
  729. strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
  730. CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
  731. break;
  732. case SND_SOC_DAIFMT_LEFT_J:
  733. /* Data on rising edge of bclk, frame high */
  734. strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
  735. break;
  736. case SND_SOC_DAIFMT_DSP_A:
  737. /* Data on rising edge of bclk, frame high, 1clk before data */
  738. strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
  739. CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
  740. break;
  741. case SND_SOC_DAIFMT_DSP_B:
  742. /* Data on rising edge of bclk, frame high */
  743. strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
  744. CCSR_SSI_STCR_TXBIT0;
  745. break;
  746. case SND_SOC_DAIFMT_AC97:
  747. ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
  748. break;
  749. default:
  750. return -EINVAL;
  751. }
  752. scr |= ssi_private->i2s_mode;
  753. /* DAI clock inversion */
  754. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  755. case SND_SOC_DAIFMT_NB_NF:
  756. /* Nothing to do for both normal cases */
  757. break;
  758. case SND_SOC_DAIFMT_IB_NF:
  759. /* Invert bit clock */
  760. strcr ^= CCSR_SSI_STCR_TSCKP;
  761. break;
  762. case SND_SOC_DAIFMT_NB_IF:
  763. /* Invert frame clock */
  764. strcr ^= CCSR_SSI_STCR_TFSI;
  765. break;
  766. case SND_SOC_DAIFMT_IB_IF:
  767. /* Invert both clocks */
  768. strcr ^= CCSR_SSI_STCR_TSCKP;
  769. strcr ^= CCSR_SSI_STCR_TFSI;
  770. break;
  771. default:
  772. return -EINVAL;
  773. }
  774. /* DAI clock master masks */
  775. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  776. case SND_SOC_DAIFMT_CBS_CFS:
  777. strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
  778. scr |= CCSR_SSI_SCR_SYS_CLK_EN;
  779. break;
  780. case SND_SOC_DAIFMT_CBM_CFM:
  781. scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
  782. break;
  783. case SND_SOC_DAIFMT_CBM_CFS:
  784. strcr &= ~CCSR_SSI_STCR_TXDIR;
  785. strcr |= CCSR_SSI_STCR_TFDIR;
  786. scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
  787. break;
  788. default:
  789. return -EINVAL;
  790. }
  791. stcr |= strcr;
  792. srcr |= strcr;
  793. if (ssi_private->cpu_dai_drv.symmetric_rates) {
  794. /* Need to clear RXDIR when using SYNC mode */
  795. srcr &= ~CCSR_SSI_SRCR_RXDIR;
  796. scr |= CCSR_SSI_SCR_SYN;
  797. }
  798. regmap_write(regs, CCSR_SSI_STCR, stcr);
  799. regmap_write(regs, CCSR_SSI_SRCR, srcr);
  800. regmap_write(regs, CCSR_SSI_SCR, scr);
  801. /*
  802. * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
  803. * use FIFO 1. We program the transmit water to signal a DMA transfer
  804. * if there are only two (or fewer) elements left in the FIFO. Two
  805. * elements equals one frame (left channel, right channel). This value,
  806. * however, depends on the depth of the transmit buffer.
  807. *
  808. * We set the watermark on the same level as the DMA burstsize. For
  809. * fiq it is probably better to use the biggest possible watermark
  810. * size.
  811. */
  812. if (ssi_private->use_dma)
  813. wm = ssi_private->fifo_depth - 2;
  814. else
  815. wm = ssi_private->fifo_depth;
  816. regmap_write(regs, CCSR_SSI_SFCSR,
  817. CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
  818. CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm));
  819. if (ssi_private->use_dual_fifo) {
  820. regmap_update_bits(regs, CCSR_SSI_SRCR, CCSR_SSI_SRCR_RFEN1,
  821. CCSR_SSI_SRCR_RFEN1);
  822. regmap_update_bits(regs, CCSR_SSI_STCR, CCSR_SSI_STCR_TFEN1,
  823. CCSR_SSI_STCR_TFEN1);
  824. regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_TCH_EN,
  825. CCSR_SSI_SCR_TCH_EN);
  826. }
  827. if (fmt & SND_SOC_DAIFMT_AC97)
  828. fsl_ssi_setup_ac97(ssi_private);
  829. return 0;
  830. }
  831. /**
  832. * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
  833. */
  834. static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  835. {
  836. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
  837. return _fsl_ssi_set_dai_fmt(cpu_dai->dev, ssi_private, fmt);
  838. }
  839. /**
  840. * fsl_ssi_set_dai_tdm_slot - set TDM slot number
  841. *
  842. * Note: This function can be only called when using SSI as DAI master
  843. */
  844. static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
  845. u32 rx_mask, int slots, int slot_width)
  846. {
  847. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
  848. struct regmap *regs = ssi_private->regs;
  849. u32 val;
  850. /* The slot number should be >= 2 if using Network mode or I2S mode */
  851. regmap_read(regs, CCSR_SSI_SCR, &val);
  852. val &= CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET;
  853. if (val && slots < 2) {
  854. dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
  855. return -EINVAL;
  856. }
  857. regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_DC_MASK,
  858. CCSR_SSI_SxCCR_DC(slots));
  859. regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_DC_MASK,
  860. CCSR_SSI_SxCCR_DC(slots));
  861. /* The register SxMSKs needs SSI to provide essential clock due to
  862. * hardware design. So we here temporarily enable SSI to set them.
  863. */
  864. regmap_read(regs, CCSR_SSI_SCR, &val);
  865. val &= CCSR_SSI_SCR_SSIEN;
  866. regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN,
  867. CCSR_SSI_SCR_SSIEN);
  868. regmap_write(regs, CCSR_SSI_STMSK, tx_mask);
  869. regmap_write(regs, CCSR_SSI_SRMSK, rx_mask);
  870. regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, val);
  871. return 0;
  872. }
  873. /**
  874. * fsl_ssi_trigger: start and stop the DMA transfer.
  875. *
  876. * This function is called by ALSA to start, stop, pause, and resume the DMA
  877. * transfer of data.
  878. *
  879. * The DMA channel is in external master start and pause mode, which
  880. * means the SSI completely controls the flow of data.
  881. */
  882. static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
  883. struct snd_soc_dai *dai)
  884. {
  885. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  886. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  887. struct regmap *regs = ssi_private->regs;
  888. switch (cmd) {
  889. case SNDRV_PCM_TRIGGER_START:
  890. case SNDRV_PCM_TRIGGER_RESUME:
  891. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  892. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  893. fsl_ssi_tx_config(ssi_private, true);
  894. else
  895. fsl_ssi_rx_config(ssi_private, true);
  896. break;
  897. case SNDRV_PCM_TRIGGER_STOP:
  898. case SNDRV_PCM_TRIGGER_SUSPEND:
  899. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  900. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  901. fsl_ssi_tx_config(ssi_private, false);
  902. else
  903. fsl_ssi_rx_config(ssi_private, false);
  904. break;
  905. default:
  906. return -EINVAL;
  907. }
  908. if (fsl_ssi_is_ac97(ssi_private)) {
  909. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  910. regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_TX_CLR);
  911. else
  912. regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_RX_CLR);
  913. }
  914. return 0;
  915. }
  916. static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
  917. {
  918. struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
  919. if (ssi_private->soc->imx && ssi_private->use_dma) {
  920. dai->playback_dma_data = &ssi_private->dma_params_tx;
  921. dai->capture_dma_data = &ssi_private->dma_params_rx;
  922. }
  923. return 0;
  924. }
  925. static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
  926. .startup = fsl_ssi_startup,
  927. .shutdown = fsl_ssi_shutdown,
  928. .hw_params = fsl_ssi_hw_params,
  929. .hw_free = fsl_ssi_hw_free,
  930. .set_fmt = fsl_ssi_set_dai_fmt,
  931. .set_sysclk = fsl_ssi_set_dai_sysclk,
  932. .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
  933. .trigger = fsl_ssi_trigger,
  934. };
  935. /* Template for the CPU dai driver structure */
  936. static struct snd_soc_dai_driver fsl_ssi_dai_template = {
  937. .probe = fsl_ssi_dai_probe,
  938. .playback = {
  939. .stream_name = "CPU-Playback",
  940. .channels_min = 1,
  941. .channels_max = 2,
  942. .rates = FSLSSI_I2S_RATES,
  943. .formats = FSLSSI_I2S_FORMATS,
  944. },
  945. .capture = {
  946. .stream_name = "CPU-Capture",
  947. .channels_min = 1,
  948. .channels_max = 2,
  949. .rates = FSLSSI_I2S_RATES,
  950. .formats = FSLSSI_I2S_FORMATS,
  951. },
  952. .ops = &fsl_ssi_dai_ops,
  953. };
  954. static const struct snd_soc_component_driver fsl_ssi_component = {
  955. .name = "fsl-ssi",
  956. };
  957. static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
  958. .ac97_control = 1,
  959. .playback = {
  960. .stream_name = "AC97 Playback",
  961. .channels_min = 2,
  962. .channels_max = 2,
  963. .rates = SNDRV_PCM_RATE_8000_48000,
  964. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  965. },
  966. .capture = {
  967. .stream_name = "AC97 Capture",
  968. .channels_min = 2,
  969. .channels_max = 2,
  970. .rates = SNDRV_PCM_RATE_48000,
  971. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  972. },
  973. .ops = &fsl_ssi_dai_ops,
  974. };
  975. static struct fsl_ssi_private *fsl_ac97_data;
  976. static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  977. unsigned short val)
  978. {
  979. struct regmap *regs = fsl_ac97_data->regs;
  980. unsigned int lreg;
  981. unsigned int lval;
  982. if (reg > 0x7f)
  983. return;
  984. lreg = reg << 12;
  985. regmap_write(regs, CCSR_SSI_SACADD, lreg);
  986. lval = val << 4;
  987. regmap_write(regs, CCSR_SSI_SACDAT, lval);
  988. regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
  989. CCSR_SSI_SACNT_WR);
  990. udelay(100);
  991. }
  992. static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
  993. unsigned short reg)
  994. {
  995. struct regmap *regs = fsl_ac97_data->regs;
  996. unsigned short val = -1;
  997. u32 reg_val;
  998. unsigned int lreg;
  999. lreg = (reg & 0x7f) << 12;
  1000. regmap_write(regs, CCSR_SSI_SACADD, lreg);
  1001. regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
  1002. CCSR_SSI_SACNT_RD);
  1003. udelay(100);
  1004. regmap_read(regs, CCSR_SSI_SACDAT, &reg_val);
  1005. val = (reg_val >> 4) & 0xffff;
  1006. return val;
  1007. }
  1008. static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
  1009. .read = fsl_ssi_ac97_read,
  1010. .write = fsl_ssi_ac97_write,
  1011. };
  1012. /**
  1013. * Make every character in a string lower-case
  1014. */
  1015. static void make_lowercase(char *s)
  1016. {
  1017. char *p = s;
  1018. char c;
  1019. while ((c = *p)) {
  1020. if ((c >= 'A') && (c <= 'Z'))
  1021. *p = c + ('a' - 'A');
  1022. p++;
  1023. }
  1024. }
  1025. static int fsl_ssi_imx_probe(struct platform_device *pdev,
  1026. struct fsl_ssi_private *ssi_private, void __iomem *iomem)
  1027. {
  1028. struct device_node *np = pdev->dev.of_node;
  1029. u32 dmas[4];
  1030. int ret;
  1031. if (ssi_private->has_ipg_clk_name)
  1032. ssi_private->clk = devm_clk_get(&pdev->dev, "ipg");
  1033. else
  1034. ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
  1035. if (IS_ERR(ssi_private->clk)) {
  1036. ret = PTR_ERR(ssi_private->clk);
  1037. dev_err(&pdev->dev, "could not get clock: %d\n", ret);
  1038. return ret;
  1039. }
  1040. if (!ssi_private->has_ipg_clk_name) {
  1041. ret = clk_prepare_enable(ssi_private->clk);
  1042. if (ret) {
  1043. dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
  1044. return ret;
  1045. }
  1046. }
  1047. /* For those SLAVE implementations, we ingore non-baudclk cases
  1048. * and, instead, abandon MASTER mode that needs baud clock.
  1049. */
  1050. ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
  1051. if (IS_ERR(ssi_private->baudclk))
  1052. dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
  1053. PTR_ERR(ssi_private->baudclk));
  1054. /*
  1055. * We have burstsize be "fifo_depth - 2" to match the SSI
  1056. * watermark setting in fsl_ssi_startup().
  1057. */
  1058. ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
  1059. ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
  1060. ssi_private->dma_params_tx.addr = ssi_private->ssi_phys + CCSR_SSI_STX0;
  1061. ssi_private->dma_params_rx.addr = ssi_private->ssi_phys + CCSR_SSI_SRX0;
  1062. ret = !of_property_read_u32_array(np, "dmas", dmas, 4);
  1063. if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
  1064. ssi_private->use_dual_fifo = true;
  1065. /* When using dual fifo mode, we need to keep watermark
  1066. * as even numbers due to dma script limitation.
  1067. */
  1068. ssi_private->dma_params_tx.maxburst &= ~0x1;
  1069. ssi_private->dma_params_rx.maxburst &= ~0x1;
  1070. }
  1071. if (!ssi_private->use_dma) {
  1072. /*
  1073. * Some boards use an incompatible codec. To get it
  1074. * working, we are using imx-fiq-pcm-audio, that
  1075. * can handle those codecs. DMA is not possible in this
  1076. * situation.
  1077. */
  1078. ssi_private->fiq_params.irq = ssi_private->irq;
  1079. ssi_private->fiq_params.base = iomem;
  1080. ssi_private->fiq_params.dma_params_rx =
  1081. &ssi_private->dma_params_rx;
  1082. ssi_private->fiq_params.dma_params_tx =
  1083. &ssi_private->dma_params_tx;
  1084. ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
  1085. if (ret)
  1086. goto error_pcm;
  1087. } else {
  1088. ret = imx_pcm_dma_init(pdev);
  1089. if (ret)
  1090. goto error_pcm;
  1091. }
  1092. return 0;
  1093. error_pcm:
  1094. if (!ssi_private->has_ipg_clk_name)
  1095. clk_disable_unprepare(ssi_private->clk);
  1096. return ret;
  1097. }
  1098. static void fsl_ssi_imx_clean(struct platform_device *pdev,
  1099. struct fsl_ssi_private *ssi_private)
  1100. {
  1101. if (!ssi_private->use_dma)
  1102. imx_pcm_fiq_exit(pdev);
  1103. if (!ssi_private->has_ipg_clk_name)
  1104. clk_disable_unprepare(ssi_private->clk);
  1105. }
  1106. static int fsl_ssi_probe(struct platform_device *pdev)
  1107. {
  1108. struct fsl_ssi_private *ssi_private;
  1109. int ret = 0;
  1110. struct device_node *np = pdev->dev.of_node;
  1111. const struct of_device_id *of_id;
  1112. const char *p, *sprop;
  1113. const uint32_t *iprop;
  1114. struct resource res;
  1115. void __iomem *iomem;
  1116. char name[64];
  1117. /* SSIs that are not connected on the board should have a
  1118. * status = "disabled"
  1119. * property in their device tree nodes.
  1120. */
  1121. if (!of_device_is_available(np))
  1122. return -ENODEV;
  1123. of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
  1124. if (!of_id || !of_id->data)
  1125. return -EINVAL;
  1126. ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
  1127. GFP_KERNEL);
  1128. if (!ssi_private) {
  1129. dev_err(&pdev->dev, "could not allocate DAI object\n");
  1130. return -ENOMEM;
  1131. }
  1132. ssi_private->soc = of_id->data;
  1133. sprop = of_get_property(np, "fsl,mode", NULL);
  1134. if (sprop) {
  1135. if (!strcmp(sprop, "ac97-slave"))
  1136. ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
  1137. }
  1138. ssi_private->use_dma = !of_property_read_bool(np,
  1139. "fsl,fiq-stream-filter");
  1140. if (fsl_ssi_is_ac97(ssi_private)) {
  1141. memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
  1142. sizeof(fsl_ssi_ac97_dai));
  1143. fsl_ac97_data = ssi_private;
  1144. snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
  1145. } else {
  1146. /* Initialize this copy of the CPU DAI driver structure */
  1147. memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
  1148. sizeof(fsl_ssi_dai_template));
  1149. }
  1150. ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
  1151. /* Get the addresses and IRQ */
  1152. ret = of_address_to_resource(np, 0, &res);
  1153. if (ret) {
  1154. dev_err(&pdev->dev, "could not determine device resources\n");
  1155. return ret;
  1156. }
  1157. ssi_private->ssi_phys = res.start;
  1158. iomem = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
  1159. if (!iomem) {
  1160. dev_err(&pdev->dev, "could not map device resources\n");
  1161. return -ENOMEM;
  1162. }
  1163. ret = of_property_match_string(np, "clock-names", "ipg");
  1164. if (ret < 0) {
  1165. ssi_private->has_ipg_clk_name = false;
  1166. ssi_private->regs = devm_regmap_init_mmio(&pdev->dev, iomem,
  1167. &fsl_ssi_regconfig);
  1168. } else {
  1169. ssi_private->has_ipg_clk_name = true;
  1170. ssi_private->regs = devm_regmap_init_mmio_clk(&pdev->dev,
  1171. "ipg", iomem, &fsl_ssi_regconfig);
  1172. }
  1173. if (IS_ERR(ssi_private->regs)) {
  1174. dev_err(&pdev->dev, "Failed to init register map\n");
  1175. return PTR_ERR(ssi_private->regs);
  1176. }
  1177. ssi_private->irq = irq_of_parse_and_map(np, 0);
  1178. if (!ssi_private->irq) {
  1179. dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
  1180. return -ENXIO;
  1181. }
  1182. /* Are the RX and the TX clocks locked? */
  1183. if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
  1184. ssi_private->cpu_dai_drv.symmetric_rates = 1;
  1185. ssi_private->cpu_dai_drv.symmetric_channels = 1;
  1186. ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
  1187. }
  1188. /* Determine the FIFO depth. */
  1189. iprop = of_get_property(np, "fsl,fifo-depth", NULL);
  1190. if (iprop)
  1191. ssi_private->fifo_depth = be32_to_cpup(iprop);
  1192. else
  1193. /* Older 8610 DTs didn't have the fifo-depth property */
  1194. ssi_private->fifo_depth = 8;
  1195. dev_set_drvdata(&pdev->dev, ssi_private);
  1196. if (ssi_private->soc->imx) {
  1197. ret = fsl_ssi_imx_probe(pdev, ssi_private, iomem);
  1198. if (ret)
  1199. goto error_irqmap;
  1200. }
  1201. ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
  1202. &ssi_private->cpu_dai_drv, 1);
  1203. if (ret) {
  1204. dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
  1205. goto error_asoc_register;
  1206. }
  1207. if (ssi_private->use_dma) {
  1208. ret = devm_request_irq(&pdev->dev, ssi_private->irq,
  1209. fsl_ssi_isr, 0, dev_name(&pdev->dev),
  1210. ssi_private);
  1211. if (ret < 0) {
  1212. dev_err(&pdev->dev, "could not claim irq %u\n",
  1213. ssi_private->irq);
  1214. goto error_irq;
  1215. }
  1216. }
  1217. ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
  1218. if (ret)
  1219. goto error_asoc_register;
  1220. /*
  1221. * If codec-handle property is missing from SSI node, we assume
  1222. * that the machine driver uses new binding which does not require
  1223. * SSI driver to trigger machine driver's probe.
  1224. */
  1225. if (!of_get_property(np, "codec-handle", NULL))
  1226. goto done;
  1227. /* Trigger the machine driver's probe function. The platform driver
  1228. * name of the machine driver is taken from /compatible property of the
  1229. * device tree. We also pass the address of the CPU DAI driver
  1230. * structure.
  1231. */
  1232. sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
  1233. /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
  1234. p = strrchr(sprop, ',');
  1235. if (p)
  1236. sprop = p + 1;
  1237. snprintf(name, sizeof(name), "snd-soc-%s", sprop);
  1238. make_lowercase(name);
  1239. ssi_private->pdev =
  1240. platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
  1241. if (IS_ERR(ssi_private->pdev)) {
  1242. ret = PTR_ERR(ssi_private->pdev);
  1243. dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
  1244. goto error_sound_card;
  1245. }
  1246. done:
  1247. if (ssi_private->dai_fmt)
  1248. _fsl_ssi_set_dai_fmt(&pdev->dev, ssi_private,
  1249. ssi_private->dai_fmt);
  1250. return 0;
  1251. error_sound_card:
  1252. fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
  1253. error_irq:
  1254. snd_soc_unregister_component(&pdev->dev);
  1255. error_asoc_register:
  1256. if (ssi_private->soc->imx)
  1257. fsl_ssi_imx_clean(pdev, ssi_private);
  1258. error_irqmap:
  1259. if (ssi_private->use_dma)
  1260. irq_dispose_mapping(ssi_private->irq);
  1261. return ret;
  1262. }
  1263. static int fsl_ssi_remove(struct platform_device *pdev)
  1264. {
  1265. struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
  1266. fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
  1267. if (ssi_private->pdev)
  1268. platform_device_unregister(ssi_private->pdev);
  1269. snd_soc_unregister_component(&pdev->dev);
  1270. if (ssi_private->soc->imx)
  1271. fsl_ssi_imx_clean(pdev, ssi_private);
  1272. if (ssi_private->use_dma)
  1273. irq_dispose_mapping(ssi_private->irq);
  1274. return 0;
  1275. }
  1276. static struct platform_driver fsl_ssi_driver = {
  1277. .driver = {
  1278. .name = "fsl-ssi-dai",
  1279. .owner = THIS_MODULE,
  1280. .of_match_table = fsl_ssi_ids,
  1281. },
  1282. .probe = fsl_ssi_probe,
  1283. .remove = fsl_ssi_remove,
  1284. };
  1285. module_platform_driver(fsl_ssi_driver);
  1286. MODULE_ALIAS("platform:fsl-ssi-dai");
  1287. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  1288. MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
  1289. MODULE_LICENSE("GPL v2");