mxs-saif.c 20 KB

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