fsl_esai.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. * @fifo_depth: depth of tx/rx FIFO
  35. * @slot_width: width of each DAI slot
  36. * @slots: number of slots
  37. * @hck_rate: clock rate of desired HCKx clock
  38. * @sck_rate: clock rate of desired SCKx clock
  39. * @hck_dir: the direction of HCKx pads
  40. * @sck_div: if using PSR/PM dividers for SCKx clock
  41. * @slave_mode: if fully using DAI slave mode
  42. * @synchronous: if using tx/rx synchronous mode
  43. * @name: driver name
  44. */
  45. struct fsl_esai {
  46. struct snd_dmaengine_dai_dma_data dma_params_rx;
  47. struct snd_dmaengine_dai_dma_data dma_params_tx;
  48. struct platform_device *pdev;
  49. struct regmap *regmap;
  50. struct clk *coreclk;
  51. struct clk *extalclk;
  52. struct clk *fsysclk;
  53. u32 fifo_depth;
  54. u32 slot_width;
  55. u32 slots;
  56. u32 tx_mask;
  57. u32 rx_mask;
  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: Transmition 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: Transmition 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: Transmition 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_RCCR,
  302. ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
  303. esai_priv->slot_width = slot_width;
  304. esai_priv->slots = slots;
  305. esai_priv->tx_mask = tx_mask;
  306. esai_priv->rx_mask = rx_mask;
  307. return 0;
  308. }
  309. static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  310. {
  311. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  312. u32 xcr = 0, xccr = 0, mask;
  313. /* DAI mode */
  314. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  315. case SND_SOC_DAIFMT_I2S:
  316. /* Data on rising edge of bclk, frame low, 1clk before data */
  317. xcr |= ESAI_xCR_xFSR;
  318. xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  319. break;
  320. case SND_SOC_DAIFMT_LEFT_J:
  321. /* Data on rising edge of bclk, frame high */
  322. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  323. break;
  324. case SND_SOC_DAIFMT_RIGHT_J:
  325. /* Data on rising edge of bclk, frame high, right aligned */
  326. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  327. xcr |= ESAI_xCR_xWA;
  328. break;
  329. case SND_SOC_DAIFMT_DSP_A:
  330. /* Data on rising edge of bclk, frame high, 1clk before data */
  331. xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR;
  332. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  333. break;
  334. case SND_SOC_DAIFMT_DSP_B:
  335. /* Data on rising edge of bclk, frame high */
  336. xcr |= ESAI_xCR_xFSL;
  337. xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  338. break;
  339. default:
  340. return -EINVAL;
  341. }
  342. /* DAI clock inversion */
  343. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  344. case SND_SOC_DAIFMT_NB_NF:
  345. /* Nothing to do for both normal cases */
  346. break;
  347. case SND_SOC_DAIFMT_IB_NF:
  348. /* Invert bit clock */
  349. xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
  350. break;
  351. case SND_SOC_DAIFMT_NB_IF:
  352. /* Invert frame clock */
  353. xccr ^= ESAI_xCCR_xFSP;
  354. break;
  355. case SND_SOC_DAIFMT_IB_IF:
  356. /* Invert both clocks */
  357. xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP;
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. esai_priv->slave_mode = false;
  363. /* DAI clock master masks */
  364. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  365. case SND_SOC_DAIFMT_CBM_CFM:
  366. esai_priv->slave_mode = true;
  367. break;
  368. case SND_SOC_DAIFMT_CBS_CFM:
  369. xccr |= ESAI_xCCR_xCKD;
  370. break;
  371. case SND_SOC_DAIFMT_CBM_CFS:
  372. xccr |= ESAI_xCCR_xFSD;
  373. break;
  374. case SND_SOC_DAIFMT_CBS_CFS:
  375. xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA;
  381. regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr);
  382. regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr);
  383. mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP |
  384. ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
  385. regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr);
  386. regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr);
  387. return 0;
  388. }
  389. static int fsl_esai_startup(struct snd_pcm_substream *substream,
  390. struct snd_soc_dai *dai)
  391. {
  392. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  393. int ret;
  394. /*
  395. * Some platforms might use the same bit to gate all three or two of
  396. * clocks, so keep all clocks open/close at the same time for safety
  397. */
  398. ret = clk_prepare_enable(esai_priv->coreclk);
  399. if (ret)
  400. return ret;
  401. if (!IS_ERR(esai_priv->extalclk)) {
  402. ret = clk_prepare_enable(esai_priv->extalclk);
  403. if (ret)
  404. goto err_extalck;
  405. }
  406. if (!IS_ERR(esai_priv->fsysclk)) {
  407. ret = clk_prepare_enable(esai_priv->fsysclk);
  408. if (ret)
  409. goto err_fsysclk;
  410. }
  411. if (!dai->active) {
  412. /* Set synchronous mode */
  413. regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR,
  414. ESAI_SAICR_SYNC, esai_priv->synchronous ?
  415. ESAI_SAICR_SYNC : 0);
  416. /* Set a default slot number -- 2 */
  417. regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
  418. ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
  419. regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
  420. ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
  421. }
  422. return 0;
  423. err_fsysclk:
  424. if (!IS_ERR(esai_priv->extalclk))
  425. clk_disable_unprepare(esai_priv->extalclk);
  426. err_extalck:
  427. clk_disable_unprepare(esai_priv->coreclk);
  428. return ret;
  429. }
  430. static int fsl_esai_hw_params(struct snd_pcm_substream *substream,
  431. struct snd_pcm_hw_params *params,
  432. struct snd_soc_dai *dai)
  433. {
  434. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  435. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  436. u32 width = snd_pcm_format_width(params_format(params));
  437. u32 channels = params_channels(params);
  438. u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
  439. u32 slot_width = width;
  440. u32 bclk, mask, val;
  441. int ret;
  442. /* Override slot_width if being specifically set */
  443. if (esai_priv->slot_width)
  444. slot_width = esai_priv->slot_width;
  445. bclk = params_rate(params) * slot_width * esai_priv->slots;
  446. ret = fsl_esai_set_bclk(dai, tx, bclk);
  447. if (ret)
  448. return ret;
  449. /* Use Normal mode to support monaural audio */
  450. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
  451. ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ?
  452. ESAI_xCR_xMOD_NETWORK : 0);
  453. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  454. ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR);
  455. mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK |
  456. (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK);
  457. val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) |
  458. (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins));
  459. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val);
  460. mask = ESAI_xCR_xSWS_MASK | (tx ? ESAI_xCR_PADC : 0);
  461. val = ESAI_xCR_xSWS(slot_width, width) | (tx ? ESAI_xCR_PADC : 0);
  462. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val);
  463. /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */
  464. regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
  465. ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO));
  466. regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
  467. ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO));
  468. return 0;
  469. }
  470. static void fsl_esai_shutdown(struct snd_pcm_substream *substream,
  471. struct snd_soc_dai *dai)
  472. {
  473. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  474. if (!IS_ERR(esai_priv->fsysclk))
  475. clk_disable_unprepare(esai_priv->fsysclk);
  476. if (!IS_ERR(esai_priv->extalclk))
  477. clk_disable_unprepare(esai_priv->extalclk);
  478. clk_disable_unprepare(esai_priv->coreclk);
  479. }
  480. static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd,
  481. struct snd_soc_dai *dai)
  482. {
  483. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  484. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  485. u8 i, channels = substream->runtime->channels;
  486. u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
  487. u32 mask;
  488. switch (cmd) {
  489. case SNDRV_PCM_TRIGGER_START:
  490. case SNDRV_PCM_TRIGGER_RESUME:
  491. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  492. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  493. ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN);
  494. /* Write initial words reqiured by ESAI as normal procedure */
  495. for (i = 0; tx && i < channels; i++)
  496. regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0);
  497. /*
  498. * When set the TE/RE in the end of enablement flow, there
  499. * will be channel swap issue for multi data line case.
  500. * In order to workaround this issue, we switch the bit
  501. * enablement sequence to below sequence
  502. * 1) clear the xSMB & xSMA: which is done in probe and
  503. * stop state.
  504. * 2) set TE/RE
  505. * 3) set xSMB
  506. * 4) set xSMA: xSMA is the last one in this flow, which
  507. * will trigger esai to start.
  508. */
  509. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
  510. tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK,
  511. tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins));
  512. mask = tx ? esai_priv->tx_mask : esai_priv->rx_mask;
  513. regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx),
  514. ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(mask));
  515. regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx),
  516. ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(mask));
  517. break;
  518. case SNDRV_PCM_TRIGGER_SUSPEND:
  519. case SNDRV_PCM_TRIGGER_STOP:
  520. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  521. regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
  522. tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0);
  523. regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx),
  524. ESAI_xSMA_xS_MASK, 0);
  525. regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx),
  526. ESAI_xSMB_xS_MASK, 0);
  527. /* Disable and reset FIFO */
  528. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  529. ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR);
  530. regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
  531. ESAI_xFCR_xFR, 0);
  532. break;
  533. default:
  534. return -EINVAL;
  535. }
  536. return 0;
  537. }
  538. static struct snd_soc_dai_ops fsl_esai_dai_ops = {
  539. .startup = fsl_esai_startup,
  540. .shutdown = fsl_esai_shutdown,
  541. .trigger = fsl_esai_trigger,
  542. .hw_params = fsl_esai_hw_params,
  543. .set_sysclk = fsl_esai_set_dai_sysclk,
  544. .set_fmt = fsl_esai_set_dai_fmt,
  545. .set_tdm_slot = fsl_esai_set_dai_tdm_slot,
  546. };
  547. static int fsl_esai_dai_probe(struct snd_soc_dai *dai)
  548. {
  549. struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
  550. snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx,
  551. &esai_priv->dma_params_rx);
  552. return 0;
  553. }
  554. static struct snd_soc_dai_driver fsl_esai_dai = {
  555. .probe = fsl_esai_dai_probe,
  556. .playback = {
  557. .stream_name = "CPU-Playback",
  558. .channels_min = 1,
  559. .channels_max = 12,
  560. .rates = FSL_ESAI_RATES,
  561. .formats = FSL_ESAI_FORMATS,
  562. },
  563. .capture = {
  564. .stream_name = "CPU-Capture",
  565. .channels_min = 1,
  566. .channels_max = 8,
  567. .rates = FSL_ESAI_RATES,
  568. .formats = FSL_ESAI_FORMATS,
  569. },
  570. .ops = &fsl_esai_dai_ops,
  571. };
  572. static const struct snd_soc_component_driver fsl_esai_component = {
  573. .name = "fsl-esai",
  574. };
  575. static const struct reg_default fsl_esai_reg_defaults[] = {
  576. {0x8, 0x00000000},
  577. {0x10, 0x00000000},
  578. {0x18, 0x00000000},
  579. {0x98, 0x00000000},
  580. {0xd0, 0x00000000},
  581. {0xd4, 0x00000000},
  582. {0xd8, 0x00000000},
  583. {0xdc, 0x00000000},
  584. {0xe0, 0x00000000},
  585. {0xe4, 0x0000ffff},
  586. {0xe8, 0x0000ffff},
  587. {0xec, 0x0000ffff},
  588. {0xf0, 0x0000ffff},
  589. {0xf8, 0x00000000},
  590. {0xfc, 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_ETDR:
  627. case REG_ESAI_ERDR:
  628. case REG_ESAI_ESR:
  629. case REG_ESAI_TFSR:
  630. case REG_ESAI_RFSR:
  631. case REG_ESAI_TX0:
  632. case REG_ESAI_TX1:
  633. case REG_ESAI_TX2:
  634. case REG_ESAI_TX3:
  635. case REG_ESAI_TX4:
  636. case REG_ESAI_TX5:
  637. case REG_ESAI_RX0:
  638. case REG_ESAI_RX1:
  639. case REG_ESAI_RX2:
  640. case REG_ESAI_RX3:
  641. case REG_ESAI_SAISR:
  642. return true;
  643. default:
  644. return false;
  645. }
  646. }
  647. static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg)
  648. {
  649. switch (reg) {
  650. case REG_ESAI_ETDR:
  651. case REG_ESAI_ECR:
  652. case REG_ESAI_TFCR:
  653. case REG_ESAI_RFCR:
  654. case REG_ESAI_TX0:
  655. case REG_ESAI_TX1:
  656. case REG_ESAI_TX2:
  657. case REG_ESAI_TX3:
  658. case REG_ESAI_TX4:
  659. case REG_ESAI_TX5:
  660. case REG_ESAI_TSR:
  661. case REG_ESAI_SAICR:
  662. case REG_ESAI_TCR:
  663. case REG_ESAI_TCCR:
  664. case REG_ESAI_RCR:
  665. case REG_ESAI_RCCR:
  666. case REG_ESAI_TSMA:
  667. case REG_ESAI_TSMB:
  668. case REG_ESAI_RSMA:
  669. case REG_ESAI_RSMB:
  670. case REG_ESAI_PRRC:
  671. case REG_ESAI_PCRC:
  672. return true;
  673. default:
  674. return false;
  675. }
  676. }
  677. static const struct regmap_config fsl_esai_regmap_config = {
  678. .reg_bits = 32,
  679. .reg_stride = 4,
  680. .val_bits = 32,
  681. .max_register = REG_ESAI_PCRC,
  682. .reg_defaults = fsl_esai_reg_defaults,
  683. .num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults),
  684. .readable_reg = fsl_esai_readable_reg,
  685. .volatile_reg = fsl_esai_volatile_reg,
  686. .writeable_reg = fsl_esai_writeable_reg,
  687. .cache_type = REGCACHE_RBTREE,
  688. };
  689. static int fsl_esai_probe(struct platform_device *pdev)
  690. {
  691. struct device_node *np = pdev->dev.of_node;
  692. struct fsl_esai *esai_priv;
  693. struct resource *res;
  694. const uint32_t *iprop;
  695. void __iomem *regs;
  696. int irq, ret;
  697. esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL);
  698. if (!esai_priv)
  699. return -ENOMEM;
  700. esai_priv->pdev = pdev;
  701. strncpy(esai_priv->name, np->name, sizeof(esai_priv->name) - 1);
  702. /* Get the addresses and IRQ */
  703. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  704. regs = devm_ioremap_resource(&pdev->dev, res);
  705. if (IS_ERR(regs))
  706. return PTR_ERR(regs);
  707. esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  708. "core", regs, &fsl_esai_regmap_config);
  709. if (IS_ERR(esai_priv->regmap)) {
  710. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  711. PTR_ERR(esai_priv->regmap));
  712. return PTR_ERR(esai_priv->regmap);
  713. }
  714. esai_priv->coreclk = devm_clk_get(&pdev->dev, "core");
  715. if (IS_ERR(esai_priv->coreclk)) {
  716. dev_err(&pdev->dev, "failed to get core clock: %ld\n",
  717. PTR_ERR(esai_priv->coreclk));
  718. return PTR_ERR(esai_priv->coreclk);
  719. }
  720. esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal");
  721. if (IS_ERR(esai_priv->extalclk))
  722. dev_warn(&pdev->dev, "failed to get extal clock: %ld\n",
  723. PTR_ERR(esai_priv->extalclk));
  724. esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys");
  725. if (IS_ERR(esai_priv->fsysclk))
  726. dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n",
  727. PTR_ERR(esai_priv->fsysclk));
  728. irq = platform_get_irq(pdev, 0);
  729. if (irq < 0) {
  730. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  731. return irq;
  732. }
  733. ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0,
  734. esai_priv->name, esai_priv);
  735. if (ret) {
  736. dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
  737. return ret;
  738. }
  739. /* Set a default slot number */
  740. esai_priv->slots = 2;
  741. /* Set a default master/slave state */
  742. esai_priv->slave_mode = true;
  743. /* Determine the FIFO depth */
  744. iprop = of_get_property(np, "fsl,fifo-depth", NULL);
  745. if (iprop)
  746. esai_priv->fifo_depth = be32_to_cpup(iprop);
  747. else
  748. esai_priv->fifo_depth = 64;
  749. esai_priv->dma_params_tx.maxburst = 16;
  750. esai_priv->dma_params_rx.maxburst = 16;
  751. esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR;
  752. esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR;
  753. esai_priv->synchronous =
  754. of_property_read_bool(np, "fsl,esai-synchronous");
  755. /* Implement full symmetry for synchronous mode */
  756. if (esai_priv->synchronous) {
  757. fsl_esai_dai.symmetric_rates = 1;
  758. fsl_esai_dai.symmetric_channels = 1;
  759. fsl_esai_dai.symmetric_samplebits = 1;
  760. }
  761. dev_set_drvdata(&pdev->dev, esai_priv);
  762. /* Reset ESAI unit */
  763. ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ERST);
  764. if (ret) {
  765. dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret);
  766. return ret;
  767. }
  768. /*
  769. * We need to enable ESAI so as to access some of its registers.
  770. * Otherwise, we would fail to dump regmap from user space.
  771. */
  772. ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ESAIEN);
  773. if (ret) {
  774. dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret);
  775. return ret;
  776. }
  777. esai_priv->tx_mask = 0xFFFFFFFF;
  778. esai_priv->rx_mask = 0xFFFFFFFF;
  779. /* Clear the TSMA, TSMB, RSMA, RSMB */
  780. regmap_write(esai_priv->regmap, REG_ESAI_TSMA, 0);
  781. regmap_write(esai_priv->regmap, REG_ESAI_TSMB, 0);
  782. regmap_write(esai_priv->regmap, REG_ESAI_RSMA, 0);
  783. regmap_write(esai_priv->regmap, REG_ESAI_RSMB, 0);
  784. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component,
  785. &fsl_esai_dai, 1);
  786. if (ret) {
  787. dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
  788. return ret;
  789. }
  790. ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE);
  791. if (ret)
  792. dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret);
  793. return ret;
  794. }
  795. static const struct of_device_id fsl_esai_dt_ids[] = {
  796. { .compatible = "fsl,imx35-esai", },
  797. { .compatible = "fsl,vf610-esai", },
  798. {}
  799. };
  800. MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids);
  801. #ifdef CONFIG_PM_SLEEP
  802. static int fsl_esai_suspend(struct device *dev)
  803. {
  804. struct fsl_esai *esai = dev_get_drvdata(dev);
  805. regcache_cache_only(esai->regmap, true);
  806. regcache_mark_dirty(esai->regmap);
  807. return 0;
  808. }
  809. static int fsl_esai_resume(struct device *dev)
  810. {
  811. struct fsl_esai *esai = dev_get_drvdata(dev);
  812. int ret;
  813. regcache_cache_only(esai->regmap, false);
  814. /* FIFO reset for safety */
  815. regmap_update_bits(esai->regmap, REG_ESAI_TFCR,
  816. ESAI_xFCR_xFR, ESAI_xFCR_xFR);
  817. regmap_update_bits(esai->regmap, REG_ESAI_RFCR,
  818. ESAI_xFCR_xFR, ESAI_xFCR_xFR);
  819. ret = regcache_sync(esai->regmap);
  820. if (ret)
  821. return ret;
  822. /* FIFO reset done */
  823. regmap_update_bits(esai->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0);
  824. regmap_update_bits(esai->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0);
  825. return 0;
  826. }
  827. #endif /* CONFIG_PM_SLEEP */
  828. static const struct dev_pm_ops fsl_esai_pm_ops = {
  829. SET_SYSTEM_SLEEP_PM_OPS(fsl_esai_suspend, fsl_esai_resume)
  830. };
  831. static struct platform_driver fsl_esai_driver = {
  832. .probe = fsl_esai_probe,
  833. .driver = {
  834. .name = "fsl-esai-dai",
  835. .pm = &fsl_esai_pm_ops,
  836. .of_match_table = fsl_esai_dt_ids,
  837. },
  838. };
  839. module_platform_driver(fsl_esai_driver);
  840. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  841. MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver");
  842. MODULE_LICENSE("GPL v2");
  843. MODULE_ALIAS("platform:fsl-esai-dai");