designware_i2s.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * ALSA SoC Synopsys I2S Audio Layer
  3. *
  4. * sound/soc/dwc/designware_i2s.c
  5. *
  6. * Copyright (C) 2010 ST Microelectronics
  7. * Rajeev Kumar <rajeevkumar.linux@gmail.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/pm_runtime.h>
  21. #include <sound/designware_i2s.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/dmaengine_pcm.h>
  26. #include "local.h"
  27. static inline void i2s_write_reg(void __iomem *io_base, int reg, u32 val)
  28. {
  29. writel(val, io_base + reg);
  30. }
  31. static inline u32 i2s_read_reg(void __iomem *io_base, int reg)
  32. {
  33. return readl(io_base + reg);
  34. }
  35. static inline void i2s_disable_channels(struct dw_i2s_dev *dev, u32 stream)
  36. {
  37. u32 i = 0;
  38. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  39. for (i = 0; i < 4; i++)
  40. i2s_write_reg(dev->i2s_base, TER(i), 0);
  41. } else {
  42. for (i = 0; i < 4; i++)
  43. i2s_write_reg(dev->i2s_base, RER(i), 0);
  44. }
  45. }
  46. static inline void i2s_clear_irqs(struct dw_i2s_dev *dev, u32 stream)
  47. {
  48. u32 i = 0;
  49. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  50. for (i = 0; i < 4; i++)
  51. i2s_read_reg(dev->i2s_base, TOR(i));
  52. } else {
  53. for (i = 0; i < 4; i++)
  54. i2s_read_reg(dev->i2s_base, ROR(i));
  55. }
  56. }
  57. static inline void i2s_disable_irqs(struct dw_i2s_dev *dev, u32 stream,
  58. int chan_nr)
  59. {
  60. u32 i, irq;
  61. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  62. for (i = 0; i < (chan_nr / 2); i++) {
  63. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  64. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x30);
  65. }
  66. } else {
  67. for (i = 0; i < (chan_nr / 2); i++) {
  68. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  69. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x03);
  70. }
  71. }
  72. }
  73. static inline void i2s_enable_irqs(struct dw_i2s_dev *dev, u32 stream,
  74. int chan_nr)
  75. {
  76. u32 i, irq;
  77. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  78. for (i = 0; i < (chan_nr / 2); i++) {
  79. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  80. i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x30);
  81. }
  82. } else {
  83. for (i = 0; i < (chan_nr / 2); i++) {
  84. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  85. i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x03);
  86. }
  87. }
  88. }
  89. static irqreturn_t i2s_irq_handler(int irq, void *dev_id)
  90. {
  91. struct dw_i2s_dev *dev = dev_id;
  92. bool irq_valid = false;
  93. u32 isr[4];
  94. int i;
  95. for (i = 0; i < 4; i++)
  96. isr[i] = i2s_read_reg(dev->i2s_base, ISR(i));
  97. i2s_clear_irqs(dev, SNDRV_PCM_STREAM_PLAYBACK);
  98. i2s_clear_irqs(dev, SNDRV_PCM_STREAM_CAPTURE);
  99. for (i = 0; i < 4; i++) {
  100. /*
  101. * Check if TX fifo is empty. If empty fill FIFO with samples
  102. * NOTE: Only two channels supported
  103. */
  104. if ((isr[i] & ISR_TXFE) && (i == 0) && dev->use_pio) {
  105. dw_pcm_push_tx(dev);
  106. irq_valid = true;
  107. }
  108. /* Data available. Record mode not supported in PIO mode */
  109. if (isr[i] & ISR_RXDA)
  110. irq_valid = true;
  111. /* Error Handling: TX */
  112. if (isr[i] & ISR_TXFO) {
  113. dev_err(dev->dev, "TX overrun (ch_id=%d)\n", i);
  114. irq_valid = true;
  115. }
  116. /* Error Handling: TX */
  117. if (isr[i] & ISR_RXFO) {
  118. dev_err(dev->dev, "RX overrun (ch_id=%d)\n", i);
  119. irq_valid = true;
  120. }
  121. }
  122. if (irq_valid)
  123. return IRQ_HANDLED;
  124. else
  125. return IRQ_NONE;
  126. }
  127. static void i2s_start(struct dw_i2s_dev *dev,
  128. struct snd_pcm_substream *substream)
  129. {
  130. struct i2s_clk_config_data *config = &dev->config;
  131. i2s_write_reg(dev->i2s_base, IER, 1);
  132. i2s_enable_irqs(dev, substream->stream, config->chan_nr);
  133. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  134. i2s_write_reg(dev->i2s_base, ITER, 1);
  135. else
  136. i2s_write_reg(dev->i2s_base, IRER, 1);
  137. i2s_write_reg(dev->i2s_base, CER, 1);
  138. }
  139. static void i2s_stop(struct dw_i2s_dev *dev,
  140. struct snd_pcm_substream *substream)
  141. {
  142. i2s_clear_irqs(dev, substream->stream);
  143. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  144. i2s_write_reg(dev->i2s_base, ITER, 0);
  145. else
  146. i2s_write_reg(dev->i2s_base, IRER, 0);
  147. i2s_disable_irqs(dev, substream->stream, 8);
  148. if (!dev->active) {
  149. i2s_write_reg(dev->i2s_base, CER, 0);
  150. i2s_write_reg(dev->i2s_base, IER, 0);
  151. }
  152. }
  153. static int dw_i2s_startup(struct snd_pcm_substream *substream,
  154. struct snd_soc_dai *cpu_dai)
  155. {
  156. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  157. union dw_i2s_snd_dma_data *dma_data = NULL;
  158. if (!(dev->capability & DWC_I2S_RECORD) &&
  159. (substream->stream == SNDRV_PCM_STREAM_CAPTURE))
  160. return -EINVAL;
  161. if (!(dev->capability & DWC_I2S_PLAY) &&
  162. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  163. return -EINVAL;
  164. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  165. dma_data = &dev->play_dma_data;
  166. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  167. dma_data = &dev->capture_dma_data;
  168. snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)dma_data);
  169. return 0;
  170. }
  171. static void dw_i2s_config(struct dw_i2s_dev *dev, int stream)
  172. {
  173. u32 ch_reg;
  174. struct i2s_clk_config_data *config = &dev->config;
  175. i2s_disable_channels(dev, stream);
  176. for (ch_reg = 0; ch_reg < (config->chan_nr / 2); ch_reg++) {
  177. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  178. i2s_write_reg(dev->i2s_base, TCR(ch_reg),
  179. dev->xfer_resolution);
  180. i2s_write_reg(dev->i2s_base, TFCR(ch_reg),
  181. dev->fifo_th - 1);
  182. i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
  183. } else {
  184. i2s_write_reg(dev->i2s_base, RCR(ch_reg),
  185. dev->xfer_resolution);
  186. i2s_write_reg(dev->i2s_base, RFCR(ch_reg),
  187. dev->fifo_th - 1);
  188. i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
  189. }
  190. }
  191. }
  192. static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
  193. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  194. {
  195. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  196. struct i2s_clk_config_data *config = &dev->config;
  197. int ret;
  198. switch (params_format(params)) {
  199. case SNDRV_PCM_FORMAT_S16_LE:
  200. config->data_width = 16;
  201. dev->ccr = 0x00;
  202. dev->xfer_resolution = 0x02;
  203. break;
  204. case SNDRV_PCM_FORMAT_S24_LE:
  205. config->data_width = 24;
  206. dev->ccr = 0x08;
  207. dev->xfer_resolution = 0x04;
  208. break;
  209. case SNDRV_PCM_FORMAT_S32_LE:
  210. config->data_width = 32;
  211. dev->ccr = 0x10;
  212. dev->xfer_resolution = 0x05;
  213. break;
  214. default:
  215. dev_err(dev->dev, "designware-i2s: unsupported PCM fmt");
  216. return -EINVAL;
  217. }
  218. config->chan_nr = params_channels(params);
  219. switch (config->chan_nr) {
  220. case EIGHT_CHANNEL_SUPPORT:
  221. case SIX_CHANNEL_SUPPORT:
  222. case FOUR_CHANNEL_SUPPORT:
  223. case TWO_CHANNEL_SUPPORT:
  224. break;
  225. default:
  226. dev_err(dev->dev, "channel not supported\n");
  227. return -EINVAL;
  228. }
  229. dw_i2s_config(dev, substream->stream);
  230. i2s_write_reg(dev->i2s_base, CCR, dev->ccr);
  231. config->sample_rate = params_rate(params);
  232. if (dev->capability & DW_I2S_MASTER) {
  233. if (dev->i2s_clk_cfg) {
  234. ret = dev->i2s_clk_cfg(config);
  235. if (ret < 0) {
  236. dev_err(dev->dev, "runtime audio clk config fail\n");
  237. return ret;
  238. }
  239. } else {
  240. u32 bitclk = config->sample_rate *
  241. config->data_width * 2;
  242. ret = clk_set_rate(dev->clk, bitclk);
  243. if (ret) {
  244. dev_err(dev->dev, "Can't set I2S clock rate: %d\n",
  245. ret);
  246. return ret;
  247. }
  248. }
  249. }
  250. return 0;
  251. }
  252. static void dw_i2s_shutdown(struct snd_pcm_substream *substream,
  253. struct snd_soc_dai *dai)
  254. {
  255. snd_soc_dai_set_dma_data(dai, substream, NULL);
  256. }
  257. static int dw_i2s_prepare(struct snd_pcm_substream *substream,
  258. struct snd_soc_dai *dai)
  259. {
  260. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  261. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  262. i2s_write_reg(dev->i2s_base, TXFFR, 1);
  263. else
  264. i2s_write_reg(dev->i2s_base, RXFFR, 1);
  265. return 0;
  266. }
  267. static int dw_i2s_trigger(struct snd_pcm_substream *substream,
  268. int cmd, struct snd_soc_dai *dai)
  269. {
  270. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  271. int ret = 0;
  272. switch (cmd) {
  273. case SNDRV_PCM_TRIGGER_START:
  274. case SNDRV_PCM_TRIGGER_RESUME:
  275. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  276. dev->active++;
  277. i2s_start(dev, substream);
  278. break;
  279. case SNDRV_PCM_TRIGGER_STOP:
  280. case SNDRV_PCM_TRIGGER_SUSPEND:
  281. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  282. dev->active--;
  283. i2s_stop(dev, substream);
  284. break;
  285. default:
  286. ret = -EINVAL;
  287. break;
  288. }
  289. return ret;
  290. }
  291. static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  292. {
  293. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  294. int ret = 0;
  295. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  296. case SND_SOC_DAIFMT_CBM_CFM:
  297. if (dev->capability & DW_I2S_SLAVE)
  298. ret = 0;
  299. else
  300. ret = -EINVAL;
  301. break;
  302. case SND_SOC_DAIFMT_CBS_CFS:
  303. if (dev->capability & DW_I2S_MASTER)
  304. ret = 0;
  305. else
  306. ret = -EINVAL;
  307. break;
  308. case SND_SOC_DAIFMT_CBM_CFS:
  309. case SND_SOC_DAIFMT_CBS_CFM:
  310. ret = -EINVAL;
  311. break;
  312. default:
  313. dev_dbg(dev->dev, "dwc : Invalid master/slave format\n");
  314. ret = -EINVAL;
  315. break;
  316. }
  317. return ret;
  318. }
  319. static struct snd_soc_dai_ops dw_i2s_dai_ops = {
  320. .startup = dw_i2s_startup,
  321. .shutdown = dw_i2s_shutdown,
  322. .hw_params = dw_i2s_hw_params,
  323. .prepare = dw_i2s_prepare,
  324. .trigger = dw_i2s_trigger,
  325. .set_fmt = dw_i2s_set_fmt,
  326. };
  327. static const struct snd_soc_component_driver dw_i2s_component = {
  328. .name = "dw-i2s",
  329. };
  330. #ifdef CONFIG_PM
  331. static int dw_i2s_runtime_suspend(struct device *dev)
  332. {
  333. struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
  334. if (dw_dev->capability & DW_I2S_MASTER)
  335. clk_disable(dw_dev->clk);
  336. return 0;
  337. }
  338. static int dw_i2s_runtime_resume(struct device *dev)
  339. {
  340. struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
  341. if (dw_dev->capability & DW_I2S_MASTER)
  342. clk_enable(dw_dev->clk);
  343. return 0;
  344. }
  345. static int dw_i2s_suspend(struct snd_soc_dai *dai)
  346. {
  347. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  348. if (dev->capability & DW_I2S_MASTER)
  349. clk_disable(dev->clk);
  350. return 0;
  351. }
  352. static int dw_i2s_resume(struct snd_soc_dai *dai)
  353. {
  354. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  355. if (dev->capability & DW_I2S_MASTER)
  356. clk_enable(dev->clk);
  357. if (dai->playback_active)
  358. dw_i2s_config(dev, SNDRV_PCM_STREAM_PLAYBACK);
  359. if (dai->capture_active)
  360. dw_i2s_config(dev, SNDRV_PCM_STREAM_CAPTURE);
  361. return 0;
  362. }
  363. #else
  364. #define dw_i2s_suspend NULL
  365. #define dw_i2s_resume NULL
  366. #endif
  367. /*
  368. * The following tables allow a direct lookup of various parameters
  369. * defined in the I2S block's configuration in terms of sound system
  370. * parameters. Each table is sized to the number of entries possible
  371. * according to the number of configuration bits describing an I2S
  372. * block parameter.
  373. */
  374. /* Maximum bit resolution of a channel - not uniformly spaced */
  375. static const u32 fifo_width[COMP_MAX_WORDSIZE] = {
  376. 12, 16, 20, 24, 32, 0, 0, 0
  377. };
  378. /* Width of (DMA) bus */
  379. static const u32 bus_widths[COMP_MAX_DATA_WIDTH] = {
  380. DMA_SLAVE_BUSWIDTH_1_BYTE,
  381. DMA_SLAVE_BUSWIDTH_2_BYTES,
  382. DMA_SLAVE_BUSWIDTH_4_BYTES,
  383. DMA_SLAVE_BUSWIDTH_UNDEFINED
  384. };
  385. /* PCM format to support channel resolution */
  386. static const u32 formats[COMP_MAX_WORDSIZE] = {
  387. SNDRV_PCM_FMTBIT_S16_LE,
  388. SNDRV_PCM_FMTBIT_S16_LE,
  389. SNDRV_PCM_FMTBIT_S24_LE,
  390. SNDRV_PCM_FMTBIT_S24_LE,
  391. SNDRV_PCM_FMTBIT_S32_LE,
  392. 0,
  393. 0,
  394. 0
  395. };
  396. static int dw_configure_dai(struct dw_i2s_dev *dev,
  397. struct snd_soc_dai_driver *dw_i2s_dai,
  398. unsigned int rates)
  399. {
  400. /*
  401. * Read component parameter registers to extract
  402. * the I2S block's configuration.
  403. */
  404. u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
  405. u32 comp2 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp2);
  406. u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
  407. u32 idx;
  408. if (dev->capability & DWC_I2S_RECORD &&
  409. dev->quirks & DW_I2S_QUIRK_COMP_PARAM1)
  410. comp1 = comp1 & ~BIT(5);
  411. if (COMP1_TX_ENABLED(comp1)) {
  412. dev_dbg(dev->dev, " designware: play supported\n");
  413. idx = COMP1_TX_WORDSIZE_0(comp1);
  414. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  415. return -EINVAL;
  416. dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
  417. dw_i2s_dai->playback.channels_max =
  418. 1 << (COMP1_TX_CHANNELS(comp1) + 1);
  419. dw_i2s_dai->playback.formats = formats[idx];
  420. dw_i2s_dai->playback.rates = rates;
  421. }
  422. if (COMP1_RX_ENABLED(comp1)) {
  423. dev_dbg(dev->dev, "designware: record supported\n");
  424. idx = COMP2_RX_WORDSIZE_0(comp2);
  425. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  426. return -EINVAL;
  427. dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
  428. dw_i2s_dai->capture.channels_max =
  429. 1 << (COMP1_RX_CHANNELS(comp1) + 1);
  430. dw_i2s_dai->capture.formats = formats[idx];
  431. dw_i2s_dai->capture.rates = rates;
  432. }
  433. if (COMP1_MODE_EN(comp1)) {
  434. dev_dbg(dev->dev, "designware: i2s master mode supported\n");
  435. dev->capability |= DW_I2S_MASTER;
  436. } else {
  437. dev_dbg(dev->dev, "designware: i2s slave mode supported\n");
  438. dev->capability |= DW_I2S_SLAVE;
  439. }
  440. dev->fifo_th = fifo_depth / 2;
  441. return 0;
  442. }
  443. static int dw_configure_dai_by_pd(struct dw_i2s_dev *dev,
  444. struct snd_soc_dai_driver *dw_i2s_dai,
  445. struct resource *res,
  446. const struct i2s_platform_data *pdata)
  447. {
  448. u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
  449. u32 idx = COMP1_APB_DATA_WIDTH(comp1);
  450. int ret;
  451. if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
  452. return -EINVAL;
  453. ret = dw_configure_dai(dev, dw_i2s_dai, pdata->snd_rates);
  454. if (ret < 0)
  455. return ret;
  456. /* Set DMA slaves info */
  457. dev->play_dma_data.pd.data = pdata->play_dma_data;
  458. dev->capture_dma_data.pd.data = pdata->capture_dma_data;
  459. dev->play_dma_data.pd.addr = res->start + I2S_TXDMA;
  460. dev->capture_dma_data.pd.addr = res->start + I2S_RXDMA;
  461. dev->play_dma_data.pd.max_burst = 16;
  462. dev->capture_dma_data.pd.max_burst = 16;
  463. dev->play_dma_data.pd.addr_width = bus_widths[idx];
  464. dev->capture_dma_data.pd.addr_width = bus_widths[idx];
  465. dev->play_dma_data.pd.filter = pdata->filter;
  466. dev->capture_dma_data.pd.filter = pdata->filter;
  467. return 0;
  468. }
  469. static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
  470. struct snd_soc_dai_driver *dw_i2s_dai,
  471. struct resource *res)
  472. {
  473. u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
  474. u32 comp2 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_2);
  475. u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
  476. u32 idx = COMP1_APB_DATA_WIDTH(comp1);
  477. u32 idx2;
  478. int ret;
  479. if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
  480. return -EINVAL;
  481. ret = dw_configure_dai(dev, dw_i2s_dai, SNDRV_PCM_RATE_8000_192000);
  482. if (ret < 0)
  483. return ret;
  484. if (COMP1_TX_ENABLED(comp1)) {
  485. idx2 = COMP1_TX_WORDSIZE_0(comp1);
  486. dev->capability |= DWC_I2S_PLAY;
  487. dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
  488. dev->play_dma_data.dt.addr_width = bus_widths[idx];
  489. dev->play_dma_data.dt.fifo_size = fifo_depth *
  490. (fifo_width[idx2]) >> 8;
  491. dev->play_dma_data.dt.maxburst = 16;
  492. }
  493. if (COMP1_RX_ENABLED(comp1)) {
  494. idx2 = COMP2_RX_WORDSIZE_0(comp2);
  495. dev->capability |= DWC_I2S_RECORD;
  496. dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
  497. dev->capture_dma_data.dt.addr_width = bus_widths[idx];
  498. dev->capture_dma_data.dt.fifo_size = fifo_depth *
  499. (fifo_width[idx2] >> 8);
  500. dev->capture_dma_data.dt.maxburst = 16;
  501. }
  502. return 0;
  503. }
  504. static int dw_i2s_probe(struct platform_device *pdev)
  505. {
  506. const struct i2s_platform_data *pdata = pdev->dev.platform_data;
  507. struct dw_i2s_dev *dev;
  508. struct resource *res;
  509. int ret, irq;
  510. struct snd_soc_dai_driver *dw_i2s_dai;
  511. const char *clk_id;
  512. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  513. if (!dev) {
  514. dev_warn(&pdev->dev, "kzalloc fail\n");
  515. return -ENOMEM;
  516. }
  517. dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
  518. if (!dw_i2s_dai)
  519. return -ENOMEM;
  520. dw_i2s_dai->ops = &dw_i2s_dai_ops;
  521. dw_i2s_dai->suspend = dw_i2s_suspend;
  522. dw_i2s_dai->resume = dw_i2s_resume;
  523. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  524. dev->i2s_base = devm_ioremap_resource(&pdev->dev, res);
  525. if (IS_ERR(dev->i2s_base))
  526. return PTR_ERR(dev->i2s_base);
  527. dev->dev = &pdev->dev;
  528. irq = platform_get_irq(pdev, 0);
  529. if (irq >= 0) {
  530. ret = devm_request_irq(&pdev->dev, irq, i2s_irq_handler, 0,
  531. pdev->name, dev);
  532. if (ret < 0) {
  533. dev_err(&pdev->dev, "failed to request irq\n");
  534. return ret;
  535. }
  536. }
  537. dev->i2s_reg_comp1 = I2S_COMP_PARAM_1;
  538. dev->i2s_reg_comp2 = I2S_COMP_PARAM_2;
  539. if (pdata) {
  540. dev->capability = pdata->cap;
  541. clk_id = NULL;
  542. dev->quirks = pdata->quirks;
  543. if (dev->quirks & DW_I2S_QUIRK_COMP_REG_OFFSET) {
  544. dev->i2s_reg_comp1 = pdata->i2s_reg_comp1;
  545. dev->i2s_reg_comp2 = pdata->i2s_reg_comp2;
  546. }
  547. ret = dw_configure_dai_by_pd(dev, dw_i2s_dai, res, pdata);
  548. } else {
  549. clk_id = "i2sclk";
  550. ret = dw_configure_dai_by_dt(dev, dw_i2s_dai, res);
  551. }
  552. if (ret < 0)
  553. return ret;
  554. if (dev->capability & DW_I2S_MASTER) {
  555. if (pdata) {
  556. dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
  557. if (!dev->i2s_clk_cfg) {
  558. dev_err(&pdev->dev, "no clock configure method\n");
  559. return -ENODEV;
  560. }
  561. }
  562. dev->clk = devm_clk_get(&pdev->dev, clk_id);
  563. if (IS_ERR(dev->clk))
  564. return PTR_ERR(dev->clk);
  565. ret = clk_prepare_enable(dev->clk);
  566. if (ret < 0)
  567. return ret;
  568. }
  569. dev_set_drvdata(&pdev->dev, dev);
  570. ret = devm_snd_soc_register_component(&pdev->dev, &dw_i2s_component,
  571. dw_i2s_dai, 1);
  572. if (ret != 0) {
  573. dev_err(&pdev->dev, "not able to register dai\n");
  574. goto err_clk_disable;
  575. }
  576. if (!pdata) {
  577. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  578. if (ret == -EPROBE_DEFER) {
  579. dev_err(&pdev->dev,
  580. "failed to register PCM, deferring probe\n");
  581. return ret;
  582. } else if (ret) {
  583. dev_err(&pdev->dev,
  584. "Could not register DMA PCM: %d\n"
  585. "falling back to PIO mode\n", ret);
  586. ret = dw_pcm_register(pdev);
  587. if (ret) {
  588. dev_err(&pdev->dev,
  589. "Could not register PIO PCM: %d\n",
  590. ret);
  591. goto err_clk_disable;
  592. }
  593. }
  594. }
  595. pm_runtime_enable(&pdev->dev);
  596. return 0;
  597. err_clk_disable:
  598. if (dev->capability & DW_I2S_MASTER)
  599. clk_disable_unprepare(dev->clk);
  600. return ret;
  601. }
  602. static int dw_i2s_remove(struct platform_device *pdev)
  603. {
  604. struct dw_i2s_dev *dev = dev_get_drvdata(&pdev->dev);
  605. if (dev->capability & DW_I2S_MASTER)
  606. clk_disable_unprepare(dev->clk);
  607. pm_runtime_disable(&pdev->dev);
  608. return 0;
  609. }
  610. #ifdef CONFIG_OF
  611. static const struct of_device_id dw_i2s_of_match[] = {
  612. { .compatible = "snps,designware-i2s", },
  613. {},
  614. };
  615. MODULE_DEVICE_TABLE(of, dw_i2s_of_match);
  616. #endif
  617. static const struct dev_pm_ops dwc_pm_ops = {
  618. SET_RUNTIME_PM_OPS(dw_i2s_runtime_suspend, dw_i2s_runtime_resume, NULL)
  619. };
  620. static struct platform_driver dw_i2s_driver = {
  621. .probe = dw_i2s_probe,
  622. .remove = dw_i2s_remove,
  623. .driver = {
  624. .name = "designware-i2s",
  625. .of_match_table = of_match_ptr(dw_i2s_of_match),
  626. .pm = &dwc_pm_ops,
  627. },
  628. };
  629. module_platform_driver(dw_i2s_driver);
  630. MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
  631. MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
  632. MODULE_LICENSE("GPL");
  633. MODULE_ALIAS("platform:designware_i2s");