mxs-saif.c 20 KB

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