fsl_ssi.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  4. //
  5. // Author: Timur Tabi <timur@freescale.com>
  6. //
  7. // Copyright 2007-2010 Freescale Semiconductor, Inc.
  8. //
  9. // Some notes why imx-pcm-fiq is used instead of DMA on some boards:
  10. //
  11. // The i.MX SSI core has some nasty limitations in AC97 mode. While most
  12. // sane processor vendors have a FIFO per AC97 slot, the i.MX has only
  13. // one FIFO which combines all valid receive slots. We cannot even select
  14. // which slots we want to receive. The WM9712 with which this driver
  15. // was developed with always sends GPIO status data in slot 12 which
  16. // we receive in our (PCM-) data stream. The only chance we have is to
  17. // manually skip this data in the FIQ handler. With sampling rates different
  18. // from 48000Hz not every frame has valid receive data, so the ratio
  19. // between pcm data and GPIO status data changes. Our FIQ handler is not
  20. // able to handle this, hence this driver only works with 48000Hz sampling
  21. // rate.
  22. // Reading and writing AC97 registers is another challenge. The core
  23. // provides us status bits when the read register is updated with *another*
  24. // value. When we read the same register two times (and the register still
  25. // contains the same value) these status bits are not set. We work
  26. // around this by not polling these bits but only wait a fixed delay.
  27. #include <linux/init.h>
  28. #include <linux/io.h>
  29. #include <linux/module.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/clk.h>
  32. #include <linux/ctype.h>
  33. #include <linux/device.h>
  34. #include <linux/delay.h>
  35. #include <linux/mutex.h>
  36. #include <linux/slab.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/of.h>
  39. #include <linux/of_address.h>
  40. #include <linux/of_irq.h>
  41. #include <linux/of_platform.h>
  42. #include <sound/core.h>
  43. #include <sound/pcm.h>
  44. #include <sound/pcm_params.h>
  45. #include <sound/initval.h>
  46. #include <sound/soc.h>
  47. #include <sound/dmaengine_pcm.h>
  48. #include "fsl_ssi.h"
  49. #include "imx-pcm.h"
  50. /* Define RX and TX to index ssi->regvals array; Can be 0 or 1 only */
  51. #define RX 0
  52. #define TX 1
  53. /**
  54. * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
  55. *
  56. * The SSI has a limitation in that the samples must be in the same byte
  57. * order as the host CPU. This is because when multiple bytes are written
  58. * to the STX register, the bytes and bits must be written in the same
  59. * order. The STX is a shift register, so all the bits need to be aligned
  60. * (bit-endianness must match byte-endianness). Processors typically write
  61. * the bits within a byte in the same order that the bytes of a word are
  62. * written in. So if the host CPU is big-endian, then only big-endian
  63. * samples will be written to STX properly.
  64. */
  65. #ifdef __BIG_ENDIAN
  66. #define FSLSSI_I2S_FORMATS \
  67. (SNDRV_PCM_FMTBIT_S8 | \
  68. SNDRV_PCM_FMTBIT_S16_BE | \
  69. SNDRV_PCM_FMTBIT_S18_3BE | \
  70. SNDRV_PCM_FMTBIT_S20_3BE | \
  71. SNDRV_PCM_FMTBIT_S24_3BE | \
  72. SNDRV_PCM_FMTBIT_S24_BE)
  73. #else
  74. #define FSLSSI_I2S_FORMATS \
  75. (SNDRV_PCM_FMTBIT_S8 | \
  76. SNDRV_PCM_FMTBIT_S16_LE | \
  77. SNDRV_PCM_FMTBIT_S18_3LE | \
  78. SNDRV_PCM_FMTBIT_S20_3LE | \
  79. SNDRV_PCM_FMTBIT_S24_3LE | \
  80. SNDRV_PCM_FMTBIT_S24_LE)
  81. #endif
  82. /*
  83. * In AC97 mode, TXDIR bit is forced to 0 and TFDIR bit is forced to 1:
  84. * - SSI inputs external bit clock and outputs frame sync clock -- CBM_CFS
  85. * - Also have NB_NF to mark these two clocks will not be inverted
  86. */
  87. #define FSLSSI_AC97_DAIFMT \
  88. (SND_SOC_DAIFMT_AC97 | \
  89. SND_SOC_DAIFMT_CBM_CFS | \
  90. SND_SOC_DAIFMT_NB_NF)
  91. #define FSLSSI_SIER_DBG_RX_FLAGS \
  92. (SSI_SIER_RFF0_EN | \
  93. SSI_SIER_RLS_EN | \
  94. SSI_SIER_RFS_EN | \
  95. SSI_SIER_ROE0_EN | \
  96. SSI_SIER_RFRC_EN)
  97. #define FSLSSI_SIER_DBG_TX_FLAGS \
  98. (SSI_SIER_TFE0_EN | \
  99. SSI_SIER_TLS_EN | \
  100. SSI_SIER_TFS_EN | \
  101. SSI_SIER_TUE0_EN | \
  102. SSI_SIER_TFRC_EN)
  103. enum fsl_ssi_type {
  104. FSL_SSI_MCP8610,
  105. FSL_SSI_MX21,
  106. FSL_SSI_MX35,
  107. FSL_SSI_MX51,
  108. };
  109. struct fsl_ssi_regvals {
  110. u32 sier;
  111. u32 srcr;
  112. u32 stcr;
  113. u32 scr;
  114. };
  115. static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg)
  116. {
  117. switch (reg) {
  118. case REG_SSI_SACCEN:
  119. case REG_SSI_SACCDIS:
  120. return false;
  121. default:
  122. return true;
  123. }
  124. }
  125. static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg)
  126. {
  127. switch (reg) {
  128. case REG_SSI_STX0:
  129. case REG_SSI_STX1:
  130. case REG_SSI_SRX0:
  131. case REG_SSI_SRX1:
  132. case REG_SSI_SISR:
  133. case REG_SSI_SFCSR:
  134. case REG_SSI_SACNT:
  135. case REG_SSI_SACADD:
  136. case REG_SSI_SACDAT:
  137. case REG_SSI_SATAG:
  138. case REG_SSI_SACCST:
  139. case REG_SSI_SOR:
  140. return true;
  141. default:
  142. return false;
  143. }
  144. }
  145. static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg)
  146. {
  147. switch (reg) {
  148. case REG_SSI_SRX0:
  149. case REG_SSI_SRX1:
  150. case REG_SSI_SISR:
  151. case REG_SSI_SACADD:
  152. case REG_SSI_SACDAT:
  153. case REG_SSI_SATAG:
  154. return true;
  155. default:
  156. return false;
  157. }
  158. }
  159. static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg)
  160. {
  161. switch (reg) {
  162. case REG_SSI_SRX0:
  163. case REG_SSI_SRX1:
  164. case REG_SSI_SACCST:
  165. return false;
  166. default:
  167. return true;
  168. }
  169. }
  170. static const struct regmap_config fsl_ssi_regconfig = {
  171. .max_register = REG_SSI_SACCDIS,
  172. .reg_bits = 32,
  173. .val_bits = 32,
  174. .reg_stride = 4,
  175. .val_format_endian = REGMAP_ENDIAN_NATIVE,
  176. .num_reg_defaults_raw = REG_SSI_SACCDIS / sizeof(uint32_t) + 1,
  177. .readable_reg = fsl_ssi_readable_reg,
  178. .volatile_reg = fsl_ssi_volatile_reg,
  179. .precious_reg = fsl_ssi_precious_reg,
  180. .writeable_reg = fsl_ssi_writeable_reg,
  181. .cache_type = REGCACHE_FLAT,
  182. };
  183. struct fsl_ssi_soc_data {
  184. bool imx;
  185. bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */
  186. bool offline_config;
  187. u32 sisr_write_mask;
  188. };
  189. /**
  190. * fsl_ssi: per-SSI private data
  191. *
  192. * @regs: Pointer to the regmap registers
  193. * @irq: IRQ of this SSI
  194. * @cpu_dai_drv: CPU DAI driver for this device
  195. *
  196. * @dai_fmt: DAI configuration this device is currently used with
  197. * @streams: Mask of current active streams: BIT(TX) and BIT(RX)
  198. * @i2s_net: I2S and Network mode configurations of SCR register
  199. * (this is the initial settings based on the DAI format)
  200. * @synchronous: Use synchronous mode - both of TX and RX use STCK and SFCK
  201. * @use_dma: DMA is used or FIQ with stream filter
  202. * @use_dual_fifo: DMA with support for dual FIFO mode
  203. * @has_ipg_clk_name: If "ipg" is in the clock name list of device tree
  204. * @fifo_depth: Depth of the SSI FIFOs
  205. * @slot_width: Width of each DAI slot
  206. * @slots: Number of slots
  207. * @regvals: Specific RX/TX register settings
  208. *
  209. * @clk: Clock source to access register
  210. * @baudclk: Clock source to generate bit and frame-sync clocks
  211. * @baudclk_streams: Active streams that are using baudclk
  212. *
  213. * @regcache_sfcsr: Cache sfcsr register value during suspend and resume
  214. * @regcache_sacnt: Cache sacnt register value during suspend and resume
  215. *
  216. * @dma_params_tx: DMA transmit parameters
  217. * @dma_params_rx: DMA receive parameters
  218. * @ssi_phys: physical address of the SSI registers
  219. *
  220. * @fiq_params: FIQ stream filtering parameters
  221. *
  222. * @card_pdev: Platform_device pointer to register a sound card for PowerPC or
  223. * to register a CODEC platform device for AC97
  224. * @card_name: Platform_device name to register a sound card for PowerPC or
  225. * to register a CODEC platform device for AC97
  226. * @card_idx: The index of SSI to register a sound card for PowerPC or
  227. * to register a CODEC platform device for AC97
  228. *
  229. * @dbg_stats: Debugging statistics
  230. *
  231. * @soc: SoC specific data
  232. * @dev: Pointer to &pdev->dev
  233. *
  234. * @fifo_watermark: The FIFO watermark setting. Notifies DMA when there are
  235. * @fifo_watermark or fewer words in TX fifo or
  236. * @fifo_watermark or more empty words in RX fifo.
  237. * @dma_maxburst: Max number of words to transfer in one go. So far,
  238. * this is always the same as fifo_watermark.
  239. *
  240. * @ac97_reg_lock: Mutex lock to serialize AC97 register access operations
  241. */
  242. struct fsl_ssi {
  243. struct regmap *regs;
  244. int irq;
  245. struct snd_soc_dai_driver cpu_dai_drv;
  246. unsigned int dai_fmt;
  247. u8 streams;
  248. u8 i2s_net;
  249. bool synchronous;
  250. bool use_dma;
  251. bool use_dual_fifo;
  252. bool has_ipg_clk_name;
  253. unsigned int fifo_depth;
  254. unsigned int slot_width;
  255. unsigned int slots;
  256. struct fsl_ssi_regvals regvals[2];
  257. struct clk *clk;
  258. struct clk *baudclk;
  259. unsigned int baudclk_streams;
  260. u32 regcache_sfcsr;
  261. u32 regcache_sacnt;
  262. struct snd_dmaengine_dai_dma_data dma_params_tx;
  263. struct snd_dmaengine_dai_dma_data dma_params_rx;
  264. dma_addr_t ssi_phys;
  265. struct imx_pcm_fiq_params fiq_params;
  266. struct platform_device *card_pdev;
  267. char card_name[32];
  268. u32 card_idx;
  269. struct fsl_ssi_dbg dbg_stats;
  270. const struct fsl_ssi_soc_data *soc;
  271. struct device *dev;
  272. u32 fifo_watermark;
  273. u32 dma_maxburst;
  274. struct mutex ac97_reg_lock;
  275. };
  276. /*
  277. * SoC specific data
  278. *
  279. * Notes:
  280. * 1) SSI in earlier SoCS has critical bits in control registers that
  281. * cannot be changed after SSI starts running -- a software reset
  282. * (set SSIEN to 0) is required to change their values. So adding
  283. * an offline_config flag for these SoCs.
  284. * 2) SDMA is available since imx35. However, imx35 does not support
  285. * DMA bits changing when SSI is running, so set offline_config.
  286. * 3) imx51 and later versions support register configurations when
  287. * SSI is running (SSIEN); For these versions, DMA needs to be
  288. * configured before SSI sends DMA request to avoid an undefined
  289. * DMA request on the SDMA side.
  290. */
  291. static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
  292. .imx = false,
  293. .offline_config = true,
  294. .sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC |
  295. SSI_SISR_ROE0 | SSI_SISR_ROE1 |
  296. SSI_SISR_TUE0 | SSI_SISR_TUE1,
  297. };
  298. static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
  299. .imx = true,
  300. .imx21regs = true,
  301. .offline_config = true,
  302. .sisr_write_mask = 0,
  303. };
  304. static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
  305. .imx = true,
  306. .offline_config = true,
  307. .sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC |
  308. SSI_SISR_ROE0 | SSI_SISR_ROE1 |
  309. SSI_SISR_TUE0 | SSI_SISR_TUE1,
  310. };
  311. static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
  312. .imx = true,
  313. .offline_config = false,
  314. .sisr_write_mask = SSI_SISR_ROE0 | SSI_SISR_ROE1 |
  315. SSI_SISR_TUE0 | SSI_SISR_TUE1,
  316. };
  317. static const struct of_device_id fsl_ssi_ids[] = {
  318. { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
  319. { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
  320. { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
  321. { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
  322. {}
  323. };
  324. MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
  325. static bool fsl_ssi_is_ac97(struct fsl_ssi *ssi)
  326. {
  327. return (ssi->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
  328. SND_SOC_DAIFMT_AC97;
  329. }
  330. static bool fsl_ssi_is_i2s_master(struct fsl_ssi *ssi)
  331. {
  332. return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
  333. SND_SOC_DAIFMT_CBS_CFS;
  334. }
  335. static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi *ssi)
  336. {
  337. return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
  338. SND_SOC_DAIFMT_CBM_CFS;
  339. }
  340. /**
  341. * Interrupt handler to gather states
  342. */
  343. static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
  344. {
  345. struct fsl_ssi *ssi = dev_id;
  346. struct regmap *regs = ssi->regs;
  347. u32 sisr, sisr2;
  348. regmap_read(regs, REG_SSI_SISR, &sisr);
  349. sisr2 = sisr & ssi->soc->sisr_write_mask;
  350. /* Clear the bits that we set */
  351. if (sisr2)
  352. regmap_write(regs, REG_SSI_SISR, sisr2);
  353. fsl_ssi_dbg_isr(&ssi->dbg_stats, sisr);
  354. return IRQ_HANDLED;
  355. }
  356. /**
  357. * Set SCR, SIER, STCR and SRCR registers with cached values in regvals
  358. *
  359. * Notes:
  360. * 1) For offline_config SoCs, enable all necessary bits of both streams
  361. * when 1st stream starts, even if the opposite stream will not start
  362. * 2) It also clears FIFO before setting regvals; SOR is safe to set online
  363. */
  364. static void fsl_ssi_config_enable(struct fsl_ssi *ssi, bool tx)
  365. {
  366. struct fsl_ssi_regvals *vals = ssi->regvals;
  367. int dir = tx ? TX : RX;
  368. u32 sier, srcr, stcr;
  369. /* Clear dirty data in the FIFO; It also prevents channel slipping */
  370. regmap_update_bits(ssi->regs, REG_SSI_SOR,
  371. SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx));
  372. /*
  373. * On offline_config SoCs, SxCR and SIER are already configured when
  374. * the previous stream started. So skip all SxCR and SIER settings
  375. * to prevent online reconfigurations, then jump to set SCR directly
  376. */
  377. if (ssi->soc->offline_config && ssi->streams)
  378. goto enable_scr;
  379. if (ssi->soc->offline_config) {
  380. /*
  381. * Online reconfiguration not supported, so enable all bits for
  382. * both streams at once to avoid necessity of reconfigurations
  383. */
  384. srcr = vals[RX].srcr | vals[TX].srcr;
  385. stcr = vals[RX].stcr | vals[TX].stcr;
  386. sier = vals[RX].sier | vals[TX].sier;
  387. } else {
  388. /* Otherwise, only set bits for the current stream */
  389. srcr = vals[dir].srcr;
  390. stcr = vals[dir].stcr;
  391. sier = vals[dir].sier;
  392. }
  393. /* Configure SRCR, STCR and SIER at once */
  394. regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, srcr);
  395. regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, stcr);
  396. regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, sier);
  397. enable_scr:
  398. /*
  399. * Start DMA before setting TE to avoid FIFO underrun
  400. * which may cause a channel slip or a channel swap
  401. *
  402. * TODO: FIQ cases might also need this upon testing
  403. */
  404. if (ssi->use_dma && tx) {
  405. int try = 100;
  406. u32 sfcsr;
  407. /* Enable SSI first to send TX DMA request */
  408. regmap_update_bits(ssi->regs, REG_SSI_SCR,
  409. SSI_SCR_SSIEN, SSI_SCR_SSIEN);
  410. /* Busy wait until TX FIFO not empty -- DMA working */
  411. do {
  412. regmap_read(ssi->regs, REG_SSI_SFCSR, &sfcsr);
  413. if (SSI_SFCSR_TFCNT0(sfcsr))
  414. break;
  415. } while (--try);
  416. /* FIFO still empty -- something might be wrong */
  417. if (!SSI_SFCSR_TFCNT0(sfcsr))
  418. dev_warn(ssi->dev, "Timeout waiting TX FIFO filling\n");
  419. }
  420. /* Enable all remaining bits in SCR */
  421. regmap_update_bits(ssi->regs, REG_SSI_SCR,
  422. vals[dir].scr, vals[dir].scr);
  423. /* Log the enabled stream to the mask */
  424. ssi->streams |= BIT(dir);
  425. }
  426. /**
  427. * Exclude bits that are used by the opposite stream
  428. *
  429. * When both streams are active, disabling some bits for the current stream
  430. * might break the other stream if these bits are used by it.
  431. *
  432. * @vals : regvals of the current stream
  433. * @avals: regvals of the opposite stream
  434. * @aactive: active state of the opposite stream
  435. *
  436. * 1) XOR vals and avals to get the differences if the other stream is active;
  437. * Otherwise, return current vals if the other stream is not active
  438. * 2) AND the result of 1) with the current vals
  439. */
  440. #define _ssi_xor_shared_bits(vals, avals, aactive) \
  441. ((vals) ^ ((avals) * (aactive)))
  442. #define ssi_excl_shared_bits(vals, avals, aactive) \
  443. ((vals) & _ssi_xor_shared_bits(vals, avals, aactive))
  444. /**
  445. * Unset SCR, SIER, STCR and SRCR registers with cached values in regvals
  446. *
  447. * Notes:
  448. * 1) For offline_config SoCs, to avoid online reconfigurations, disable all
  449. * bits of both streams at once when the last stream is abort to end
  450. * 2) It also clears FIFO after unsetting regvals; SOR is safe to set online
  451. */
  452. static void fsl_ssi_config_disable(struct fsl_ssi *ssi, bool tx)
  453. {
  454. struct fsl_ssi_regvals *vals, *avals;
  455. u32 sier, srcr, stcr, scr;
  456. int adir = tx ? RX : TX;
  457. int dir = tx ? TX : RX;
  458. bool aactive;
  459. /* Check if the opposite stream is active */
  460. aactive = ssi->streams & BIT(adir);
  461. vals = &ssi->regvals[dir];
  462. /* Get regvals of the opposite stream to keep opposite stream safe */
  463. avals = &ssi->regvals[adir];
  464. /*
  465. * To keep the other stream safe, exclude shared bits between
  466. * both streams, and get safe bits to disable current stream
  467. */
  468. scr = ssi_excl_shared_bits(vals->scr, avals->scr, aactive);
  469. /* Disable safe bits of SCR register for the current stream */
  470. regmap_update_bits(ssi->regs, REG_SSI_SCR, scr, 0);
  471. /* Log the disabled stream to the mask */
  472. ssi->streams &= ~BIT(dir);
  473. /*
  474. * On offline_config SoCs, if the other stream is active, skip
  475. * SxCR and SIER settings to prevent online reconfigurations
  476. */
  477. if (ssi->soc->offline_config && aactive)
  478. goto fifo_clear;
  479. if (ssi->soc->offline_config) {
  480. /* Now there is only current stream active, disable all bits */
  481. srcr = vals->srcr | avals->srcr;
  482. stcr = vals->stcr | avals->stcr;
  483. sier = vals->sier | avals->sier;
  484. } else {
  485. /*
  486. * To keep the other stream safe, exclude shared bits between
  487. * both streams, and get safe bits to disable current stream
  488. */
  489. sier = ssi_excl_shared_bits(vals->sier, avals->sier, aactive);
  490. srcr = ssi_excl_shared_bits(vals->srcr, avals->srcr, aactive);
  491. stcr = ssi_excl_shared_bits(vals->stcr, avals->stcr, aactive);
  492. }
  493. /* Clear configurations of SRCR, STCR and SIER at once */
  494. regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, 0);
  495. regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, 0);
  496. regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, 0);
  497. fifo_clear:
  498. /* Clear remaining data in the FIFO */
  499. regmap_update_bits(ssi->regs, REG_SSI_SOR,
  500. SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx));
  501. }
  502. static void fsl_ssi_tx_ac97_saccst_setup(struct fsl_ssi *ssi)
  503. {
  504. struct regmap *regs = ssi->regs;
  505. /* no SACC{ST,EN,DIS} regs on imx21-class SSI */
  506. if (!ssi->soc->imx21regs) {
  507. /* Disable all channel slots */
  508. regmap_write(regs, REG_SSI_SACCDIS, 0xff);
  509. /* Enable slots 3 & 4 -- PCM Playback Left & Right channels */
  510. regmap_write(regs, REG_SSI_SACCEN, 0x300);
  511. }
  512. }
  513. /**
  514. * Cache critical bits of SIER, SRCR, STCR and SCR to later set them safely
  515. */
  516. static void fsl_ssi_setup_regvals(struct fsl_ssi *ssi)
  517. {
  518. struct fsl_ssi_regvals *vals = ssi->regvals;
  519. vals[RX].sier = SSI_SIER_RFF0_EN | FSLSSI_SIER_DBG_RX_FLAGS;
  520. vals[RX].srcr = SSI_SRCR_RFEN0;
  521. vals[RX].scr = SSI_SCR_SSIEN | SSI_SCR_RE;
  522. vals[TX].sier = SSI_SIER_TFE0_EN | FSLSSI_SIER_DBG_TX_FLAGS;
  523. vals[TX].stcr = SSI_STCR_TFEN0;
  524. vals[TX].scr = SSI_SCR_SSIEN | SSI_SCR_TE;
  525. /* AC97 has already enabled SSIEN, RE and TE, so ignore them */
  526. if (fsl_ssi_is_ac97(ssi))
  527. vals[RX].scr = vals[TX].scr = 0;
  528. if (ssi->use_dual_fifo) {
  529. vals[RX].srcr |= SSI_SRCR_RFEN1;
  530. vals[TX].stcr |= SSI_STCR_TFEN1;
  531. }
  532. if (ssi->use_dma) {
  533. vals[RX].sier |= SSI_SIER_RDMAE;
  534. vals[TX].sier |= SSI_SIER_TDMAE;
  535. } else {
  536. vals[RX].sier |= SSI_SIER_RIE;
  537. vals[TX].sier |= SSI_SIER_TIE;
  538. }
  539. }
  540. static void fsl_ssi_setup_ac97(struct fsl_ssi *ssi)
  541. {
  542. struct regmap *regs = ssi->regs;
  543. /* Setup the clock control register */
  544. regmap_write(regs, REG_SSI_STCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13));
  545. regmap_write(regs, REG_SSI_SRCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13));
  546. /* Enable AC97 mode and startup the SSI */
  547. regmap_write(regs, REG_SSI_SACNT, SSI_SACNT_AC97EN | SSI_SACNT_FV);
  548. /* AC97 has to communicate with codec before starting a stream */
  549. regmap_update_bits(regs, REG_SSI_SCR,
  550. SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE,
  551. SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE);
  552. regmap_write(regs, REG_SSI_SOR, SSI_SOR_WAIT(3));
  553. }
  554. static int fsl_ssi_startup(struct snd_pcm_substream *substream,
  555. struct snd_soc_dai *dai)
  556. {
  557. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  558. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  559. int ret;
  560. ret = clk_prepare_enable(ssi->clk);
  561. if (ret)
  562. return ret;
  563. /*
  564. * When using dual fifo mode, it is safer to ensure an even period
  565. * size. If appearing to an odd number while DMA always starts its
  566. * task from fifo0, fifo1 would be neglected at the end of each
  567. * period. But SSI would still access fifo1 with an invalid data.
  568. */
  569. if (ssi->use_dual_fifo)
  570. snd_pcm_hw_constraint_step(substream->runtime, 0,
  571. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
  572. return 0;
  573. }
  574. static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
  575. struct snd_soc_dai *dai)
  576. {
  577. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  578. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  579. clk_disable_unprepare(ssi->clk);
  580. }
  581. /**
  582. * Configure Digital Audio Interface bit clock
  583. *
  584. * Note: This function can be only called when using SSI as DAI master
  585. *
  586. * Quick instruction for parameters:
  587. * freq: Output BCLK frequency = samplerate * slots * slot_width
  588. * (In 2-channel I2S Master mode, slot_width is fixed 32)
  589. */
  590. static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
  591. struct snd_soc_dai *dai,
  592. struct snd_pcm_hw_params *hw_params)
  593. {
  594. bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  595. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
  596. struct regmap *regs = ssi->regs;
  597. u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
  598. unsigned long clkrate, baudrate, tmprate;
  599. unsigned int channels = params_channels(hw_params);
  600. unsigned int slot_width = params_width(hw_params);
  601. unsigned int slots = 2;
  602. u64 sub, savesub = 100000;
  603. unsigned int freq;
  604. bool baudclk_is_used;
  605. int ret;
  606. /* Override slots and slot_width if being specifically set... */
  607. if (ssi->slots)
  608. slots = ssi->slots;
  609. if (ssi->slot_width)
  610. slot_width = ssi->slot_width;
  611. /* ...but force 32 bits for stereo audio using I2S Master Mode */
  612. if (channels == 2 &&
  613. (ssi->i2s_net & SSI_SCR_I2S_MODE_MASK) == SSI_SCR_I2S_MODE_MASTER)
  614. slot_width = 32;
  615. /* Generate bit clock based on the slot number and slot width */
  616. freq = slots * slot_width * params_rate(hw_params);
  617. /* Don't apply it to any non-baudclk circumstance */
  618. if (IS_ERR(ssi->baudclk))
  619. return -EINVAL;
  620. /*
  621. * Hardware limitation: The bclk rate must be
  622. * never greater than 1/5 IPG clock rate
  623. */
  624. if (freq * 5 > clk_get_rate(ssi->clk)) {
  625. dev_err(dai->dev, "bitclk > ipgclk / 5\n");
  626. return -EINVAL;
  627. }
  628. baudclk_is_used = ssi->baudclk_streams & ~(BIT(substream->stream));
  629. /* It should be already enough to divide clock by setting pm alone */
  630. psr = 0;
  631. div2 = 0;
  632. factor = (div2 + 1) * (7 * psr + 1) * 2;
  633. for (i = 0; i < 255; i++) {
  634. tmprate = freq * factor * (i + 1);
  635. if (baudclk_is_used)
  636. clkrate = clk_get_rate(ssi->baudclk);
  637. else
  638. clkrate = clk_round_rate(ssi->baudclk, tmprate);
  639. clkrate /= factor;
  640. afreq = clkrate / (i + 1);
  641. if (freq == afreq)
  642. sub = 0;
  643. else if (freq / afreq == 1)
  644. sub = freq - afreq;
  645. else if (afreq / freq == 1)
  646. sub = afreq - freq;
  647. else
  648. continue;
  649. /* Calculate the fraction */
  650. sub *= 100000;
  651. do_div(sub, freq);
  652. if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) {
  653. baudrate = tmprate;
  654. savesub = sub;
  655. pm = i;
  656. }
  657. /* We are lucky */
  658. if (savesub == 0)
  659. break;
  660. }
  661. /* No proper pm found if it is still remaining the initial value */
  662. if (pm == 999) {
  663. dev_err(dai->dev, "failed to handle the required sysclk\n");
  664. return -EINVAL;
  665. }
  666. stccr = SSI_SxCCR_PM(pm + 1) | (div2 ? SSI_SxCCR_DIV2 : 0) |
  667. (psr ? SSI_SxCCR_PSR : 0);
  668. mask = SSI_SxCCR_PM_MASK | SSI_SxCCR_DIV2 | SSI_SxCCR_PSR;
  669. /* STCCR is used for RX in synchronous mode */
  670. tx2 = tx || ssi->synchronous;
  671. regmap_update_bits(regs, REG_SSI_SxCCR(tx2), mask, stccr);
  672. if (!baudclk_is_used) {
  673. ret = clk_set_rate(ssi->baudclk, baudrate);
  674. if (ret) {
  675. dev_err(dai->dev, "failed to set baudclk rate\n");
  676. return -EINVAL;
  677. }
  678. }
  679. return 0;
  680. }
  681. /**
  682. * Configure SSI based on PCM hardware parameters
  683. *
  684. * Notes:
  685. * 1) SxCCR.WL bits are critical bits that require SSI to be temporarily
  686. * disabled on offline_config SoCs. Even for online configurable SoCs
  687. * running in synchronous mode (both TX and RX use STCCR), it is not
  688. * safe to re-configure them when both two streams start running.
  689. * 2) SxCCR.PM, SxCCR.DIV2 and SxCCR.PSR bits will be configured in the
  690. * fsl_ssi_set_bclk() if SSI is the DAI clock master.
  691. */
  692. static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
  693. struct snd_pcm_hw_params *hw_params,
  694. struct snd_soc_dai *dai)
  695. {
  696. bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  697. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
  698. struct regmap *regs = ssi->regs;
  699. unsigned int channels = params_channels(hw_params);
  700. unsigned int sample_size = params_width(hw_params);
  701. u32 wl = SSI_SxCCR_WL(sample_size);
  702. int ret;
  703. if (fsl_ssi_is_i2s_master(ssi)) {
  704. ret = fsl_ssi_set_bclk(substream, dai, hw_params);
  705. if (ret)
  706. return ret;
  707. /* Do not enable the clock if it is already enabled */
  708. if (!(ssi->baudclk_streams & BIT(substream->stream))) {
  709. ret = clk_prepare_enable(ssi->baudclk);
  710. if (ret)
  711. return ret;
  712. ssi->baudclk_streams |= BIT(substream->stream);
  713. }
  714. }
  715. /*
  716. * SSI is properly configured if it is enabled and running in
  717. * the synchronous mode; Note that AC97 mode is an exception
  718. * that should set separate configurations for STCCR and SRCCR
  719. * despite running in the synchronous mode.
  720. */
  721. if (ssi->streams && ssi->synchronous)
  722. return 0;
  723. if (!fsl_ssi_is_ac97(ssi)) {
  724. /*
  725. * Keep the ssi->i2s_net intact while having a local variable
  726. * to override settings for special use cases. Otherwise, the
  727. * ssi->i2s_net will lose the settings for regular use cases.
  728. */
  729. u8 i2s_net = ssi->i2s_net;
  730. /* Normal + Network mode to send 16-bit data in 32-bit frames */
  731. if (fsl_ssi_is_i2s_cbm_cfs(ssi) && sample_size == 16)
  732. i2s_net = SSI_SCR_I2S_MODE_NORMAL | SSI_SCR_NET;
  733. /* Use Normal mode to send mono data at 1st slot of 2 slots */
  734. if (channels == 1)
  735. i2s_net = SSI_SCR_I2S_MODE_NORMAL;
  736. regmap_update_bits(regs, REG_SSI_SCR,
  737. SSI_SCR_I2S_NET_MASK, i2s_net);
  738. }
  739. /* In synchronous mode, the SSI uses STCCR for capture */
  740. tx2 = tx || ssi->synchronous;
  741. regmap_update_bits(regs, REG_SSI_SxCCR(tx2), SSI_SxCCR_WL_MASK, wl);
  742. return 0;
  743. }
  744. static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
  745. struct snd_soc_dai *dai)
  746. {
  747. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  748. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  749. if (fsl_ssi_is_i2s_master(ssi) &&
  750. ssi->baudclk_streams & BIT(substream->stream)) {
  751. clk_disable_unprepare(ssi->baudclk);
  752. ssi->baudclk_streams &= ~BIT(substream->stream);
  753. }
  754. return 0;
  755. }
  756. static int _fsl_ssi_set_dai_fmt(struct fsl_ssi *ssi, unsigned int fmt)
  757. {
  758. u32 strcr = 0, scr = 0, stcr, srcr, mask;
  759. unsigned int slots;
  760. ssi->dai_fmt = fmt;
  761. /* Synchronize frame sync clock for TE to avoid data slipping */
  762. scr |= SSI_SCR_SYNC_TX_FS;
  763. /* Set to default shifting settings: LSB_ALIGNED */
  764. strcr |= SSI_STCR_TXBIT0;
  765. /* Use Network mode as default */
  766. ssi->i2s_net = SSI_SCR_NET;
  767. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  768. case SND_SOC_DAIFMT_I2S:
  769. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  770. case SND_SOC_DAIFMT_CBS_CFS:
  771. if (IS_ERR(ssi->baudclk)) {
  772. dev_err(ssi->dev,
  773. "missing baudclk for master mode\n");
  774. return -EINVAL;
  775. }
  776. /* fall through */
  777. case SND_SOC_DAIFMT_CBM_CFS:
  778. ssi->i2s_net |= SSI_SCR_I2S_MODE_MASTER;
  779. break;
  780. case SND_SOC_DAIFMT_CBM_CFM:
  781. ssi->i2s_net |= SSI_SCR_I2S_MODE_SLAVE;
  782. break;
  783. default:
  784. return -EINVAL;
  785. }
  786. slots = ssi->slots ? : 2;
  787. regmap_update_bits(ssi->regs, REG_SSI_STCCR,
  788. SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
  789. regmap_update_bits(ssi->regs, REG_SSI_SRCCR,
  790. SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
  791. /* Data on rising edge of bclk, frame low, 1clk before data */
  792. strcr |= SSI_STCR_TFSI | SSI_STCR_TSCKP | SSI_STCR_TEFS;
  793. break;
  794. case SND_SOC_DAIFMT_LEFT_J:
  795. /* Data on rising edge of bclk, frame high */
  796. strcr |= SSI_STCR_TSCKP;
  797. break;
  798. case SND_SOC_DAIFMT_DSP_A:
  799. /* Data on rising edge of bclk, frame high, 1clk before data */
  800. strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP | SSI_STCR_TEFS;
  801. break;
  802. case SND_SOC_DAIFMT_DSP_B:
  803. /* Data on rising edge of bclk, frame high */
  804. strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP;
  805. break;
  806. case SND_SOC_DAIFMT_AC97:
  807. /* Data on falling edge of bclk, frame high, 1clk before data */
  808. strcr |= SSI_STCR_TEFS;
  809. break;
  810. default:
  811. return -EINVAL;
  812. }
  813. scr |= ssi->i2s_net;
  814. /* DAI clock inversion */
  815. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  816. case SND_SOC_DAIFMT_NB_NF:
  817. /* Nothing to do for both normal cases */
  818. break;
  819. case SND_SOC_DAIFMT_IB_NF:
  820. /* Invert bit clock */
  821. strcr ^= SSI_STCR_TSCKP;
  822. break;
  823. case SND_SOC_DAIFMT_NB_IF:
  824. /* Invert frame clock */
  825. strcr ^= SSI_STCR_TFSI;
  826. break;
  827. case SND_SOC_DAIFMT_IB_IF:
  828. /* Invert both clocks */
  829. strcr ^= SSI_STCR_TSCKP;
  830. strcr ^= SSI_STCR_TFSI;
  831. break;
  832. default:
  833. return -EINVAL;
  834. }
  835. /* DAI clock master masks */
  836. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  837. case SND_SOC_DAIFMT_CBS_CFS:
  838. /* Output bit and frame sync clocks */
  839. strcr |= SSI_STCR_TFDIR | SSI_STCR_TXDIR;
  840. scr |= SSI_SCR_SYS_CLK_EN;
  841. break;
  842. case SND_SOC_DAIFMT_CBM_CFM:
  843. /* Input bit or frame sync clocks */
  844. break;
  845. case SND_SOC_DAIFMT_CBM_CFS:
  846. /* Input bit clock but output frame sync clock */
  847. strcr |= SSI_STCR_TFDIR;
  848. break;
  849. default:
  850. return -EINVAL;
  851. }
  852. stcr = strcr;
  853. srcr = strcr;
  854. /* Set SYN mode and clear RXDIR bit when using SYN or AC97 mode */
  855. if (ssi->synchronous || fsl_ssi_is_ac97(ssi)) {
  856. srcr &= ~SSI_SRCR_RXDIR;
  857. scr |= SSI_SCR_SYN;
  858. }
  859. mask = SSI_STCR_TFDIR | SSI_STCR_TXDIR | SSI_STCR_TSCKP |
  860. SSI_STCR_TFSL | SSI_STCR_TFSI | SSI_STCR_TEFS | SSI_STCR_TXBIT0;
  861. regmap_update_bits(ssi->regs, REG_SSI_STCR, mask, stcr);
  862. regmap_update_bits(ssi->regs, REG_SSI_SRCR, mask, srcr);
  863. mask = SSI_SCR_SYNC_TX_FS | SSI_SCR_I2S_MODE_MASK |
  864. SSI_SCR_SYS_CLK_EN | SSI_SCR_SYN;
  865. regmap_update_bits(ssi->regs, REG_SSI_SCR, mask, scr);
  866. return 0;
  867. }
  868. /**
  869. * Configure Digital Audio Interface (DAI) Format
  870. */
  871. static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  872. {
  873. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
  874. /* AC97 configured DAIFMT earlier in the probe() */
  875. if (fsl_ssi_is_ac97(ssi))
  876. return 0;
  877. return _fsl_ssi_set_dai_fmt(ssi, fmt);
  878. }
  879. /**
  880. * Set TDM slot number and slot width
  881. */
  882. static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask,
  883. u32 rx_mask, int slots, int slot_width)
  884. {
  885. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
  886. struct regmap *regs = ssi->regs;
  887. u32 val;
  888. /* The word length should be 8, 10, 12, 16, 18, 20, 22 or 24 */
  889. if (slot_width & 1 || slot_width < 8 || slot_width > 24) {
  890. dev_err(dai->dev, "invalid slot width: %d\n", slot_width);
  891. return -EINVAL;
  892. }
  893. /* The slot number should be >= 2 if using Network mode or I2S mode */
  894. if (ssi->i2s_net && slots < 2) {
  895. dev_err(dai->dev, "slot number should be >= 2 in I2S or NET\n");
  896. return -EINVAL;
  897. }
  898. regmap_update_bits(regs, REG_SSI_STCCR,
  899. SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
  900. regmap_update_bits(regs, REG_SSI_SRCCR,
  901. SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
  902. /* Save the SCR register value */
  903. regmap_read(regs, REG_SSI_SCR, &val);
  904. /* Temporarily enable SSI to allow SxMSKs to be configurable */
  905. regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, SSI_SCR_SSIEN);
  906. regmap_write(regs, REG_SSI_STMSK, ~tx_mask);
  907. regmap_write(regs, REG_SSI_SRMSK, ~rx_mask);
  908. /* Restore the value of SSIEN bit */
  909. regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, val);
  910. ssi->slot_width = slot_width;
  911. ssi->slots = slots;
  912. return 0;
  913. }
  914. /**
  915. * Start or stop SSI and corresponding DMA transaction.
  916. *
  917. * The DMA channel is in external master start and pause mode, which
  918. * means the SSI completely controls the flow of data.
  919. */
  920. static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
  921. struct snd_soc_dai *dai)
  922. {
  923. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  924. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  925. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  926. switch (cmd) {
  927. case SNDRV_PCM_TRIGGER_START:
  928. case SNDRV_PCM_TRIGGER_RESUME:
  929. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  930. /*
  931. * SACCST might be modified via AC Link by a CODEC if it sends
  932. * extra bits in their SLOTREQ requests, which'll accidentally
  933. * send valid data to slots other than normal playback slots.
  934. *
  935. * To be safe, configure SACCST right before TX starts.
  936. */
  937. if (tx && fsl_ssi_is_ac97(ssi))
  938. fsl_ssi_tx_ac97_saccst_setup(ssi);
  939. fsl_ssi_config_enable(ssi, tx);
  940. break;
  941. case SNDRV_PCM_TRIGGER_STOP:
  942. case SNDRV_PCM_TRIGGER_SUSPEND:
  943. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  944. fsl_ssi_config_disable(ssi, tx);
  945. break;
  946. default:
  947. return -EINVAL;
  948. }
  949. return 0;
  950. }
  951. static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
  952. {
  953. struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
  954. if (ssi->soc->imx && ssi->use_dma)
  955. snd_soc_dai_init_dma_data(dai, &ssi->dma_params_tx,
  956. &ssi->dma_params_rx);
  957. return 0;
  958. }
  959. static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
  960. .startup = fsl_ssi_startup,
  961. .shutdown = fsl_ssi_shutdown,
  962. .hw_params = fsl_ssi_hw_params,
  963. .hw_free = fsl_ssi_hw_free,
  964. .set_fmt = fsl_ssi_set_dai_fmt,
  965. .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
  966. .trigger = fsl_ssi_trigger,
  967. };
  968. static struct snd_soc_dai_driver fsl_ssi_dai_template = {
  969. .probe = fsl_ssi_dai_probe,
  970. .playback = {
  971. .stream_name = "CPU-Playback",
  972. .channels_min = 1,
  973. .channels_max = 32,
  974. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  975. .formats = FSLSSI_I2S_FORMATS,
  976. },
  977. .capture = {
  978. .stream_name = "CPU-Capture",
  979. .channels_min = 1,
  980. .channels_max = 32,
  981. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  982. .formats = FSLSSI_I2S_FORMATS,
  983. },
  984. .ops = &fsl_ssi_dai_ops,
  985. };
  986. static const struct snd_soc_component_driver fsl_ssi_component = {
  987. .name = "fsl-ssi",
  988. };
  989. static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
  990. .bus_control = true,
  991. .symmetric_channels = 1,
  992. .probe = fsl_ssi_dai_probe,
  993. .playback = {
  994. .stream_name = "AC97 Playback",
  995. .channels_min = 2,
  996. .channels_max = 2,
  997. .rates = SNDRV_PCM_RATE_8000_48000,
  998. .formats = SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S20,
  999. },
  1000. .capture = {
  1001. .stream_name = "AC97 Capture",
  1002. .channels_min = 2,
  1003. .channels_max = 2,
  1004. .rates = SNDRV_PCM_RATE_48000,
  1005. /* 16-bit capture is broken (errata ERR003778) */
  1006. .formats = SNDRV_PCM_FMTBIT_S20,
  1007. },
  1008. .ops = &fsl_ssi_dai_ops,
  1009. };
  1010. static struct fsl_ssi *fsl_ac97_data;
  1011. static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  1012. unsigned short val)
  1013. {
  1014. struct regmap *regs = fsl_ac97_data->regs;
  1015. unsigned int lreg;
  1016. unsigned int lval;
  1017. int ret;
  1018. if (reg > 0x7f)
  1019. return;
  1020. mutex_lock(&fsl_ac97_data->ac97_reg_lock);
  1021. ret = clk_prepare_enable(fsl_ac97_data->clk);
  1022. if (ret) {
  1023. pr_err("ac97 write clk_prepare_enable failed: %d\n",
  1024. ret);
  1025. goto ret_unlock;
  1026. }
  1027. lreg = reg << 12;
  1028. regmap_write(regs, REG_SSI_SACADD, lreg);
  1029. lval = val << 4;
  1030. regmap_write(regs, REG_SSI_SACDAT, lval);
  1031. regmap_update_bits(regs, REG_SSI_SACNT,
  1032. SSI_SACNT_RDWR_MASK, SSI_SACNT_WR);
  1033. udelay(100);
  1034. clk_disable_unprepare(fsl_ac97_data->clk);
  1035. ret_unlock:
  1036. mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
  1037. }
  1038. static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
  1039. unsigned short reg)
  1040. {
  1041. struct regmap *regs = fsl_ac97_data->regs;
  1042. unsigned short val = 0;
  1043. u32 reg_val;
  1044. unsigned int lreg;
  1045. int ret;
  1046. mutex_lock(&fsl_ac97_data->ac97_reg_lock);
  1047. ret = clk_prepare_enable(fsl_ac97_data->clk);
  1048. if (ret) {
  1049. pr_err("ac97 read clk_prepare_enable failed: %d\n", ret);
  1050. goto ret_unlock;
  1051. }
  1052. lreg = (reg & 0x7f) << 12;
  1053. regmap_write(regs, REG_SSI_SACADD, lreg);
  1054. regmap_update_bits(regs, REG_SSI_SACNT,
  1055. SSI_SACNT_RDWR_MASK, SSI_SACNT_RD);
  1056. udelay(100);
  1057. regmap_read(regs, REG_SSI_SACDAT, &reg_val);
  1058. val = (reg_val >> 4) & 0xffff;
  1059. clk_disable_unprepare(fsl_ac97_data->clk);
  1060. ret_unlock:
  1061. mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
  1062. return val;
  1063. }
  1064. static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
  1065. .read = fsl_ssi_ac97_read,
  1066. .write = fsl_ssi_ac97_write,
  1067. };
  1068. /**
  1069. * Initialize SSI registers
  1070. */
  1071. static int fsl_ssi_hw_init(struct fsl_ssi *ssi)
  1072. {
  1073. u32 wm = ssi->fifo_watermark;
  1074. /* Initialize regvals */
  1075. fsl_ssi_setup_regvals(ssi);
  1076. /* Set watermarks */
  1077. regmap_write(ssi->regs, REG_SSI_SFCSR,
  1078. SSI_SFCSR_TFWM0(wm) | SSI_SFCSR_RFWM0(wm) |
  1079. SSI_SFCSR_TFWM1(wm) | SSI_SFCSR_RFWM1(wm));
  1080. /* Enable Dual FIFO mode */
  1081. if (ssi->use_dual_fifo)
  1082. regmap_update_bits(ssi->regs, REG_SSI_SCR,
  1083. SSI_SCR_TCH_EN, SSI_SCR_TCH_EN);
  1084. /* AC97 should start earlier to communicate with CODECs */
  1085. if (fsl_ssi_is_ac97(ssi)) {
  1086. _fsl_ssi_set_dai_fmt(ssi, ssi->dai_fmt);
  1087. fsl_ssi_setup_ac97(ssi);
  1088. }
  1089. return 0;
  1090. }
  1091. /**
  1092. * Clear SSI registers
  1093. */
  1094. static void fsl_ssi_hw_clean(struct fsl_ssi *ssi)
  1095. {
  1096. /* Disable registers for AC97 */
  1097. if (fsl_ssi_is_ac97(ssi)) {
  1098. /* Disable TE and RE bits first */
  1099. regmap_update_bits(ssi->regs, REG_SSI_SCR,
  1100. SSI_SCR_TE | SSI_SCR_RE, 0);
  1101. /* Disable AC97 mode */
  1102. regmap_write(ssi->regs, REG_SSI_SACNT, 0);
  1103. /* Unset WAIT bits */
  1104. regmap_write(ssi->regs, REG_SSI_SOR, 0);
  1105. /* Disable SSI -- software reset */
  1106. regmap_update_bits(ssi->regs, REG_SSI_SCR, SSI_SCR_SSIEN, 0);
  1107. }
  1108. }
  1109. /**
  1110. * Make every character in a string lower-case
  1111. */
  1112. static void make_lowercase(char *s)
  1113. {
  1114. if (!s)
  1115. return;
  1116. for (; *s; s++)
  1117. *s = tolower(*s);
  1118. }
  1119. static int fsl_ssi_imx_probe(struct platform_device *pdev,
  1120. struct fsl_ssi *ssi, void __iomem *iomem)
  1121. {
  1122. struct device *dev = &pdev->dev;
  1123. int ret;
  1124. /* Backward compatible for a DT without ipg clock name assigned */
  1125. if (ssi->has_ipg_clk_name)
  1126. ssi->clk = devm_clk_get(dev, "ipg");
  1127. else
  1128. ssi->clk = devm_clk_get(dev, NULL);
  1129. if (IS_ERR(ssi->clk)) {
  1130. ret = PTR_ERR(ssi->clk);
  1131. dev_err(dev, "failed to get clock: %d\n", ret);
  1132. return ret;
  1133. }
  1134. /* Enable the clock since regmap will not handle it in this case */
  1135. if (!ssi->has_ipg_clk_name) {
  1136. ret = clk_prepare_enable(ssi->clk);
  1137. if (ret) {
  1138. dev_err(dev, "clk_prepare_enable failed: %d\n", ret);
  1139. return ret;
  1140. }
  1141. }
  1142. /* Do not error out for slave cases that live without a baud clock */
  1143. ssi->baudclk = devm_clk_get(dev, "baud");
  1144. if (IS_ERR(ssi->baudclk))
  1145. dev_dbg(dev, "failed to get baud clock: %ld\n",
  1146. PTR_ERR(ssi->baudclk));
  1147. ssi->dma_params_tx.maxburst = ssi->dma_maxburst;
  1148. ssi->dma_params_rx.maxburst = ssi->dma_maxburst;
  1149. ssi->dma_params_tx.addr = ssi->ssi_phys + REG_SSI_STX0;
  1150. ssi->dma_params_rx.addr = ssi->ssi_phys + REG_SSI_SRX0;
  1151. /* Use even numbers to avoid channel swap due to SDMA script design */
  1152. if (ssi->use_dual_fifo) {
  1153. ssi->dma_params_tx.maxburst &= ~0x1;
  1154. ssi->dma_params_rx.maxburst &= ~0x1;
  1155. }
  1156. if (!ssi->use_dma) {
  1157. /*
  1158. * Some boards use an incompatible codec. Use imx-fiq-pcm-audio
  1159. * to get it working, as DMA is not possible in this situation.
  1160. */
  1161. ssi->fiq_params.irq = ssi->irq;
  1162. ssi->fiq_params.base = iomem;
  1163. ssi->fiq_params.dma_params_rx = &ssi->dma_params_rx;
  1164. ssi->fiq_params.dma_params_tx = &ssi->dma_params_tx;
  1165. ret = imx_pcm_fiq_init(pdev, &ssi->fiq_params);
  1166. if (ret)
  1167. goto error_pcm;
  1168. } else {
  1169. ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE);
  1170. if (ret)
  1171. goto error_pcm;
  1172. }
  1173. return 0;
  1174. error_pcm:
  1175. if (!ssi->has_ipg_clk_name)
  1176. clk_disable_unprepare(ssi->clk);
  1177. return ret;
  1178. }
  1179. static void fsl_ssi_imx_clean(struct platform_device *pdev, struct fsl_ssi *ssi)
  1180. {
  1181. if (!ssi->use_dma)
  1182. imx_pcm_fiq_exit(pdev);
  1183. if (!ssi->has_ipg_clk_name)
  1184. clk_disable_unprepare(ssi->clk);
  1185. }
  1186. static int fsl_ssi_probe_from_dt(struct fsl_ssi *ssi)
  1187. {
  1188. struct device *dev = ssi->dev;
  1189. struct device_node *np = dev->of_node;
  1190. const struct of_device_id *of_id;
  1191. const char *p, *sprop;
  1192. const __be32 *iprop;
  1193. u32 dmas[4];
  1194. int ret;
  1195. of_id = of_match_device(fsl_ssi_ids, dev);
  1196. if (!of_id || !of_id->data)
  1197. return -EINVAL;
  1198. ssi->soc = of_id->data;
  1199. ret = of_property_match_string(np, "clock-names", "ipg");
  1200. /* Get error code if not found */
  1201. ssi->has_ipg_clk_name = ret >= 0;
  1202. /* Check if being used in AC97 mode */
  1203. sprop = of_get_property(np, "fsl,mode", NULL);
  1204. if (sprop && !strcmp(sprop, "ac97-slave")) {
  1205. ssi->dai_fmt = FSLSSI_AC97_DAIFMT;
  1206. ret = of_property_read_u32(np, "cell-index", &ssi->card_idx);
  1207. if (ret) {
  1208. dev_err(dev, "failed to get SSI index property\n");
  1209. return -EINVAL;
  1210. }
  1211. strcpy(ssi->card_name, "ac97-codec");
  1212. } else if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
  1213. /*
  1214. * In synchronous mode, STCK and STFS ports are used by RX
  1215. * as well. So the software should limit the sample rates,
  1216. * sample bits and channels to be symmetric.
  1217. *
  1218. * This is exclusive with FSLSSI_AC97_FORMATS as AC97 runs
  1219. * in the SSI synchronous mode however it does not have to
  1220. * limit symmetric sample rates and sample bits.
  1221. */
  1222. ssi->synchronous = true;
  1223. }
  1224. /* Select DMA or FIQ */
  1225. ssi->use_dma = !of_property_read_bool(np, "fsl,fiq-stream-filter");
  1226. /* Fetch FIFO depth; Set to 8 for older DT without this property */
  1227. iprop = of_get_property(np, "fsl,fifo-depth", NULL);
  1228. if (iprop)
  1229. ssi->fifo_depth = be32_to_cpup(iprop);
  1230. else
  1231. ssi->fifo_depth = 8;
  1232. /* Use dual FIFO mode depending on the support from SDMA script */
  1233. ret = of_property_read_u32_array(np, "dmas", dmas, 4);
  1234. if (ssi->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL)
  1235. ssi->use_dual_fifo = true;
  1236. /*
  1237. * Backward compatible for older bindings by manually triggering the
  1238. * machine driver's probe(). Use /compatible property, including the
  1239. * address of CPU DAI driver structure, as the name of machine driver
  1240. *
  1241. * If card_name is set by AC97 earlier, bypass here since it uses a
  1242. * different name to register the device.
  1243. */
  1244. if (!ssi->card_name[0] && of_get_property(np, "codec-handle", NULL)) {
  1245. struct device_node *root = of_find_node_by_path("/");
  1246. sprop = of_get_property(root, "compatible", NULL);
  1247. of_node_put(root);
  1248. /* Strip "fsl," in the compatible name if applicable */
  1249. p = strrchr(sprop, ',');
  1250. if (p)
  1251. sprop = p + 1;
  1252. snprintf(ssi->card_name, sizeof(ssi->card_name),
  1253. "snd-soc-%s", sprop);
  1254. make_lowercase(ssi->card_name);
  1255. ssi->card_idx = 0;
  1256. }
  1257. return 0;
  1258. }
  1259. static int fsl_ssi_probe(struct platform_device *pdev)
  1260. {
  1261. struct regmap_config regconfig = fsl_ssi_regconfig;
  1262. struct device *dev = &pdev->dev;
  1263. struct fsl_ssi *ssi;
  1264. struct resource *res;
  1265. void __iomem *iomem;
  1266. int ret = 0;
  1267. ssi = devm_kzalloc(dev, sizeof(*ssi), GFP_KERNEL);
  1268. if (!ssi)
  1269. return -ENOMEM;
  1270. ssi->dev = dev;
  1271. /* Probe from DT */
  1272. ret = fsl_ssi_probe_from_dt(ssi);
  1273. if (ret)
  1274. return ret;
  1275. if (fsl_ssi_is_ac97(ssi)) {
  1276. memcpy(&ssi->cpu_dai_drv, &fsl_ssi_ac97_dai,
  1277. sizeof(fsl_ssi_ac97_dai));
  1278. fsl_ac97_data = ssi;
  1279. } else {
  1280. memcpy(&ssi->cpu_dai_drv, &fsl_ssi_dai_template,
  1281. sizeof(fsl_ssi_dai_template));
  1282. }
  1283. ssi->cpu_dai_drv.name = dev_name(dev);
  1284. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1285. iomem = devm_ioremap_resource(dev, res);
  1286. if (IS_ERR(iomem))
  1287. return PTR_ERR(iomem);
  1288. ssi->ssi_phys = res->start;
  1289. if (ssi->soc->imx21regs) {
  1290. /* No SACC{ST,EN,DIS} regs in imx21-class SSI */
  1291. regconfig.max_register = REG_SSI_SRMSK;
  1292. regconfig.num_reg_defaults_raw =
  1293. REG_SSI_SRMSK / sizeof(uint32_t) + 1;
  1294. }
  1295. if (ssi->has_ipg_clk_name)
  1296. ssi->regs = devm_regmap_init_mmio_clk(dev, "ipg", iomem,
  1297. &regconfig);
  1298. else
  1299. ssi->regs = devm_regmap_init_mmio(dev, iomem, &regconfig);
  1300. if (IS_ERR(ssi->regs)) {
  1301. dev_err(dev, "failed to init register map\n");
  1302. return PTR_ERR(ssi->regs);
  1303. }
  1304. ssi->irq = platform_get_irq(pdev, 0);
  1305. if (ssi->irq < 0) {
  1306. dev_err(dev, "no irq for node %s\n", pdev->name);
  1307. return ssi->irq;
  1308. }
  1309. /* Set software limitations for synchronous mode except AC97 */
  1310. if (ssi->synchronous && !fsl_ssi_is_ac97(ssi)) {
  1311. ssi->cpu_dai_drv.symmetric_rates = 1;
  1312. ssi->cpu_dai_drv.symmetric_channels = 1;
  1313. ssi->cpu_dai_drv.symmetric_samplebits = 1;
  1314. }
  1315. /*
  1316. * Configure TX and RX DMA watermarks -- when to send a DMA request
  1317. *
  1318. * Values should be tested to avoid FIFO under/over run. Set maxburst
  1319. * to fifo_watermark to maxiumize DMA transaction to reduce overhead.
  1320. */
  1321. switch (ssi->fifo_depth) {
  1322. case 15:
  1323. /*
  1324. * Set to 8 as a balanced configuration -- When TX FIFO has 8
  1325. * empty slots, send a DMA request to fill these 8 slots. The
  1326. * remaining 7 slots should be able to allow DMA to finish the
  1327. * transaction before TX FIFO underruns; Same applies to RX.
  1328. *
  1329. * Tested with cases running at 48kHz @ 16 bits x 16 channels
  1330. */
  1331. ssi->fifo_watermark = 8;
  1332. ssi->dma_maxburst = 8;
  1333. break;
  1334. case 8:
  1335. default:
  1336. /* Safely use old watermark configurations for older chips */
  1337. ssi->fifo_watermark = ssi->fifo_depth - 2;
  1338. ssi->dma_maxburst = ssi->fifo_depth - 2;
  1339. break;
  1340. }
  1341. dev_set_drvdata(dev, ssi);
  1342. if (ssi->soc->imx) {
  1343. ret = fsl_ssi_imx_probe(pdev, ssi, iomem);
  1344. if (ret)
  1345. return ret;
  1346. }
  1347. if (fsl_ssi_is_ac97(ssi)) {
  1348. mutex_init(&ssi->ac97_reg_lock);
  1349. ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
  1350. if (ret) {
  1351. dev_err(dev, "failed to set AC'97 ops\n");
  1352. goto error_ac97_ops;
  1353. }
  1354. }
  1355. ret = devm_snd_soc_register_component(dev, &fsl_ssi_component,
  1356. &ssi->cpu_dai_drv, 1);
  1357. if (ret) {
  1358. dev_err(dev, "failed to register DAI: %d\n", ret);
  1359. goto error_asoc_register;
  1360. }
  1361. if (ssi->use_dma) {
  1362. ret = devm_request_irq(dev, ssi->irq, fsl_ssi_isr, 0,
  1363. dev_name(dev), ssi);
  1364. if (ret < 0) {
  1365. dev_err(dev, "failed to claim irq %u\n", ssi->irq);
  1366. goto error_asoc_register;
  1367. }
  1368. }
  1369. ret = fsl_ssi_debugfs_create(&ssi->dbg_stats, dev);
  1370. if (ret)
  1371. goto error_asoc_register;
  1372. /* Initially configures SSI registers */
  1373. fsl_ssi_hw_init(ssi);
  1374. /* Register a platform device for older bindings or AC97 */
  1375. if (ssi->card_name[0]) {
  1376. struct device *parent = dev;
  1377. /*
  1378. * Do not set SSI dev as the parent of AC97 CODEC device since
  1379. * it does not have a DT node. Otherwise ASoC core will assume
  1380. * CODEC has the same DT node as the SSI, so it may bypass the
  1381. * dai_probe() of SSI and then cause NULL DMA data pointers.
  1382. */
  1383. if (fsl_ssi_is_ac97(ssi))
  1384. parent = NULL;
  1385. ssi->card_pdev = platform_device_register_data(parent,
  1386. ssi->card_name, ssi->card_idx, NULL, 0);
  1387. if (IS_ERR(ssi->card_pdev)) {
  1388. ret = PTR_ERR(ssi->card_pdev);
  1389. dev_err(dev, "failed to register %s: %d\n",
  1390. ssi->card_name, ret);
  1391. goto error_sound_card;
  1392. }
  1393. }
  1394. return 0;
  1395. error_sound_card:
  1396. fsl_ssi_debugfs_remove(&ssi->dbg_stats);
  1397. error_asoc_register:
  1398. if (fsl_ssi_is_ac97(ssi))
  1399. snd_soc_set_ac97_ops(NULL);
  1400. error_ac97_ops:
  1401. if (fsl_ssi_is_ac97(ssi))
  1402. mutex_destroy(&ssi->ac97_reg_lock);
  1403. if (ssi->soc->imx)
  1404. fsl_ssi_imx_clean(pdev, ssi);
  1405. return ret;
  1406. }
  1407. static int fsl_ssi_remove(struct platform_device *pdev)
  1408. {
  1409. struct fsl_ssi *ssi = dev_get_drvdata(&pdev->dev);
  1410. fsl_ssi_debugfs_remove(&ssi->dbg_stats);
  1411. if (ssi->card_pdev)
  1412. platform_device_unregister(ssi->card_pdev);
  1413. /* Clean up SSI registers */
  1414. fsl_ssi_hw_clean(ssi);
  1415. if (ssi->soc->imx)
  1416. fsl_ssi_imx_clean(pdev, ssi);
  1417. if (fsl_ssi_is_ac97(ssi)) {
  1418. snd_soc_set_ac97_ops(NULL);
  1419. mutex_destroy(&ssi->ac97_reg_lock);
  1420. }
  1421. return 0;
  1422. }
  1423. #ifdef CONFIG_PM_SLEEP
  1424. static int fsl_ssi_suspend(struct device *dev)
  1425. {
  1426. struct fsl_ssi *ssi = dev_get_drvdata(dev);
  1427. struct regmap *regs = ssi->regs;
  1428. regmap_read(regs, REG_SSI_SFCSR, &ssi->regcache_sfcsr);
  1429. regmap_read(regs, REG_SSI_SACNT, &ssi->regcache_sacnt);
  1430. regcache_cache_only(regs, true);
  1431. regcache_mark_dirty(regs);
  1432. return 0;
  1433. }
  1434. static int fsl_ssi_resume(struct device *dev)
  1435. {
  1436. struct fsl_ssi *ssi = dev_get_drvdata(dev);
  1437. struct regmap *regs = ssi->regs;
  1438. regcache_cache_only(regs, false);
  1439. regmap_update_bits(regs, REG_SSI_SFCSR,
  1440. SSI_SFCSR_RFWM1_MASK | SSI_SFCSR_TFWM1_MASK |
  1441. SSI_SFCSR_RFWM0_MASK | SSI_SFCSR_TFWM0_MASK,
  1442. ssi->regcache_sfcsr);
  1443. regmap_write(regs, REG_SSI_SACNT, ssi->regcache_sacnt);
  1444. return regcache_sync(regs);
  1445. }
  1446. #endif /* CONFIG_PM_SLEEP */
  1447. static const struct dev_pm_ops fsl_ssi_pm = {
  1448. SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume)
  1449. };
  1450. static struct platform_driver fsl_ssi_driver = {
  1451. .driver = {
  1452. .name = "fsl-ssi-dai",
  1453. .of_match_table = fsl_ssi_ids,
  1454. .pm = &fsl_ssi_pm,
  1455. },
  1456. .probe = fsl_ssi_probe,
  1457. .remove = fsl_ssi_remove,
  1458. };
  1459. module_platform_driver(fsl_ssi_driver);
  1460. MODULE_ALIAS("platform:fsl-ssi-dai");
  1461. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  1462. MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
  1463. MODULE_LICENSE("GPL v2");