dwc-i2s.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 <[email protected]>
  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. /*
  109. * Data available. Retrieve samples from FIFO
  110. * NOTE: Only two channels supported
  111. */
  112. if ((isr[i] & ISR_RXDA) && (i == 0) && dev->use_pio) {
  113. dw_pcm_pop_rx(dev);
  114. irq_valid = true;
  115. }
  116. /* Error Handling: TX */
  117. if (isr[i] & ISR_TXFO) {
  118. dev_err_ratelimited(dev->dev, "TX overrun (ch_id=%d)\n", i);
  119. irq_valid = true;
  120. }
  121. /* Error Handling: TX */
  122. if (isr[i] & ISR_RXFO) {
  123. dev_err_ratelimited(dev->dev, "RX overrun (ch_id=%d)\n", i);
  124. irq_valid = true;
  125. }
  126. }
  127. if (irq_valid)
  128. return IRQ_HANDLED;
  129. else
  130. return IRQ_NONE;
  131. }
  132. static void i2s_start(struct dw_i2s_dev *dev,
  133. struct snd_pcm_substream *substream)
  134. {
  135. struct i2s_clk_config_data *config = &dev->config;
  136. i2s_write_reg(dev->i2s_base, IER, 1);
  137. i2s_enable_irqs(dev, substream->stream, config->chan_nr);
  138. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  139. i2s_write_reg(dev->i2s_base, ITER, 1);
  140. else
  141. i2s_write_reg(dev->i2s_base, IRER, 1);
  142. i2s_write_reg(dev->i2s_base, CER, 1);
  143. }
  144. static void i2s_stop(struct dw_i2s_dev *dev,
  145. struct snd_pcm_substream *substream)
  146. {
  147. i2s_clear_irqs(dev, substream->stream);
  148. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  149. i2s_write_reg(dev->i2s_base, ITER, 0);
  150. else
  151. i2s_write_reg(dev->i2s_base, IRER, 0);
  152. i2s_disable_irqs(dev, substream->stream, 8);
  153. if (!dev->active) {
  154. i2s_write_reg(dev->i2s_base, CER, 0);
  155. i2s_write_reg(dev->i2s_base, IER, 0);
  156. }
  157. }
  158. static void dw_i2s_config(struct dw_i2s_dev *dev, int stream)
  159. {
  160. u32 ch_reg;
  161. struct i2s_clk_config_data *config = &dev->config;
  162. i2s_disable_channels(dev, stream);
  163. for (ch_reg = 0; ch_reg < (config->chan_nr / 2); ch_reg++) {
  164. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  165. i2s_write_reg(dev->i2s_base, TCR(ch_reg),
  166. dev->xfer_resolution);
  167. i2s_write_reg(dev->i2s_base, TFCR(ch_reg),
  168. dev->fifo_th - 1);
  169. i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
  170. } else {
  171. i2s_write_reg(dev->i2s_base, RCR(ch_reg),
  172. dev->xfer_resolution);
  173. i2s_write_reg(dev->i2s_base, RFCR(ch_reg),
  174. dev->fifo_th - 1);
  175. i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
  176. }
  177. }
  178. }
  179. static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
  180. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  181. {
  182. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  183. struct i2s_clk_config_data *config = &dev->config;
  184. int ret;
  185. switch (params_format(params)) {
  186. case SNDRV_PCM_FORMAT_S16_LE:
  187. config->data_width = 16;
  188. dev->ccr = 0x00;
  189. dev->xfer_resolution = 0x02;
  190. break;
  191. case SNDRV_PCM_FORMAT_S24_LE:
  192. config->data_width = 24;
  193. dev->ccr = 0x08;
  194. dev->xfer_resolution = 0x04;
  195. break;
  196. case SNDRV_PCM_FORMAT_S32_LE:
  197. config->data_width = 32;
  198. dev->ccr = 0x10;
  199. dev->xfer_resolution = 0x05;
  200. break;
  201. default:
  202. dev_err(dev->dev, "designware-i2s: unsupported PCM fmt");
  203. return -EINVAL;
  204. }
  205. config->chan_nr = params_channels(params);
  206. switch (config->chan_nr) {
  207. case EIGHT_CHANNEL_SUPPORT:
  208. case SIX_CHANNEL_SUPPORT:
  209. case FOUR_CHANNEL_SUPPORT:
  210. case TWO_CHANNEL_SUPPORT:
  211. break;
  212. default:
  213. dev_err(dev->dev, "channel not supported\n");
  214. return -EINVAL;
  215. }
  216. dw_i2s_config(dev, substream->stream);
  217. i2s_write_reg(dev->i2s_base, CCR, dev->ccr);
  218. config->sample_rate = params_rate(params);
  219. if (dev->capability & DW_I2S_MASTER) {
  220. if (dev->i2s_clk_cfg) {
  221. ret = dev->i2s_clk_cfg(config);
  222. if (ret < 0) {
  223. dev_err(dev->dev, "runtime audio clk config fail\n");
  224. return ret;
  225. }
  226. } else {
  227. u32 bitclk = config->sample_rate *
  228. config->data_width * 2;
  229. ret = clk_set_rate(dev->clk, bitclk);
  230. if (ret) {
  231. dev_err(dev->dev, "Can't set I2S clock rate: %d\n",
  232. ret);
  233. return ret;
  234. }
  235. }
  236. }
  237. return 0;
  238. }
  239. static int dw_i2s_prepare(struct snd_pcm_substream *substream,
  240. struct snd_soc_dai *dai)
  241. {
  242. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  243. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  244. i2s_write_reg(dev->i2s_base, TXFFR, 1);
  245. else
  246. i2s_write_reg(dev->i2s_base, RXFFR, 1);
  247. return 0;
  248. }
  249. static int dw_i2s_trigger(struct snd_pcm_substream *substream,
  250. int cmd, struct snd_soc_dai *dai)
  251. {
  252. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  253. int ret = 0;
  254. switch (cmd) {
  255. case SNDRV_PCM_TRIGGER_START:
  256. case SNDRV_PCM_TRIGGER_RESUME:
  257. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  258. dev->active++;
  259. i2s_start(dev, substream);
  260. break;
  261. case SNDRV_PCM_TRIGGER_STOP:
  262. case SNDRV_PCM_TRIGGER_SUSPEND:
  263. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  264. dev->active--;
  265. i2s_stop(dev, substream);
  266. break;
  267. default:
  268. ret = -EINVAL;
  269. break;
  270. }
  271. return ret;
  272. }
  273. static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  274. {
  275. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  276. int ret = 0;
  277. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  278. case SND_SOC_DAIFMT_BC_FC:
  279. if (dev->capability & DW_I2S_SLAVE)
  280. ret = 0;
  281. else
  282. ret = -EINVAL;
  283. break;
  284. case SND_SOC_DAIFMT_BP_FP:
  285. if (dev->capability & DW_I2S_MASTER)
  286. ret = 0;
  287. else
  288. ret = -EINVAL;
  289. break;
  290. case SND_SOC_DAIFMT_BC_FP:
  291. case SND_SOC_DAIFMT_BP_FC:
  292. ret = -EINVAL;
  293. break;
  294. default:
  295. dev_dbg(dev->dev, "dwc : Invalid clock provider format\n");
  296. ret = -EINVAL;
  297. break;
  298. }
  299. return ret;
  300. }
  301. static const struct snd_soc_dai_ops dw_i2s_dai_ops = {
  302. .hw_params = dw_i2s_hw_params,
  303. .prepare = dw_i2s_prepare,
  304. .trigger = dw_i2s_trigger,
  305. .set_fmt = dw_i2s_set_fmt,
  306. };
  307. #ifdef CONFIG_PM
  308. static int dw_i2s_runtime_suspend(struct device *dev)
  309. {
  310. struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
  311. if (dw_dev->capability & DW_I2S_MASTER)
  312. clk_disable(dw_dev->clk);
  313. return 0;
  314. }
  315. static int dw_i2s_runtime_resume(struct device *dev)
  316. {
  317. struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
  318. int ret;
  319. if (dw_dev->capability & DW_I2S_MASTER) {
  320. ret = clk_enable(dw_dev->clk);
  321. if (ret)
  322. return ret;
  323. }
  324. return 0;
  325. }
  326. static int dw_i2s_suspend(struct snd_soc_component *component)
  327. {
  328. struct dw_i2s_dev *dev = snd_soc_component_get_drvdata(component);
  329. if (dev->capability & DW_I2S_MASTER)
  330. clk_disable(dev->clk);
  331. return 0;
  332. }
  333. static int dw_i2s_resume(struct snd_soc_component *component)
  334. {
  335. struct dw_i2s_dev *dev = snd_soc_component_get_drvdata(component);
  336. struct snd_soc_dai *dai;
  337. int stream, ret;
  338. if (dev->capability & DW_I2S_MASTER) {
  339. ret = clk_enable(dev->clk);
  340. if (ret)
  341. return ret;
  342. }
  343. for_each_component_dais(component, dai) {
  344. for_each_pcm_streams(stream)
  345. if (snd_soc_dai_stream_active(dai, stream))
  346. dw_i2s_config(dev, stream);
  347. }
  348. return 0;
  349. }
  350. #else
  351. #define dw_i2s_suspend NULL
  352. #define dw_i2s_resume NULL
  353. #endif
  354. static const struct snd_soc_component_driver dw_i2s_component = {
  355. .name = "dw-i2s",
  356. .suspend = dw_i2s_suspend,
  357. .resume = dw_i2s_resume,
  358. .legacy_dai_naming = 1,
  359. };
  360. /*
  361. * The following tables allow a direct lookup of various parameters
  362. * defined in the I2S block's configuration in terms of sound system
  363. * parameters. Each table is sized to the number of entries possible
  364. * according to the number of configuration bits describing an I2S
  365. * block parameter.
  366. */
  367. /* Maximum bit resolution of a channel - not uniformly spaced */
  368. static const u32 fifo_width[COMP_MAX_WORDSIZE] = {
  369. 12, 16, 20, 24, 32, 0, 0, 0
  370. };
  371. /* Width of (DMA) bus */
  372. static const u32 bus_widths[COMP_MAX_DATA_WIDTH] = {
  373. DMA_SLAVE_BUSWIDTH_1_BYTE,
  374. DMA_SLAVE_BUSWIDTH_2_BYTES,
  375. DMA_SLAVE_BUSWIDTH_4_BYTES,
  376. DMA_SLAVE_BUSWIDTH_UNDEFINED
  377. };
  378. /* PCM format to support channel resolution */
  379. static const u32 formats[COMP_MAX_WORDSIZE] = {
  380. SNDRV_PCM_FMTBIT_S16_LE,
  381. SNDRV_PCM_FMTBIT_S16_LE,
  382. SNDRV_PCM_FMTBIT_S24_LE,
  383. SNDRV_PCM_FMTBIT_S24_LE,
  384. SNDRV_PCM_FMTBIT_S32_LE,
  385. 0,
  386. 0,
  387. 0
  388. };
  389. static int dw_configure_dai(struct dw_i2s_dev *dev,
  390. struct snd_soc_dai_driver *dw_i2s_dai,
  391. unsigned int rates)
  392. {
  393. /*
  394. * Read component parameter registers to extract
  395. * the I2S block's configuration.
  396. */
  397. u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
  398. u32 comp2 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp2);
  399. u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
  400. u32 idx;
  401. if (dev->capability & DWC_I2S_RECORD &&
  402. dev->quirks & DW_I2S_QUIRK_COMP_PARAM1)
  403. comp1 = comp1 & ~BIT(5);
  404. if (dev->capability & DWC_I2S_PLAY &&
  405. dev->quirks & DW_I2S_QUIRK_COMP_PARAM1)
  406. comp1 = comp1 & ~BIT(6);
  407. if (COMP1_TX_ENABLED(comp1)) {
  408. dev_dbg(dev->dev, " designware: play supported\n");
  409. idx = COMP1_TX_WORDSIZE_0(comp1);
  410. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  411. return -EINVAL;
  412. if (dev->quirks & DW_I2S_QUIRK_16BIT_IDX_OVERRIDE)
  413. idx = 1;
  414. dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
  415. dw_i2s_dai->playback.channels_max =
  416. 1 << (COMP1_TX_CHANNELS(comp1) + 1);
  417. dw_i2s_dai->playback.formats = formats[idx];
  418. dw_i2s_dai->playback.rates = rates;
  419. }
  420. if (COMP1_RX_ENABLED(comp1)) {
  421. dev_dbg(dev->dev, "designware: record supported\n");
  422. idx = COMP2_RX_WORDSIZE_0(comp2);
  423. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  424. return -EINVAL;
  425. if (dev->quirks & DW_I2S_QUIRK_16BIT_IDX_OVERRIDE)
  426. idx = 1;
  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. if (dev->quirks & DW_I2S_QUIRK_16BIT_IDX_OVERRIDE)
  457. idx = 1;
  458. /* Set DMA slaves info */
  459. dev->play_dma_data.pd.data = pdata->play_dma_data;
  460. dev->capture_dma_data.pd.data = pdata->capture_dma_data;
  461. dev->play_dma_data.pd.addr = res->start + I2S_TXDMA;
  462. dev->capture_dma_data.pd.addr = res->start + I2S_RXDMA;
  463. dev->play_dma_data.pd.max_burst = 16;
  464. dev->capture_dma_data.pd.max_burst = 16;
  465. dev->play_dma_data.pd.addr_width = bus_widths[idx];
  466. dev->capture_dma_data.pd.addr_width = bus_widths[idx];
  467. dev->play_dma_data.pd.filter = pdata->filter;
  468. dev->capture_dma_data.pd.filter = pdata->filter;
  469. return 0;
  470. }
  471. static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
  472. struct snd_soc_dai_driver *dw_i2s_dai,
  473. struct resource *res)
  474. {
  475. u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
  476. u32 comp2 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_2);
  477. u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
  478. u32 idx = COMP1_APB_DATA_WIDTH(comp1);
  479. u32 idx2;
  480. int ret;
  481. if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
  482. return -EINVAL;
  483. ret = dw_configure_dai(dev, dw_i2s_dai, SNDRV_PCM_RATE_8000_192000);
  484. if (ret < 0)
  485. return ret;
  486. if (COMP1_TX_ENABLED(comp1)) {
  487. idx2 = COMP1_TX_WORDSIZE_0(comp1);
  488. dev->capability |= DWC_I2S_PLAY;
  489. dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
  490. dev->play_dma_data.dt.addr_width = bus_widths[idx];
  491. dev->play_dma_data.dt.fifo_size = fifo_depth *
  492. (fifo_width[idx2]) >> 8;
  493. dev->play_dma_data.dt.maxburst = 16;
  494. }
  495. if (COMP1_RX_ENABLED(comp1)) {
  496. idx2 = COMP2_RX_WORDSIZE_0(comp2);
  497. dev->capability |= DWC_I2S_RECORD;
  498. dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
  499. dev->capture_dma_data.dt.addr_width = bus_widths[idx];
  500. dev->capture_dma_data.dt.fifo_size = fifo_depth *
  501. (fifo_width[idx2] >> 8);
  502. dev->capture_dma_data.dt.maxburst = 16;
  503. }
  504. return 0;
  505. }
  506. static int dw_i2s_dai_probe(struct snd_soc_dai *dai)
  507. {
  508. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  509. snd_soc_dai_init_dma_data(dai, &dev->play_dma_data, &dev->capture_dma_data);
  510. return 0;
  511. }
  512. static int dw_i2s_probe(struct platform_device *pdev)
  513. {
  514. const struct i2s_platform_data *pdata = pdev->dev.platform_data;
  515. struct dw_i2s_dev *dev;
  516. struct resource *res;
  517. int ret, irq;
  518. struct snd_soc_dai_driver *dw_i2s_dai;
  519. const char *clk_id;
  520. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  521. if (!dev)
  522. return -ENOMEM;
  523. dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
  524. if (!dw_i2s_dai)
  525. return -ENOMEM;
  526. dw_i2s_dai->ops = &dw_i2s_dai_ops;
  527. dw_i2s_dai->probe = dw_i2s_dai_probe;
  528. dev->i2s_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  529. if (IS_ERR(dev->i2s_base))
  530. return PTR_ERR(dev->i2s_base);
  531. dev->dev = &pdev->dev;
  532. irq = platform_get_irq_optional(pdev, 0);
  533. if (irq >= 0) {
  534. ret = devm_request_irq(&pdev->dev, irq, i2s_irq_handler, 0,
  535. pdev->name, dev);
  536. if (ret < 0) {
  537. dev_err(&pdev->dev, "failed to request irq\n");
  538. return ret;
  539. }
  540. }
  541. dev->i2s_reg_comp1 = I2S_COMP_PARAM_1;
  542. dev->i2s_reg_comp2 = I2S_COMP_PARAM_2;
  543. if (pdata) {
  544. dev->capability = pdata->cap;
  545. clk_id = NULL;
  546. dev->quirks = pdata->quirks;
  547. if (dev->quirks & DW_I2S_QUIRK_COMP_REG_OFFSET) {
  548. dev->i2s_reg_comp1 = pdata->i2s_reg_comp1;
  549. dev->i2s_reg_comp2 = pdata->i2s_reg_comp2;
  550. }
  551. ret = dw_configure_dai_by_pd(dev, dw_i2s_dai, res, pdata);
  552. } else {
  553. clk_id = "i2sclk";
  554. ret = dw_configure_dai_by_dt(dev, dw_i2s_dai, res);
  555. }
  556. if (ret < 0)
  557. return ret;
  558. if (dev->capability & DW_I2S_MASTER) {
  559. if (pdata) {
  560. dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
  561. if (!dev->i2s_clk_cfg) {
  562. dev_err(&pdev->dev, "no clock configure method\n");
  563. return -ENODEV;
  564. }
  565. }
  566. dev->clk = devm_clk_get(&pdev->dev, clk_id);
  567. if (IS_ERR(dev->clk))
  568. return PTR_ERR(dev->clk);
  569. ret = clk_prepare_enable(dev->clk);
  570. if (ret < 0)
  571. return ret;
  572. }
  573. dev_set_drvdata(&pdev->dev, dev);
  574. ret = devm_snd_soc_register_component(&pdev->dev, &dw_i2s_component,
  575. dw_i2s_dai, 1);
  576. if (ret != 0) {
  577. dev_err(&pdev->dev, "not able to register dai\n");
  578. goto err_clk_disable;
  579. }
  580. if (!pdata) {
  581. if (irq >= 0) {
  582. ret = dw_pcm_register(pdev);
  583. dev->use_pio = true;
  584. } else {
  585. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL,
  586. 0);
  587. dev->use_pio = false;
  588. }
  589. if (ret) {
  590. dev_err(&pdev->dev, "could not register pcm: %d\n",
  591. ret);
  592. goto err_clk_disable;
  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 <[email protected]>");
  631. MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
  632. MODULE_LICENSE("GPL");
  633. MODULE_ALIAS("platform:designware_i2s");