fsl_ssi.c 48 KB

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