mxs-saif.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright 2011 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/clk.h>
  26. #include <linux/clk-provider.h>
  27. #include <linux/delay.h>
  28. #include <linux/time.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include "mxs-saif.h"
  34. #define MXS_SET_ADDR 0x4
  35. #define MXS_CLR_ADDR 0x8
  36. static struct mxs_saif *mxs_saif[2];
  37. /*
  38. * SAIF is a little different with other normal SOC DAIs on clock using.
  39. *
  40. * For MXS, two SAIF modules are instantiated on-chip.
  41. * Each SAIF has a set of clock pins and can be operating in master
  42. * mode simultaneously if they are connected to different off-chip codecs.
  43. * Also, one of the two SAIFs can master or drive the clock pins while the
  44. * other SAIF, in slave mode, receives clocking from the master SAIF.
  45. * This also means that both SAIFs must operate at the same sample rate.
  46. *
  47. * We abstract this as each saif has a master, the master could be
  48. * itself or other saifs. In the generic saif driver, saif does not need
  49. * to know the different clkmux. Saif only needs to know who is its master
  50. * and operating its master to generate the proper clock rate for it.
  51. * The master id is provided in mach-specific layer according to different
  52. * clkmux setting.
  53. */
  54. static int mxs_saif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  55. int clk_id, unsigned int freq, int dir)
  56. {
  57. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  58. switch (clk_id) {
  59. case MXS_SAIF_MCLK:
  60. saif->mclk = freq;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * Since SAIF may work on EXTMASTER mode, IOW, it's working BITCLK&LRCLK
  69. * is provided by other SAIF, we provide a interface here to get its master
  70. * from its master_id.
  71. * Note that the master could be itself.
  72. */
  73. static inline struct mxs_saif *mxs_saif_get_master(struct mxs_saif * saif)
  74. {
  75. return mxs_saif[saif->master_id];
  76. }
  77. /*
  78. * Set SAIF clock and MCLK
  79. */
  80. static int mxs_saif_set_clk(struct mxs_saif *saif,
  81. unsigned int mclk,
  82. unsigned int rate)
  83. {
  84. u32 scr;
  85. int ret;
  86. struct mxs_saif *master_saif;
  87. dev_dbg(saif->dev, "mclk %d rate %d\n", mclk, rate);
  88. /* Set master saif to generate proper clock */
  89. master_saif = mxs_saif_get_master(saif);
  90. if (!master_saif)
  91. return -EINVAL;
  92. dev_dbg(saif->dev, "master saif%d\n", master_saif->id);
  93. /* Checking if can playback and capture simutaneously */
  94. if (master_saif->ongoing && rate != master_saif->cur_rate) {
  95. dev_err(saif->dev,
  96. "can not change clock, master saif%d(rate %d) is ongoing\n",
  97. master_saif->id, master_saif->cur_rate);
  98. return -EINVAL;
  99. }
  100. scr = __raw_readl(master_saif->base + SAIF_CTRL);
  101. scr &= ~BM_SAIF_CTRL_BITCLK_MULT_RATE;
  102. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  103. /*
  104. * Set SAIF clock
  105. *
  106. * The SAIF clock should be either 384*fs or 512*fs.
  107. * If MCLK is used, the SAIF clk ratio need to match mclk ratio.
  108. * For 32x mclk, set saif clk as 512*fs.
  109. * For 48x mclk, set saif clk as 384*fs.
  110. *
  111. * If MCLK is not used, we just set saif clk to 512*fs.
  112. */
  113. clk_prepare_enable(master_saif->clk);
  114. if (master_saif->mclk_in_use) {
  115. if (mclk % 32 == 0) {
  116. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  117. ret = clk_set_rate(master_saif->clk, 512 * rate);
  118. } else if (mclk % 48 == 0) {
  119. scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
  120. ret = clk_set_rate(master_saif->clk, 384 * rate);
  121. } else {
  122. /* SAIF MCLK should be either 32x or 48x */
  123. clk_disable_unprepare(master_saif->clk);
  124. return -EINVAL;
  125. }
  126. } else {
  127. ret = clk_set_rate(master_saif->clk, 512 * rate);
  128. scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
  129. }
  130. clk_disable_unprepare(master_saif->clk);
  131. if (ret)
  132. return ret;
  133. master_saif->cur_rate = rate;
  134. if (!master_saif->mclk_in_use) {
  135. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  136. return 0;
  137. }
  138. /*
  139. * Program the over-sample rate for MCLK output
  140. *
  141. * The available MCLK range is 32x, 48x... 512x. The rate
  142. * could be from 8kHz to 192kH.
  143. */
  144. switch (mclk / rate) {
  145. case 32:
  146. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
  147. break;
  148. case 64:
  149. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  150. break;
  151. case 128:
  152. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  153. break;
  154. case 256:
  155. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  156. break;
  157. case 512:
  158. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  159. break;
  160. case 48:
  161. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
  162. break;
  163. case 96:
  164. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
  165. break;
  166. case 192:
  167. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
  168. break;
  169. case 384:
  170. scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
  171. break;
  172. default:
  173. return -EINVAL;
  174. }
  175. __raw_writel(scr, master_saif->base + SAIF_CTRL);
  176. return 0;
  177. }
  178. /*
  179. * Put and disable MCLK.
  180. */
  181. int mxs_saif_put_mclk(unsigned int saif_id)
  182. {
  183. struct mxs_saif *saif = mxs_saif[saif_id];
  184. u32 stat;
  185. if (!saif)
  186. return -EINVAL;
  187. stat = __raw_readl(saif->base + SAIF_STAT);
  188. if (stat & BM_SAIF_STAT_BUSY) {
  189. dev_err(saif->dev, "error: busy\n");
  190. return -EBUSY;
  191. }
  192. clk_disable_unprepare(saif->clk);
  193. /* disable MCLK output */
  194. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  195. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  196. __raw_writel(BM_SAIF_CTRL_RUN,
  197. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  198. saif->mclk_in_use = 0;
  199. return 0;
  200. }
  201. EXPORT_SYMBOL_GPL(mxs_saif_put_mclk);
  202. /*
  203. * Get MCLK and set clock rate, then enable it
  204. *
  205. * This interface is used for codecs who are using MCLK provided
  206. * by saif.
  207. */
  208. int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
  209. unsigned int rate)
  210. {
  211. struct mxs_saif *saif = mxs_saif[saif_id];
  212. u32 stat;
  213. int ret;
  214. struct mxs_saif *master_saif;
  215. if (!saif)
  216. return -EINVAL;
  217. /* Clear Reset */
  218. __raw_writel(BM_SAIF_CTRL_SFTRST,
  219. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  220. /* FIXME: need clear clk gate for register r/w */
  221. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  222. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  223. master_saif = mxs_saif_get_master(saif);
  224. if (saif != master_saif) {
  225. dev_err(saif->dev, "can not get mclk from a non-master saif\n");
  226. return -EINVAL;
  227. }
  228. stat = __raw_readl(saif->base + SAIF_STAT);
  229. if (stat & BM_SAIF_STAT_BUSY) {
  230. dev_err(saif->dev, "error: busy\n");
  231. return -EBUSY;
  232. }
  233. saif->mclk_in_use = 1;
  234. ret = mxs_saif_set_clk(saif, mclk, rate);
  235. if (ret)
  236. return ret;
  237. ret = clk_prepare_enable(saif->clk);
  238. if (ret)
  239. return ret;
  240. /* enable MCLK output */
  241. __raw_writel(BM_SAIF_CTRL_RUN,
  242. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  243. return 0;
  244. }
  245. EXPORT_SYMBOL_GPL(mxs_saif_get_mclk);
  246. /*
  247. * SAIF DAI format configuration.
  248. * Should only be called when port is inactive.
  249. */
  250. static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  251. {
  252. u32 scr, stat;
  253. u32 scr0;
  254. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  255. stat = __raw_readl(saif->base + SAIF_STAT);
  256. if (stat & BM_SAIF_STAT_BUSY) {
  257. dev_err(cpu_dai->dev, "error: busy\n");
  258. return -EBUSY;
  259. }
  260. scr0 = __raw_readl(saif->base + SAIF_CTRL);
  261. scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
  262. & ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
  263. scr = 0;
  264. /* DAI mode */
  265. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  266. case SND_SOC_DAIFMT_I2S:
  267. /* data frame low 1clk before data */
  268. scr |= BM_SAIF_CTRL_DELAY;
  269. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  270. break;
  271. case SND_SOC_DAIFMT_LEFT_J:
  272. /* data frame high with data */
  273. scr &= ~BM_SAIF_CTRL_DELAY;
  274. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  275. scr &= ~BM_SAIF_CTRL_JUSTIFY;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. /* DAI clock inversion */
  281. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  282. case SND_SOC_DAIFMT_IB_IF:
  283. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  284. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  285. break;
  286. case SND_SOC_DAIFMT_IB_NF:
  287. scr |= BM_SAIF_CTRL_BITCLK_EDGE;
  288. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  289. break;
  290. case SND_SOC_DAIFMT_NB_IF:
  291. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  292. scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
  293. break;
  294. case SND_SOC_DAIFMT_NB_NF:
  295. scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
  296. scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
  297. break;
  298. }
  299. /*
  300. * Note: We simply just support master mode since SAIF TX can only
  301. * work as master.
  302. * Here the master is relative to codec side.
  303. * Saif internally could be slave when working on EXTMASTER mode.
  304. * We just hide this to machine driver.
  305. */
  306. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  307. case SND_SOC_DAIFMT_CBS_CFS:
  308. if (saif->id == saif->master_id)
  309. scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
  310. else
  311. scr |= BM_SAIF_CTRL_SLAVE_MODE;
  312. __raw_writel(scr | scr0, saif->base + SAIF_CTRL);
  313. break;
  314. default:
  315. return -EINVAL;
  316. }
  317. return 0;
  318. }
  319. static int mxs_saif_startup(struct snd_pcm_substream *substream,
  320. struct snd_soc_dai *cpu_dai)
  321. {
  322. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  323. /* clear error status to 0 for each re-open */
  324. saif->fifo_underrun = 0;
  325. saif->fifo_overrun = 0;
  326. /* Clear Reset for normal operations */
  327. __raw_writel(BM_SAIF_CTRL_SFTRST,
  328. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  329. /* clear clock gate */
  330. __raw_writel(BM_SAIF_CTRL_CLKGATE,
  331. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  332. clk_prepare(saif->clk);
  333. return 0;
  334. }
  335. static void mxs_saif_shutdown(struct snd_pcm_substream *substream,
  336. struct snd_soc_dai *cpu_dai)
  337. {
  338. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  339. clk_unprepare(saif->clk);
  340. }
  341. /*
  342. * Should only be called when port is inactive.
  343. * although can be called multiple times by upper layers.
  344. */
  345. static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
  346. struct snd_pcm_hw_params *params,
  347. struct snd_soc_dai *cpu_dai)
  348. {
  349. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  350. struct mxs_saif *master_saif;
  351. u32 scr, stat;
  352. int ret;
  353. master_saif = mxs_saif_get_master(saif);
  354. if (!master_saif)
  355. return -EINVAL;
  356. /* mclk should already be set */
  357. if (!saif->mclk && saif->mclk_in_use) {
  358. dev_err(cpu_dai->dev, "set mclk first\n");
  359. return -EINVAL;
  360. }
  361. stat = __raw_readl(saif->base + SAIF_STAT);
  362. if (!saif->mclk_in_use && (stat & BM_SAIF_STAT_BUSY)) {
  363. dev_err(cpu_dai->dev, "error: busy\n");
  364. return -EBUSY;
  365. }
  366. /*
  367. * Set saif clk based on sample rate.
  368. * If mclk is used, we also set mclk, if not, saif->mclk is
  369. * default 0, means not used.
  370. */
  371. ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
  372. if (ret) {
  373. dev_err(cpu_dai->dev, "unable to get proper clk\n");
  374. return ret;
  375. }
  376. if (saif != master_saif) {
  377. /*
  378. * Set an initial clock rate for the saif internal logic to work
  379. * properly. This is important when working in EXTMASTER mode
  380. * that uses the other saif's BITCLK&LRCLK but it still needs a
  381. * basic clock which should be fast enough for the internal
  382. * logic.
  383. */
  384. clk_enable(saif->clk);
  385. ret = clk_set_rate(saif->clk, 24000000);
  386. clk_disable(saif->clk);
  387. if (ret)
  388. return ret;
  389. clk_prepare(master_saif->clk);
  390. }
  391. scr = __raw_readl(saif->base + SAIF_CTRL);
  392. scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
  393. scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  394. switch (params_format(params)) {
  395. case SNDRV_PCM_FORMAT_S16_LE:
  396. scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
  397. break;
  398. case SNDRV_PCM_FORMAT_S20_3LE:
  399. scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
  400. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  401. break;
  402. case SNDRV_PCM_FORMAT_S24_LE:
  403. scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
  404. scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
  405. break;
  406. default:
  407. return -EINVAL;
  408. }
  409. /* Tx/Rx config */
  410. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  411. /* enable TX mode */
  412. scr &= ~BM_SAIF_CTRL_READ_MODE;
  413. } else {
  414. /* enable RX mode */
  415. scr |= BM_SAIF_CTRL_READ_MODE;
  416. }
  417. __raw_writel(scr, saif->base + SAIF_CTRL);
  418. return 0;
  419. }
  420. static int mxs_saif_prepare(struct snd_pcm_substream *substream,
  421. struct snd_soc_dai *cpu_dai)
  422. {
  423. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  424. /* enable FIFO error irqs */
  425. __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
  426. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  427. return 0;
  428. }
  429. static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
  430. struct snd_soc_dai *cpu_dai)
  431. {
  432. struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
  433. struct mxs_saif *master_saif;
  434. u32 delay;
  435. int ret;
  436. master_saif = mxs_saif_get_master(saif);
  437. if (!master_saif)
  438. return -EINVAL;
  439. switch (cmd) {
  440. case SNDRV_PCM_TRIGGER_START:
  441. case SNDRV_PCM_TRIGGER_RESUME:
  442. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  443. if (saif->state == MXS_SAIF_STATE_RUNNING)
  444. return 0;
  445. dev_dbg(cpu_dai->dev, "start\n");
  446. ret = clk_enable(master_saif->clk);
  447. if (ret) {
  448. dev_err(saif->dev, "Failed to enable master clock\n");
  449. return ret;
  450. }
  451. /*
  452. * If the saif's master is not itself, we also need to enable
  453. * itself clk for its internal basic logic to work.
  454. */
  455. if (saif != master_saif) {
  456. ret = clk_enable(saif->clk);
  457. if (ret) {
  458. dev_err(saif->dev, "Failed to enable master clock\n");
  459. clk_disable(master_saif->clk);
  460. return ret;
  461. }
  462. __raw_writel(BM_SAIF_CTRL_RUN,
  463. saif->base + SAIF_CTRL + MXS_SET_ADDR);
  464. }
  465. if (!master_saif->mclk_in_use)
  466. __raw_writel(BM_SAIF_CTRL_RUN,
  467. master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
  468. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  469. /*
  470. * write data to saif data register to trigger
  471. * the transfer.
  472. * For 24-bit format the 32-bit FIFO register stores
  473. * only one channel, so we need to write twice.
  474. * This is also safe for the other non 24-bit formats.
  475. */
  476. __raw_writel(0, saif->base + SAIF_DATA);
  477. __raw_writel(0, saif->base + SAIF_DATA);
  478. } else {
  479. /*
  480. * read data from saif data register to trigger
  481. * the receive.
  482. * For 24-bit format the 32-bit FIFO register stores
  483. * only one channel, so we need to read twice.
  484. * This is also safe for the other non 24-bit formats.
  485. */
  486. __raw_readl(saif->base + SAIF_DATA);
  487. __raw_readl(saif->base + SAIF_DATA);
  488. }
  489. master_saif->ongoing = 1;
  490. saif->state = MXS_SAIF_STATE_RUNNING;
  491. dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
  492. __raw_readl(saif->base + SAIF_CTRL),
  493. __raw_readl(saif->base + SAIF_STAT));
  494. dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
  495. __raw_readl(master_saif->base + SAIF_CTRL),
  496. __raw_readl(master_saif->base + SAIF_STAT));
  497. break;
  498. case SNDRV_PCM_TRIGGER_SUSPEND:
  499. case SNDRV_PCM_TRIGGER_STOP:
  500. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  501. if (saif->state == MXS_SAIF_STATE_STOPPED)
  502. return 0;
  503. dev_dbg(cpu_dai->dev, "stop\n");
  504. /* wait a while for the current sample to complete */
  505. delay = USEC_PER_SEC / master_saif->cur_rate;
  506. if (!master_saif->mclk_in_use) {
  507. __raw_writel(BM_SAIF_CTRL_RUN,
  508. master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  509. udelay(delay);
  510. }
  511. clk_disable(master_saif->clk);
  512. if (saif != master_saif) {
  513. __raw_writel(BM_SAIF_CTRL_RUN,
  514. saif->base + SAIF_CTRL + MXS_CLR_ADDR);
  515. udelay(delay);
  516. clk_disable(saif->clk);
  517. }
  518. master_saif->ongoing = 0;
  519. saif->state = MXS_SAIF_STATE_STOPPED;
  520. break;
  521. default:
  522. return -EINVAL;
  523. }
  524. return 0;
  525. }
  526. #define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
  527. #define MXS_SAIF_FORMATS \
  528. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  529. SNDRV_PCM_FMTBIT_S24_LE)
  530. static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
  531. .startup = mxs_saif_startup,
  532. .shutdown = mxs_saif_shutdown,
  533. .trigger = mxs_saif_trigger,
  534. .prepare = mxs_saif_prepare,
  535. .hw_params = mxs_saif_hw_params,
  536. .set_sysclk = mxs_saif_set_dai_sysclk,
  537. .set_fmt = mxs_saif_set_dai_fmt,
  538. };
  539. static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
  540. {
  541. struct mxs_saif *saif = dev_get_drvdata(dai->dev);
  542. snd_soc_dai_set_drvdata(dai, saif);
  543. return 0;
  544. }
  545. static struct snd_soc_dai_driver mxs_saif_dai = {
  546. .name = "mxs-saif",
  547. .probe = mxs_saif_dai_probe,
  548. .playback = {
  549. .channels_min = 2,
  550. .channels_max = 2,
  551. .rates = MXS_SAIF_RATES,
  552. .formats = MXS_SAIF_FORMATS,
  553. },
  554. .capture = {
  555. .channels_min = 2,
  556. .channels_max = 2,
  557. .rates = MXS_SAIF_RATES,
  558. .formats = MXS_SAIF_FORMATS,
  559. },
  560. .ops = &mxs_saif_dai_ops,
  561. };
  562. static const struct snd_soc_component_driver mxs_saif_component = {
  563. .name = "mxs-saif",
  564. };
  565. static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
  566. {
  567. struct mxs_saif *saif = dev_id;
  568. unsigned int stat;
  569. stat = __raw_readl(saif->base + SAIF_STAT);
  570. if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
  571. BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
  572. return IRQ_NONE;
  573. if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
  574. dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
  575. __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
  576. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  577. }
  578. if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
  579. dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
  580. __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
  581. saif->base + SAIF_STAT + MXS_CLR_ADDR);
  582. }
  583. dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
  584. __raw_readl(saif->base + SAIF_CTRL),
  585. __raw_readl(saif->base + SAIF_STAT));
  586. return IRQ_HANDLED;
  587. }
  588. static int mxs_saif_mclk_init(struct platform_device *pdev)
  589. {
  590. struct mxs_saif *saif = platform_get_drvdata(pdev);
  591. struct device_node *np = pdev->dev.of_node;
  592. struct clk *clk;
  593. int ret;
  594. clk = clk_register_divider(&pdev->dev, "mxs_saif_mclk",
  595. __clk_get_name(saif->clk), 0,
  596. saif->base + SAIF_CTRL,
  597. BP_SAIF_CTRL_BITCLK_MULT_RATE, 3,
  598. 0, NULL);
  599. if (IS_ERR(clk)) {
  600. ret = PTR_ERR(clk);
  601. if (ret == -EEXIST)
  602. return 0;
  603. dev_err(&pdev->dev, "failed to register mclk: %d\n", ret);
  604. return PTR_ERR(clk);
  605. }
  606. ret = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  607. if (ret)
  608. return ret;
  609. return 0;
  610. }
  611. static int mxs_saif_probe(struct platform_device *pdev)
  612. {
  613. struct device_node *np = pdev->dev.of_node;
  614. struct resource *iores;
  615. struct mxs_saif *saif;
  616. int irq, ret = 0;
  617. struct device_node *master;
  618. if (!np)
  619. return -EINVAL;
  620. saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
  621. if (!saif)
  622. return -ENOMEM;
  623. ret = of_alias_get_id(np, "saif");
  624. if (ret < 0)
  625. return ret;
  626. else
  627. saif->id = ret;
  628. /*
  629. * If there is no "fsl,saif-master" phandle, it's a saif
  630. * master. Otherwise, it's a slave and its phandle points
  631. * to the master.
  632. */
  633. master = of_parse_phandle(np, "fsl,saif-master", 0);
  634. if (!master) {
  635. saif->master_id = saif->id;
  636. } else {
  637. ret = of_alias_get_id(master, "saif");
  638. if (ret < 0)
  639. return ret;
  640. else
  641. saif->master_id = ret;
  642. }
  643. if (saif->master_id >= ARRAY_SIZE(mxs_saif)) {
  644. dev_err(&pdev->dev, "get wrong master id\n");
  645. return -EINVAL;
  646. }
  647. mxs_saif[saif->id] = saif;
  648. saif->clk = devm_clk_get(&pdev->dev, NULL);
  649. if (IS_ERR(saif->clk)) {
  650. ret = PTR_ERR(saif->clk);
  651. dev_err(&pdev->dev, "Cannot get the clock: %d\n",
  652. ret);
  653. return ret;
  654. }
  655. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  656. saif->base = devm_ioremap_resource(&pdev->dev, iores);
  657. if (IS_ERR(saif->base))
  658. return PTR_ERR(saif->base);
  659. irq = platform_get_irq(pdev, 0);
  660. if (irq < 0) {
  661. ret = irq;
  662. dev_err(&pdev->dev, "failed to get irq resource: %d\n",
  663. ret);
  664. return ret;
  665. }
  666. saif->dev = &pdev->dev;
  667. ret = devm_request_irq(&pdev->dev, irq, mxs_saif_irq, 0,
  668. dev_name(&pdev->dev), saif);
  669. if (ret) {
  670. dev_err(&pdev->dev, "failed to request irq\n");
  671. return ret;
  672. }
  673. platform_set_drvdata(pdev, saif);
  674. /* We only support saif0 being tx and clock master */
  675. if (saif->id == 0) {
  676. ret = mxs_saif_mclk_init(pdev);
  677. if (ret)
  678. dev_warn(&pdev->dev, "failed to init clocks\n");
  679. }
  680. ret = devm_snd_soc_register_component(&pdev->dev, &mxs_saif_component,
  681. &mxs_saif_dai, 1);
  682. if (ret) {
  683. dev_err(&pdev->dev, "register DAI failed\n");
  684. return ret;
  685. }
  686. ret = mxs_pcm_platform_register(&pdev->dev);
  687. if (ret) {
  688. dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
  689. return ret;
  690. }
  691. return 0;
  692. }
  693. static const struct of_device_id mxs_saif_dt_ids[] = {
  694. { .compatible = "fsl,imx28-saif", },
  695. { /* sentinel */ }
  696. };
  697. MODULE_DEVICE_TABLE(of, mxs_saif_dt_ids);
  698. static struct platform_driver mxs_saif_driver = {
  699. .probe = mxs_saif_probe,
  700. .driver = {
  701. .name = "mxs-saif",
  702. .of_match_table = mxs_saif_dt_ids,
  703. },
  704. };
  705. module_platform_driver(mxs_saif_driver);
  706. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  707. MODULE_DESCRIPTION("MXS ASoC SAIF driver");
  708. MODULE_LICENSE("GPL");
  709. MODULE_ALIAS("platform:mxs-saif");