fsl_ssi.c 49 KB

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