fsl_esai.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * Freescale ESAI ALSA SoC Digital Audio Interface (DAI) driver
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/module.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_platform.h>
  15. #include <sound/dmaengine_pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include "fsl_esai.h"
  18. #include "imx-pcm.h"
  19. #define FSL_ESAI_RATES SNDRV_PCM_RATE_8000_192000
  20. #define FSL_ESAI_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  21. SNDRV_PCM_FMTBIT_S16_LE | \
  22. SNDRV_PCM_FMTBIT_S20_3LE | \
  23. SNDRV_PCM_FMTBIT_S24_LE)
  24. /**
  25. * fsl_esai: ESAI private data
  26. *
  27. * @dma_params_rx: DMA parameters for receive channel
  28. * @dma_params_tx: DMA parameters for transmit channel
  29. * @pdev: platform device pointer
  30. * @regmap: regmap handler
  31. * @coreclk: clock source to access register
  32. * @extalclk: esai clock source to derive HCK, SCK and FS
  33. * @fsysclk: system clock source to derive HCK, SCK and FS
  34. * @spbaclk: SPBA clock (optional, depending on SoC design)
  35. * @fifo_depth: depth of tx/rx FIFO
  36. * @slot_width: width of each DAI slot
  37. * @slots: number of slots
  38. * @hck_rate: clock rate of desired HCKx clock
  39. * @sck_rate: clock rate of desired SCKx clock
  40. * @hck_dir: the direction of HCKx pads
  41. * @sck_div: if using PSR/PM dividers for SCKx clock
  42. * @slave_mode: if fully using DAI slave mode
  43. * @synchronous: if using tx/rx synchronous mode
  44. * @name: driver name
  45. */
  46. struct fsl_esai {
  47. struct snd_dmaengine_dai_dma_data dma_params_rx;
  48. struct snd_dmaengine_dai_dma_data dma_params_tx;
  49. struct platform_device *pdev;
  50. struct regmap *regmap;
  51. struct clk *coreclk;
  52. struct clk *extalclk;
  53. struct clk *fsysclk;
  54. struct clk *spbaclk;
  55. u32 fifo_depth;
  56. u32 slot_width;
  57. u32 slots;
  58. u32 hck_rate[2];
  59. u32 sck_rate[2];
  60. bool hck_dir[2];
  61. bool sck_div[2];
  62. bool slave_mode;
  63. bool synchronous;
  64. char name[32];
  65. };
  66. static irqreturn_t esai_isr(int irq, void *devid)
  67. {
  68. struct fsl_esai *esai_priv = (struct fsl_esai *)devid;
  69. struct platform_device *pdev = esai_priv->pdev;
  70. u32 esr;
  71. regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr);
  72. if (esr & ESAI_ESR_TINIT_MASK)
  73. dev_dbg(&pdev->dev, "isr: Transmission Initialized\n");
  74. if (esr & ESAI_ESR_RFF_MASK)
  75. dev_warn(&pdev->dev, "isr: Receiving overrun\n");
  76. if (esr & ESAI_ESR_TFE_MASK)
  77. dev_warn(&pdev->dev, "isr: Transmission underrun\n");
  78. if (esr & ESAI_ESR_TLS_MASK)
  79. dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n");
  80. if (esr & ESAI_ESR_TDE_MASK)
  81. dev_dbg(&pdev->dev, "isr: Transmission data exception\n");
  82. if (esr & ESAI_ESR_TED_MASK)
  83. dev_dbg(&pdev->dev, "isr: Transmitting even slots\n");
  84. if (esr & ESAI_ESR_TD_MASK)
  85. dev_dbg(&pdev->dev, "isr: Transmitting data\n");
  86. if (esr & ESAI_ESR_RLS_MASK)
  87. dev_dbg(&pdev->dev, "isr: Just received the last slot\n");
  88. if (esr & ESAI_ESR_RDE_MASK)
  89. dev_dbg(&pdev->dev, "isr: Receiving data exception\n");
  90. if (esr & ESAI_ESR_RED_MASK)
  91. dev_dbg(&pdev->dev, "isr: Receiving even slots\n");
  92. if (esr & ESAI_ESR_RD_MASK)
  93. dev_dbg(&pdev->dev, "isr: Receiving data\n");
  94. return IRQ_HANDLED;
  95. }
  96. /**
  97. * This function is used to calculate the divisors of psr, pm, fp and it is
  98. * supposed to be called in set_dai_sysclk() and set_bclk().
  99. *
  100. * @ratio: desired overall ratio for the paticipating dividers
  101. * @usefp: for HCK setting, there is no need to set fp divider
  102. * @fp: bypass other dividers by setting fp directly if fp != 0
  103. * @tx: current setting is for playback or capture
  104. */
  105. static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio,
  106. bool usefp, u32 fp)
  107. {
  108. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  109. u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j;
  110. maxfp = usefp ? 16 : 1;
  111. if (usefp && fp)
  112. goto out_fp;
  113. if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) {
  114. dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n",
  115. 2 * 8 * 256 * maxfp);
  116. return -EINVAL;
  117. } else if (ratio % 2) {
  118. dev_err(dai->dev, "the raio must be even if using upper divider\n");
  119. return -EINVAL;
  120. }
  121. ratio /= 2;
  122. psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8;
  123. /* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */
  124. if (ratio <= 256) {
  125. pm = ratio;
  126. fp = 1;
  127. goto out;
  128. }
  129. /* Set the max fluctuation -- 0.1% of the max devisor */
  130. savesub = (psr ? 1 : 8) * 256 * maxfp / 1000;
  131. /* Find the best value for PM */
  132. for (i = 1; i <= 256; i++) {
  133. for (j = 1; j <= maxfp; j++) {
  134. /* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */
  135. prod = (psr ? 1 : 8) * i * j;
  136. if (prod == ratio)
  137. sub = 0;
  138. else if (prod / ratio == 1)
  139. sub = prod - ratio;
  140. else if (ratio / prod == 1)
  141. sub = ratio - prod;
  142. else
  143. continue;
  144. /* Calculate the fraction */
  145. sub = sub * 1000 / ratio;
  146. if (sub < savesub) {
  147. savesub = sub;
  148. pm = i;
  149. fp = j;
  150. }
  151. /* We are lucky */
  152. if (savesub == 0)
  153. goto out;
  154. }
  155. }
  156. if (pm == 999) {
  157. dev_err(dai->dev, "failed to calculate proper divisors\n");
  158. return -EINVAL;
  159. }
  160. out:
  161. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
  162. ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK,
  163. psr | ESAI_xCCR_xPM(pm));
  164. out_fp:
  165. /* Bypass fp if not being required */
  166. if (maxfp <= 1)
  167. return 0;
  168. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
  169. ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp));
  170. return 0;
  171. }
  172. /**
  173. * This function mainly configures the clock frequency of MCLK (HCKT/HCKR)
  174. *
  175. * @Parameters:
  176. * clk_id: The clock source of HCKT/HCKR
  177. * (Input from outside; output from inside, FSYS or EXTAL)
  178. * freq: The required clock rate of HCKT/HCKR
  179. * dir: The clock direction of HCKT/HCKR
  180. *
  181. * Note: If the direction is input, we do not care about clk_id.
  182. */
  183. static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
  184. unsigned int freq, int dir)
  185. {
  186. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  187. struct clk *clksrc = esai_priv->extalclk;
  188. bool tx = clk_id <= ESAI_HCKT_EXTAL;
  189. bool in = dir == SND_SOC_CLOCK_IN;
  190. u32 ratio, ecr = 0;
  191. unsigned long clk_rate;
  192. int ret;
  193. /* Bypass divider settings if the requirement doesn't change */
  194. if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx])
  195. return 0;
  196. /* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */
  197. esai_priv->sck_div[tx] = true;
  198. /* Set the direction of HCKT/HCKR pins */
  199. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
  200. ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD);
  201. if (in)
  202. goto out;
  203. switch (clk_id) {
  204. case ESAI_HCKT_FSYS:
  205. case ESAI_HCKR_FSYS:
  206. clksrc = esai_priv->fsysclk;
  207. break;
  208. case ESAI_HCKT_EXTAL:
  209. ecr |= ESAI_ECR_ETI;
  210. case ESAI_HCKR_EXTAL:
  211. ecr |= ESAI_ECR_ERI;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. if (IS_ERR(clksrc)) {
  217. dev_err(dai->dev, "no assigned %s clock\n",
  218. clk_id % 2 ? "extal" : "fsys");
  219. return PTR_ERR(clksrc);
  220. }
  221. clk_rate = clk_get_rate(clksrc);
  222. ratio = clk_rate / freq;
  223. if (ratio * freq > clk_rate)
  224. ret = ratio * freq - clk_rate;
  225. else if (ratio * freq < clk_rate)
  226. ret = clk_rate - ratio * freq;
  227. else
  228. ret = 0;
  229. /* Block if clock source can not be divided into the required rate */
  230. if (ret != 0 && clk_rate / ret < 1000) {
  231. dev_err(dai->dev, "failed to derive required HCK%c rate\n",
  232. tx ? 'T' : 'R');
  233. return -EINVAL;
  234. }
  235. /* Only EXTAL source can be output directly without using PSR and PM */
  236. if (ratio == 1 && clksrc == esai_priv->extalclk) {
  237. /* Bypass all the dividers if not being needed */
  238. ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO;
  239. goto out;
  240. } else if (ratio < 2) {
  241. /* The ratio should be no less than 2 if using other sources */
  242. dev_err(dai->dev, "failed to derive required HCK%c rate\n",
  243. tx ? 'T' : 'R');
  244. return -EINVAL;
  245. }
  246. ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0);
  247. if (ret)
  248. return ret;
  249. esai_priv->sck_div[tx] = false;
  250. out:
  251. esai_priv->hck_dir[tx] = dir;
  252. esai_priv->hck_rate[tx] = freq;
  253. regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR,
  254. tx ? ESAI_ECR_ETI | ESAI_ECR_ETO :
  255. ESAI_ECR_ERI | ESAI_ECR_ERO, ecr);
  256. return 0;
  257. }
  258. /**
  259. * This function configures the related dividers according to the bclk rate
  260. */
  261. static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
  262. {
  263. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  264. u32 hck_rate = esai_priv->hck_rate[tx];
  265. u32 sub, ratio = hck_rate / freq;
  266. int ret;
  267. /* Don't apply for fully slave mode or unchanged bclk */
  268. if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq)
  269. return 0;
  270. if (ratio * freq > hck_rate)
  271. sub = ratio * freq - hck_rate;
  272. else if (ratio * freq < hck_rate)
  273. sub = hck_rate - ratio * freq;
  274. else
  275. sub = 0;
  276. /* Block if clock source can not be divided into the required rate */
  277. if (sub != 0 && hck_rate / sub < 1000) {
  278. dev_err(dai->dev, "failed to derive required SCK%c rate\n",
  279. tx ? 'T' : 'R');
  280. return -EINVAL;
  281. }
  282. /* The ratio should be contented by FP alone if bypassing PM and PSR */
  283. if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) {
  284. dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n");
  285. return -EINVAL;
  286. }
  287. ret = fsl_esai_divisor_cal(dai, tx, ratio, true,
  288. esai_priv->sck_div[tx] ? 0 : ratio);
  289. if (ret)
  290. return ret;
  291. /* Save current bclk rate */
  292. esai_priv->sck_rate[tx] = freq;
  293. return 0;
  294. }
  295. static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask,
  296. u32 rx_mask, int slots, int slot_width)
  297. {
  298. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  299. regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
  300. ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
  301. regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMA,
  302. ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(tx_mask));
  303. regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMB,
  304. ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(tx_mask));
  305. regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
  306. ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
  307. regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMA,
  308. ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(rx_mask));
  309. regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMB,
  310. ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(rx_mask));
  311. esai_priv->slot_width = slot_width;
  312. esai_priv->slots = slots;
  313. return 0;
  314. }
  315. static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  316. {
  317. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  318. u32 xcr = 0, xccr = 0, mask;
  319. /* DAI mode */
  320. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  321. case SND_SOC_DAIFMT_I2S:
  322. /* Data on rising edge of bclk, frame low, 1clk before data */
  323. xcr |= ESAI_xCR_xFSR;
  324. xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  325. break;
  326. case SND_SOC_DAIFMT_LEFT_J:
  327. /* Data on rising edge of bclk, frame high */
  328. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  329. break;
  330. case SND_SOC_DAIFMT_RIGHT_J:
  331. /* Data on rising edge of bclk, frame high, right aligned */
  332. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCR_xWA;
  333. break;
  334. case SND_SOC_DAIFMT_DSP_A:
  335. /* Data on rising edge of bclk, frame high, 1clk before data */
  336. xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR;
  337. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  338. break;
  339. case SND_SOC_DAIFMT_DSP_B:
  340. /* Data on rising edge of bclk, frame high */
  341. xcr |= ESAI_xCR_xFSL;
  342. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  343. break;
  344. default:
  345. return -EINVAL;
  346. }
  347. /* DAI clock inversion */
  348. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  349. case SND_SOC_DAIFMT_NB_NF:
  350. /* Nothing to do for both normal cases */
  351. break;
  352. case SND_SOC_DAIFMT_IB_NF:
  353. /* Invert bit clock */
  354. xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  355. break;
  356. case SND_SOC_DAIFMT_NB_IF:
  357. /* Invert frame clock */
  358. xccr ^= ESAI_xCCR_xFSP;
  359. break;
  360. case SND_SOC_DAIFMT_IB_IF:
  361. /* Invert both clocks */
  362. xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP;
  363. break;
  364. default:
  365. return -EINVAL;
  366. }
  367. esai_priv->slave_mode = false;
  368. /* DAI clock master masks */
  369. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  370. case SND_SOC_DAIFMT_CBM_CFM:
  371. esai_priv->slave_mode = true;
  372. break;
  373. case SND_SOC_DAIFMT_CBS_CFM:
  374. xccr |= ESAI_xCCR_xCKD;
  375. break;
  376. case SND_SOC_DAIFMT_CBM_CFS:
  377. xccr |= ESAI_xCCR_xFSD;
  378. break;
  379. case SND_SOC_DAIFMT_CBS_CFS:
  380. xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
  381. break;
  382. default:
  383. return -EINVAL;
  384. }
  385. mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR;
  386. regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr);
  387. regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr);
  388. mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP |
  389. ESAI_xCCR_xFSD | ESAI_xCCR_xCKD | ESAI_xCR_xWA;
  390. regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr);
  391. regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr);
  392. return 0;
  393. }
  394. static int fsl_esai_startup(struct snd_pcm_substream *substream,
  395. struct snd_soc_dai *dai)
  396. {
  397. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  398. int ret;
  399. /*
  400. * Some platforms might use the same bit to gate all three or two of
  401. * clocks, so keep all clocks open/close at the same time for safety
  402. */
  403. ret = clk_prepare_enable(esai_priv->coreclk);
  404. if (ret)
  405. return ret;
  406. if (!IS_ERR(esai_priv->spbaclk)) {
  407. ret = clk_prepare_enable(esai_priv->spbaclk);
  408. if (ret)
  409. goto err_spbaclk;
  410. }
  411. if (!IS_ERR(esai_priv->extalclk)) {
  412. ret = clk_prepare_enable(esai_priv->extalclk);
  413. if (ret)
  414. goto err_extalck;
  415. }
  416. if (!IS_ERR(esai_priv->fsysclk)) {
  417. ret = clk_prepare_enable(esai_priv->fsysclk);
  418. if (ret)
  419. goto err_fsysclk;
  420. }
  421. if (!dai->active) {
  422. /* Set synchronous mode */
  423. regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR,
  424. ESAI_SAICR_SYNC, esai_priv->synchronous ?
  425. ESAI_SAICR_SYNC : 0);
  426. /* Set a default slot number -- 2 */
  427. regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
  428. ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
  429. regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
  430. ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
  431. }
  432. return 0;
  433. err_fsysclk:
  434. if (!IS_ERR(esai_priv->extalclk))
  435. clk_disable_unprepare(esai_priv->extalclk);
  436. err_extalck:
  437. if (!IS_ERR(esai_priv->spbaclk))
  438. clk_disable_unprepare(esai_priv->spbaclk);
  439. err_spbaclk:
  440. clk_disable_unprepare(esai_priv->coreclk);
  441. return ret;
  442. }
  443. static int fsl_esai_hw_params(struct snd_pcm_substream *substream,
  444. struct snd_pcm_hw_params *params,
  445. struct snd_soc_dai *dai)
  446. {
  447. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  448. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  449. u32 width = params_width(params);
  450. u32 channels = params_channels(params);
  451. u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
  452. u32 slot_width = width;
  453. u32 bclk, mask, val;
  454. int ret;
  455. /* Override slot_width if being specifically set */
  456. if (esai_priv->slot_width)
  457. slot_width = esai_priv->slot_width;
  458. bclk = params_rate(params) * slot_width * esai_priv->slots;
  459. ret = fsl_esai_set_bclk(dai, tx, bclk);
  460. if (ret)
  461. return ret;
  462. /* Use Normal mode to support monaural audio */
  463. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
  464. ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ?
  465. ESAI_xCR_xMOD_NETWORK : 0);
  466. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  467. ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR);
  468. mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK |
  469. (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK);
  470. val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) |
  471. (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins));
  472. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val);
  473. mask = ESAI_xCR_xSWS_MASK | (tx ? ESAI_xCR_PADC : 0);
  474. val = ESAI_xCR_xSWS(slot_width, width) | (tx ? ESAI_xCR_PADC : 0);
  475. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val);
  476. /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */
  477. regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
  478. ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO));
  479. regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
  480. ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO));
  481. return 0;
  482. }
  483. static void fsl_esai_shutdown(struct snd_pcm_substream *substream,
  484. struct snd_soc_dai *dai)
  485. {
  486. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  487. if (!IS_ERR(esai_priv->fsysclk))
  488. clk_disable_unprepare(esai_priv->fsysclk);
  489. if (!IS_ERR(esai_priv->extalclk))
  490. clk_disable_unprepare(esai_priv->extalclk);
  491. if (!IS_ERR(esai_priv->spbaclk))
  492. clk_disable_unprepare(esai_priv->spbaclk);
  493. clk_disable_unprepare(esai_priv->coreclk);
  494. }
  495. static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd,
  496. struct snd_soc_dai *dai)
  497. {
  498. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  499. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  500. u8 i, channels = substream->runtime->channels;
  501. u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
  502. switch (cmd) {
  503. case SNDRV_PCM_TRIGGER_START:
  504. case SNDRV_PCM_TRIGGER_RESUME:
  505. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  506. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  507. ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN);
  508. /* Write initial words reqiured by ESAI as normal procedure */
  509. for (i = 0; tx && i < channels; i++)
  510. regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0);
  511. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
  512. tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK,
  513. tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins));
  514. break;
  515. case SNDRV_PCM_TRIGGER_SUSPEND:
  516. case SNDRV_PCM_TRIGGER_STOP:
  517. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  518. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
  519. tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0);
  520. /* Disable and reset FIFO */
  521. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  522. ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR);
  523. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  524. ESAI_xFCR_xFR, 0);
  525. break;
  526. default:
  527. return -EINVAL;
  528. }
  529. return 0;
  530. }
  531. static struct snd_soc_dai_ops fsl_esai_dai_ops = {
  532. .startup = fsl_esai_startup,
  533. .shutdown = fsl_esai_shutdown,
  534. .trigger = fsl_esai_trigger,
  535. .hw_params = fsl_esai_hw_params,
  536. .set_sysclk = fsl_esai_set_dai_sysclk,
  537. .set_fmt = fsl_esai_set_dai_fmt,
  538. .set_tdm_slot = fsl_esai_set_dai_tdm_slot,
  539. };
  540. static int fsl_esai_dai_probe(struct snd_soc_dai *dai)
  541. {
  542. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  543. snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx,
  544. &esai_priv->dma_params_rx);
  545. return 0;
  546. }
  547. static struct snd_soc_dai_driver fsl_esai_dai = {
  548. .probe = fsl_esai_dai_probe,
  549. .playback = {
  550. .stream_name = "CPU-Playback",
  551. .channels_min = 1,
  552. .channels_max = 12,
  553. .rates = FSL_ESAI_RATES,
  554. .formats = FSL_ESAI_FORMATS,
  555. },
  556. .capture = {
  557. .stream_name = "CPU-Capture",
  558. .channels_min = 1,
  559. .channels_max = 8,
  560. .rates = FSL_ESAI_RATES,
  561. .formats = FSL_ESAI_FORMATS,
  562. },
  563. .ops = &fsl_esai_dai_ops,
  564. };
  565. static const struct snd_soc_component_driver fsl_esai_component = {
  566. .name = "fsl-esai",
  567. };
  568. static const struct reg_default fsl_esai_reg_defaults[] = {
  569. {REG_ESAI_ETDR, 0x00000000},
  570. {REG_ESAI_ECR, 0x00000000},
  571. {REG_ESAI_TFCR, 0x00000000},
  572. {REG_ESAI_RFCR, 0x00000000},
  573. {REG_ESAI_TX0, 0x00000000},
  574. {REG_ESAI_TX1, 0x00000000},
  575. {REG_ESAI_TX2, 0x00000000},
  576. {REG_ESAI_TX3, 0x00000000},
  577. {REG_ESAI_TX4, 0x00000000},
  578. {REG_ESAI_TX5, 0x00000000},
  579. {REG_ESAI_TSR, 0x00000000},
  580. {REG_ESAI_SAICR, 0x00000000},
  581. {REG_ESAI_TCR, 0x00000000},
  582. {REG_ESAI_TCCR, 0x00000000},
  583. {REG_ESAI_RCR, 0x00000000},
  584. {REG_ESAI_RCCR, 0x00000000},
  585. {REG_ESAI_TSMA, 0x0000ffff},
  586. {REG_ESAI_TSMB, 0x0000ffff},
  587. {REG_ESAI_RSMA, 0x0000ffff},
  588. {REG_ESAI_RSMB, 0x0000ffff},
  589. {REG_ESAI_PRRC, 0x00000000},
  590. {REG_ESAI_PCRC, 0x00000000},
  591. };
  592. static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg)
  593. {
  594. switch (reg) {
  595. case REG_ESAI_ERDR:
  596. case REG_ESAI_ECR:
  597. case REG_ESAI_ESR:
  598. case REG_ESAI_TFCR:
  599. case REG_ESAI_TFSR:
  600. case REG_ESAI_RFCR:
  601. case REG_ESAI_RFSR:
  602. case REG_ESAI_RX0:
  603. case REG_ESAI_RX1:
  604. case REG_ESAI_RX2:
  605. case REG_ESAI_RX3:
  606. case REG_ESAI_SAISR:
  607. case REG_ESAI_SAICR:
  608. case REG_ESAI_TCR:
  609. case REG_ESAI_TCCR:
  610. case REG_ESAI_RCR:
  611. case REG_ESAI_RCCR:
  612. case REG_ESAI_TSMA:
  613. case REG_ESAI_TSMB:
  614. case REG_ESAI_RSMA:
  615. case REG_ESAI_RSMB:
  616. case REG_ESAI_PRRC:
  617. case REG_ESAI_PCRC:
  618. return true;
  619. default:
  620. return false;
  621. }
  622. }
  623. static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg)
  624. {
  625. switch (reg) {
  626. case REG_ESAI_ERDR:
  627. case REG_ESAI_ESR:
  628. case REG_ESAI_TFSR:
  629. case REG_ESAI_RFSR:
  630. case REG_ESAI_RX0:
  631. case REG_ESAI_RX1:
  632. case REG_ESAI_RX2:
  633. case REG_ESAI_RX3:
  634. case REG_ESAI_SAISR:
  635. return true;
  636. default:
  637. return false;
  638. }
  639. }
  640. static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg)
  641. {
  642. switch (reg) {
  643. case REG_ESAI_ETDR:
  644. case REG_ESAI_ECR:
  645. case REG_ESAI_TFCR:
  646. case REG_ESAI_RFCR:
  647. case REG_ESAI_TX0:
  648. case REG_ESAI_TX1:
  649. case REG_ESAI_TX2:
  650. case REG_ESAI_TX3:
  651. case REG_ESAI_TX4:
  652. case REG_ESAI_TX5:
  653. case REG_ESAI_TSR:
  654. case REG_ESAI_SAICR:
  655. case REG_ESAI_TCR:
  656. case REG_ESAI_TCCR:
  657. case REG_ESAI_RCR:
  658. case REG_ESAI_RCCR:
  659. case REG_ESAI_TSMA:
  660. case REG_ESAI_TSMB:
  661. case REG_ESAI_RSMA:
  662. case REG_ESAI_RSMB:
  663. case REG_ESAI_PRRC:
  664. case REG_ESAI_PCRC:
  665. return true;
  666. default:
  667. return false;
  668. }
  669. }
  670. static const struct regmap_config fsl_esai_regmap_config = {
  671. .reg_bits = 32,
  672. .reg_stride = 4,
  673. .val_bits = 32,
  674. .max_register = REG_ESAI_PCRC,
  675. .reg_defaults = fsl_esai_reg_defaults,
  676. .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults),
  677. .readable_reg = fsl_esai_readable_reg,
  678. .volatile_reg = fsl_esai_volatile_reg,
  679. .writeable_reg = fsl_esai_writeable_reg,
  680. .cache_type = REGCACHE_FLAT,
  681. };
  682. static int fsl_esai_probe(struct platform_device *pdev)
  683. {
  684. struct device_node *np = pdev->dev.of_node;
  685. struct fsl_esai *esai_priv;
  686. struct resource *res;
  687. const uint32_t *iprop;
  688. void __iomem *regs;
  689. int irq, ret;
  690. esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL);
  691. if (!esai_priv)
  692. return -ENOMEM;
  693. esai_priv->pdev = pdev;
  694. strncpy(esai_priv->name, np->name, sizeof(esai_priv->name) - 1);
  695. /* Get the addresses and IRQ */
  696. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  697. regs = devm_ioremap_resource(&pdev->dev, res);
  698. if (IS_ERR(regs))
  699. return PTR_ERR(regs);
  700. esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  701. "core", regs, &fsl_esai_regmap_config);
  702. if (IS_ERR(esai_priv->regmap)) {
  703. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  704. PTR_ERR(esai_priv->regmap));
  705. return PTR_ERR(esai_priv->regmap);
  706. }
  707. esai_priv->coreclk = devm_clk_get(&pdev->dev, "core");
  708. if (IS_ERR(esai_priv->coreclk)) {
  709. dev_err(&pdev->dev, "failed to get core clock: %ld\n",
  710. PTR_ERR(esai_priv->coreclk));
  711. return PTR_ERR(esai_priv->coreclk);
  712. }
  713. esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal");
  714. if (IS_ERR(esai_priv->extalclk))
  715. dev_warn(&pdev->dev, "failed to get extal clock: %ld\n",
  716. PTR_ERR(esai_priv->extalclk));
  717. esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys");
  718. if (IS_ERR(esai_priv->fsysclk))
  719. dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n",
  720. PTR_ERR(esai_priv->fsysclk));
  721. esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
  722. if (IS_ERR(esai_priv->spbaclk))
  723. dev_warn(&pdev->dev, "failed to get spba clock: %ld\n",
  724. PTR_ERR(esai_priv->spbaclk));
  725. irq = platform_get_irq(pdev, 0);
  726. if (irq < 0) {
  727. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  728. return irq;
  729. }
  730. ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0,
  731. esai_priv->name, esai_priv);
  732. if (ret) {
  733. dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
  734. return ret;
  735. }
  736. /* Set a default slot number */
  737. esai_priv->slots = 2;
  738. /* Set a default master/slave state */
  739. esai_priv->slave_mode = true;
  740. /* Determine the FIFO depth */
  741. iprop = of_get_property(np, "fsl,fifo-depth", NULL);
  742. if (iprop)
  743. esai_priv->fifo_depth = be32_to_cpup(iprop);
  744. else
  745. esai_priv->fifo_depth = 64;
  746. esai_priv->dma_params_tx.maxburst = 16;
  747. esai_priv->dma_params_rx.maxburst = 16;
  748. esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR;
  749. esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR;
  750. esai_priv->synchronous =
  751. of_property_read_bool(np, "fsl,esai-synchronous");
  752. /* Implement full symmetry for synchronous mode */
  753. if (esai_priv->synchronous) {
  754. fsl_esai_dai.symmetric_rates = 1;
  755. fsl_esai_dai.symmetric_channels = 1;
  756. fsl_esai_dai.symmetric_samplebits = 1;
  757. }
  758. dev_set_drvdata(&pdev->dev, esai_priv);
  759. /* Reset ESAI unit */
  760. ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ERST);
  761. if (ret) {
  762. dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret);
  763. return ret;
  764. }
  765. /*
  766. * We need to enable ESAI so as to access some of its registers.
  767. * Otherwise, we would fail to dump regmap from user space.
  768. */
  769. ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ESAIEN);
  770. if (ret) {
  771. dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret);
  772. return ret;
  773. }
  774. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component,
  775. &fsl_esai_dai, 1);
  776. if (ret) {
  777. dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
  778. return ret;
  779. }
  780. ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE);
  781. if (ret)
  782. dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret);
  783. return ret;
  784. }
  785. static const struct of_device_id fsl_esai_dt_ids[] = {
  786. { .compatible = "fsl,imx35-esai", },
  787. { .compatible = "fsl,vf610-esai", },
  788. {}
  789. };
  790. MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids);
  791. #ifdef CONFIG_PM_SLEEP
  792. static int fsl_esai_suspend(struct device *dev)
  793. {
  794. struct fsl_esai *esai = dev_get_drvdata(dev);
  795. regcache_cache_only(esai->regmap, true);
  796. regcache_mark_dirty(esai->regmap);
  797. return 0;
  798. }
  799. static int fsl_esai_resume(struct device *dev)
  800. {
  801. struct fsl_esai *esai = dev_get_drvdata(dev);
  802. int ret;
  803. regcache_cache_only(esai->regmap, false);
  804. /* FIFO reset for safety */
  805. regmap_update_bits(esai->regmap, REG_ESAI_TFCR,
  806. ESAI_xFCR_xFR, ESAI_xFCR_xFR);
  807. regmap_update_bits(esai->regmap, REG_ESAI_RFCR,
  808. ESAI_xFCR_xFR, ESAI_xFCR_xFR);
  809. ret = regcache_sync(esai->regmap);
  810. if (ret)
  811. return ret;
  812. /* FIFO reset done */
  813. regmap_update_bits(esai->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0);
  814. regmap_update_bits(esai->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0);
  815. return 0;
  816. }
  817. #endif /* CONFIG_PM_SLEEP */
  818. static const struct dev_pm_ops fsl_esai_pm_ops = {
  819. SET_SYSTEM_SLEEP_PM_OPS(fsl_esai_suspend, fsl_esai_resume)
  820. };
  821. static struct platform_driver fsl_esai_driver = {
  822. .probe = fsl_esai_probe,
  823. .driver = {
  824. .name = "fsl-esai-dai",
  825. .pm = &fsl_esai_pm_ops,
  826. .of_match_table = fsl_esai_dt_ids,
  827. },
  828. };
  829. module_platform_driver(fsl_esai_driver);
  830. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  831. MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver");
  832. MODULE_LICENSE("GPL v2");
  833. MODULE_ALIAS("platform:fsl-esai-dai");