designware_i2s.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 <sound/designware_i2s.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/dmaengine_pcm.h>
  25. /* common register for all channel */
  26. #define IER 0x000
  27. #define IRER 0x004
  28. #define ITER 0x008
  29. #define CER 0x00C
  30. #define CCR 0x010
  31. #define RXFFR 0x014
  32. #define TXFFR 0x018
  33. /* I2STxRxRegisters for all channels */
  34. #define LRBR_LTHR(x) (0x40 * x + 0x020)
  35. #define RRBR_RTHR(x) (0x40 * x + 0x024)
  36. #define RER(x) (0x40 * x + 0x028)
  37. #define TER(x) (0x40 * x + 0x02C)
  38. #define RCR(x) (0x40 * x + 0x030)
  39. #define TCR(x) (0x40 * x + 0x034)
  40. #define ISR(x) (0x40 * x + 0x038)
  41. #define IMR(x) (0x40 * x + 0x03C)
  42. #define ROR(x) (0x40 * x + 0x040)
  43. #define TOR(x) (0x40 * x + 0x044)
  44. #define RFCR(x) (0x40 * x + 0x048)
  45. #define TFCR(x) (0x40 * x + 0x04C)
  46. #define RFF(x) (0x40 * x + 0x050)
  47. #define TFF(x) (0x40 * x + 0x054)
  48. /* I2SCOMPRegisters */
  49. #define I2S_COMP_PARAM_2 0x01F0
  50. #define I2S_COMP_PARAM_1 0x01F4
  51. #define I2S_COMP_VERSION 0x01F8
  52. #define I2S_COMP_TYPE 0x01FC
  53. /*
  54. * Component parameter register fields - define the I2S block's
  55. * configuration.
  56. */
  57. #define COMP1_TX_WORDSIZE_3(r) (((r) & GENMASK(27, 25)) >> 25)
  58. #define COMP1_TX_WORDSIZE_2(r) (((r) & GENMASK(24, 22)) >> 22)
  59. #define COMP1_TX_WORDSIZE_1(r) (((r) & GENMASK(21, 19)) >> 19)
  60. #define COMP1_TX_WORDSIZE_0(r) (((r) & GENMASK(18, 16)) >> 16)
  61. #define COMP1_TX_CHANNELS(r) (((r) & GENMASK(10, 9)) >> 9)
  62. #define COMP1_RX_CHANNELS(r) (((r) & GENMASK(8, 7)) >> 7)
  63. #define COMP1_RX_ENABLED(r) (((r) & BIT(6)) >> 6)
  64. #define COMP1_TX_ENABLED(r) (((r) & BIT(5)) >> 5)
  65. #define COMP1_MODE_EN(r) (((r) & BIT(4)) >> 4)
  66. #define COMP1_FIFO_DEPTH_GLOBAL(r) (((r) & GENMASK(3, 2)) >> 2)
  67. #define COMP1_APB_DATA_WIDTH(r) (((r) & GENMASK(1, 0)) >> 0)
  68. #define COMP2_RX_WORDSIZE_3(r) (((r) & GENMASK(12, 10)) >> 10)
  69. #define COMP2_RX_WORDSIZE_2(r) (((r) & GENMASK(9, 7)) >> 7)
  70. #define COMP2_RX_WORDSIZE_1(r) (((r) & GENMASK(5, 3)) >> 3)
  71. #define COMP2_RX_WORDSIZE_0(r) (((r) & GENMASK(2, 0)) >> 0)
  72. /* Number of entries in WORDSIZE and DATA_WIDTH parameter registers */
  73. #define COMP_MAX_WORDSIZE (1 << 3)
  74. #define COMP_MAX_DATA_WIDTH (1 << 2)
  75. #define MAX_CHANNEL_NUM 8
  76. #define MIN_CHANNEL_NUM 2
  77. union dw_i2s_snd_dma_data {
  78. struct i2s_dma_data pd;
  79. struct snd_dmaengine_dai_dma_data dt;
  80. };
  81. struct dw_i2s_dev {
  82. void __iomem *i2s_base;
  83. struct clk *clk;
  84. int active;
  85. unsigned int capability;
  86. struct device *dev;
  87. /* data related to DMA transfers b/w i2s and DMAC */
  88. union dw_i2s_snd_dma_data play_dma_data;
  89. union dw_i2s_snd_dma_data capture_dma_data;
  90. struct i2s_clk_config_data config;
  91. int (*i2s_clk_cfg)(struct i2s_clk_config_data *config);
  92. };
  93. static inline void i2s_write_reg(void __iomem *io_base, int reg, u32 val)
  94. {
  95. writel(val, io_base + reg);
  96. }
  97. static inline u32 i2s_read_reg(void __iomem *io_base, int reg)
  98. {
  99. return readl(io_base + reg);
  100. }
  101. static inline void i2s_disable_channels(struct dw_i2s_dev *dev, u32 stream)
  102. {
  103. u32 i = 0;
  104. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  105. for (i = 0; i < 4; i++)
  106. i2s_write_reg(dev->i2s_base, TER(i), 0);
  107. } else {
  108. for (i = 0; i < 4; i++)
  109. i2s_write_reg(dev->i2s_base, RER(i), 0);
  110. }
  111. }
  112. static inline void i2s_clear_irqs(struct dw_i2s_dev *dev, u32 stream)
  113. {
  114. u32 i = 0;
  115. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  116. for (i = 0; i < 4; i++)
  117. i2s_read_reg(dev->i2s_base, TOR(i));
  118. } else {
  119. for (i = 0; i < 4; i++)
  120. i2s_read_reg(dev->i2s_base, ROR(i));
  121. }
  122. }
  123. static void i2s_start(struct dw_i2s_dev *dev,
  124. struct snd_pcm_substream *substream)
  125. {
  126. u32 i, irq;
  127. i2s_write_reg(dev->i2s_base, IER, 1);
  128. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  129. for (i = 0; i < 4; i++) {
  130. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  131. i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x30);
  132. }
  133. i2s_write_reg(dev->i2s_base, ITER, 1);
  134. } else {
  135. for (i = 0; i < 4; i++) {
  136. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  137. i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x03);
  138. }
  139. i2s_write_reg(dev->i2s_base, IRER, 1);
  140. }
  141. i2s_write_reg(dev->i2s_base, CER, 1);
  142. }
  143. static void i2s_stop(struct dw_i2s_dev *dev,
  144. struct snd_pcm_substream *substream)
  145. {
  146. u32 i = 0, irq;
  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. for (i = 0; i < 4; i++) {
  151. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  152. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x30);
  153. }
  154. } else {
  155. i2s_write_reg(dev->i2s_base, IRER, 0);
  156. for (i = 0; i < 4; i++) {
  157. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  158. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x03);
  159. }
  160. }
  161. if (!dev->active) {
  162. i2s_write_reg(dev->i2s_base, CER, 0);
  163. i2s_write_reg(dev->i2s_base, IER, 0);
  164. }
  165. }
  166. static int dw_i2s_startup(struct snd_pcm_substream *substream,
  167. struct snd_soc_dai *cpu_dai)
  168. {
  169. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  170. union dw_i2s_snd_dma_data *dma_data = NULL;
  171. if (!(dev->capability & DWC_I2S_RECORD) &&
  172. (substream->stream == SNDRV_PCM_STREAM_CAPTURE))
  173. return -EINVAL;
  174. if (!(dev->capability & DWC_I2S_PLAY) &&
  175. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  176. return -EINVAL;
  177. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  178. dma_data = &dev->play_dma_data;
  179. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  180. dma_data = &dev->capture_dma_data;
  181. snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)dma_data);
  182. return 0;
  183. }
  184. static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
  185. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  186. {
  187. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  188. struct i2s_clk_config_data *config = &dev->config;
  189. u32 ccr, xfer_resolution, ch_reg, irq;
  190. int ret;
  191. switch (params_format(params)) {
  192. case SNDRV_PCM_FORMAT_S16_LE:
  193. config->data_width = 16;
  194. ccr = 0x00;
  195. xfer_resolution = 0x02;
  196. break;
  197. case SNDRV_PCM_FORMAT_S24_LE:
  198. config->data_width = 24;
  199. ccr = 0x08;
  200. xfer_resolution = 0x04;
  201. break;
  202. case SNDRV_PCM_FORMAT_S32_LE:
  203. config->data_width = 32;
  204. ccr = 0x10;
  205. xfer_resolution = 0x05;
  206. break;
  207. default:
  208. dev_err(dev->dev, "designware-i2s: unsuppted PCM fmt");
  209. return -EINVAL;
  210. }
  211. config->chan_nr = params_channels(params);
  212. switch (config->chan_nr) {
  213. case EIGHT_CHANNEL_SUPPORT:
  214. case SIX_CHANNEL_SUPPORT:
  215. case FOUR_CHANNEL_SUPPORT:
  216. case TWO_CHANNEL_SUPPORT:
  217. break;
  218. default:
  219. dev_err(dev->dev, "channel not supported\n");
  220. return -EINVAL;
  221. }
  222. i2s_disable_channels(dev, substream->stream);
  223. for (ch_reg = 0; ch_reg < (config->chan_nr / 2); ch_reg++) {
  224. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  225. i2s_write_reg(dev->i2s_base, TCR(ch_reg),
  226. xfer_resolution);
  227. i2s_write_reg(dev->i2s_base, TFCR(ch_reg), 0x02);
  228. irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
  229. i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x30);
  230. i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
  231. } else {
  232. i2s_write_reg(dev->i2s_base, RCR(ch_reg),
  233. xfer_resolution);
  234. i2s_write_reg(dev->i2s_base, RFCR(ch_reg), 0x07);
  235. irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
  236. i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x03);
  237. i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
  238. }
  239. }
  240. i2s_write_reg(dev->i2s_base, CCR, ccr);
  241. config->sample_rate = params_rate(params);
  242. if (dev->i2s_clk_cfg) {
  243. ret = dev->i2s_clk_cfg(config);
  244. if (ret < 0) {
  245. dev_err(dev->dev, "runtime audio clk config fail\n");
  246. return ret;
  247. }
  248. } else {
  249. u32 bitclk = config->sample_rate * config->data_width * 2;
  250. ret = clk_set_rate(dev->clk, bitclk);
  251. if (ret) {
  252. dev_err(dev->dev, "Can't set I2S clock rate: %d\n",
  253. ret);
  254. return ret;
  255. }
  256. }
  257. return 0;
  258. }
  259. static void dw_i2s_shutdown(struct snd_pcm_substream *substream,
  260. struct snd_soc_dai *dai)
  261. {
  262. snd_soc_dai_set_dma_data(dai, substream, NULL);
  263. }
  264. static int dw_i2s_prepare(struct snd_pcm_substream *substream,
  265. struct snd_soc_dai *dai)
  266. {
  267. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  268. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  269. i2s_write_reg(dev->i2s_base, TXFFR, 1);
  270. else
  271. i2s_write_reg(dev->i2s_base, RXFFR, 1);
  272. return 0;
  273. }
  274. static int dw_i2s_trigger(struct snd_pcm_substream *substream,
  275. int cmd, struct snd_soc_dai *dai)
  276. {
  277. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  278. int ret = 0;
  279. switch (cmd) {
  280. case SNDRV_PCM_TRIGGER_START:
  281. case SNDRV_PCM_TRIGGER_RESUME:
  282. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  283. dev->active++;
  284. i2s_start(dev, substream);
  285. break;
  286. case SNDRV_PCM_TRIGGER_STOP:
  287. case SNDRV_PCM_TRIGGER_SUSPEND:
  288. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  289. dev->active--;
  290. i2s_stop(dev, substream);
  291. break;
  292. default:
  293. ret = -EINVAL;
  294. break;
  295. }
  296. return ret;
  297. }
  298. static struct snd_soc_dai_ops dw_i2s_dai_ops = {
  299. .startup = dw_i2s_startup,
  300. .shutdown = dw_i2s_shutdown,
  301. .hw_params = dw_i2s_hw_params,
  302. .prepare = dw_i2s_prepare,
  303. .trigger = dw_i2s_trigger,
  304. };
  305. static const struct snd_soc_component_driver dw_i2s_component = {
  306. .name = "dw-i2s",
  307. };
  308. #ifdef CONFIG_PM
  309. static int dw_i2s_suspend(struct snd_soc_dai *dai)
  310. {
  311. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  312. clk_disable(dev->clk);
  313. return 0;
  314. }
  315. static int dw_i2s_resume(struct snd_soc_dai *dai)
  316. {
  317. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  318. clk_enable(dev->clk);
  319. return 0;
  320. }
  321. #else
  322. #define dw_i2s_suspend NULL
  323. #define dw_i2s_resume NULL
  324. #endif
  325. /*
  326. * The following tables allow a direct lookup of various parameters
  327. * defined in the I2S block's configuration in terms of sound system
  328. * parameters. Each table is sized to the number of entries possible
  329. * according to the number of configuration bits describing an I2S
  330. * block parameter.
  331. */
  332. /* Maximum bit resolution of a channel - not uniformly spaced */
  333. static const u32 fifo_width[COMP_MAX_WORDSIZE] = {
  334. 12, 16, 20, 24, 32, 0, 0, 0
  335. };
  336. /* Width of (DMA) bus */
  337. static const u32 bus_widths[COMP_MAX_DATA_WIDTH] = {
  338. DMA_SLAVE_BUSWIDTH_1_BYTE,
  339. DMA_SLAVE_BUSWIDTH_2_BYTES,
  340. DMA_SLAVE_BUSWIDTH_4_BYTES,
  341. DMA_SLAVE_BUSWIDTH_UNDEFINED
  342. };
  343. /* PCM format to support channel resolution */
  344. static const u32 formats[COMP_MAX_WORDSIZE] = {
  345. SNDRV_PCM_FMTBIT_S16_LE,
  346. SNDRV_PCM_FMTBIT_S16_LE,
  347. SNDRV_PCM_FMTBIT_S24_LE,
  348. SNDRV_PCM_FMTBIT_S24_LE,
  349. SNDRV_PCM_FMTBIT_S32_LE,
  350. 0,
  351. 0,
  352. 0
  353. };
  354. static int dw_configure_dai(struct dw_i2s_dev *dev,
  355. struct snd_soc_dai_driver *dw_i2s_dai,
  356. unsigned int rates)
  357. {
  358. /*
  359. * Read component parameter registers to extract
  360. * the I2S block's configuration.
  361. */
  362. u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
  363. u32 comp2 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_2);
  364. u32 idx;
  365. if (COMP1_TX_ENABLED(comp1)) {
  366. dev_dbg(dev->dev, " designware: play supported\n");
  367. idx = COMP1_TX_WORDSIZE_0(comp1);
  368. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  369. return -EINVAL;
  370. dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
  371. dw_i2s_dai->playback.channels_max =
  372. 1 << (COMP1_TX_CHANNELS(comp1) + 1);
  373. dw_i2s_dai->playback.formats = formats[idx];
  374. dw_i2s_dai->playback.rates = rates;
  375. }
  376. if (COMP1_RX_ENABLED(comp1)) {
  377. dev_dbg(dev->dev, "designware: record supported\n");
  378. idx = COMP2_RX_WORDSIZE_0(comp2);
  379. if (WARN_ON(idx >= ARRAY_SIZE(formats)))
  380. return -EINVAL;
  381. dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
  382. dw_i2s_dai->capture.channels_max =
  383. 1 << (COMP1_RX_CHANNELS(comp1) + 1);
  384. dw_i2s_dai->capture.formats = formats[idx];
  385. dw_i2s_dai->capture.rates = rates;
  386. }
  387. return 0;
  388. }
  389. static int dw_configure_dai_by_pd(struct dw_i2s_dev *dev,
  390. struct snd_soc_dai_driver *dw_i2s_dai,
  391. struct resource *res,
  392. const struct i2s_platform_data *pdata)
  393. {
  394. u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
  395. u32 idx = COMP1_APB_DATA_WIDTH(comp1);
  396. int ret;
  397. if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
  398. return -EINVAL;
  399. ret = dw_configure_dai(dev, dw_i2s_dai, pdata->snd_rates);
  400. if (ret < 0)
  401. return ret;
  402. /* Set DMA slaves info */
  403. dev->play_dma_data.pd.data = pdata->play_dma_data;
  404. dev->capture_dma_data.pd.data = pdata->capture_dma_data;
  405. dev->play_dma_data.pd.addr = res->start + I2S_TXDMA;
  406. dev->capture_dma_data.pd.addr = res->start + I2S_RXDMA;
  407. dev->play_dma_data.pd.max_burst = 16;
  408. dev->capture_dma_data.pd.max_burst = 16;
  409. dev->play_dma_data.pd.addr_width = bus_widths[idx];
  410. dev->capture_dma_data.pd.addr_width = bus_widths[idx];
  411. dev->play_dma_data.pd.filter = pdata->filter;
  412. dev->capture_dma_data.pd.filter = pdata->filter;
  413. return 0;
  414. }
  415. static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
  416. struct snd_soc_dai_driver *dw_i2s_dai,
  417. struct resource *res)
  418. {
  419. u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
  420. u32 comp2 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_2);
  421. u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
  422. u32 idx = COMP1_APB_DATA_WIDTH(comp1);
  423. u32 idx2;
  424. int ret;
  425. if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
  426. return -EINVAL;
  427. ret = dw_configure_dai(dev, dw_i2s_dai, SNDRV_PCM_RATE_8000_192000);
  428. if (ret < 0)
  429. return ret;
  430. if (COMP1_TX_ENABLED(comp1)) {
  431. idx2 = COMP1_TX_WORDSIZE_0(comp1);
  432. dev->capability |= DWC_I2S_PLAY;
  433. dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
  434. dev->play_dma_data.dt.addr_width = bus_widths[idx];
  435. dev->play_dma_data.dt.chan_name = "TX";
  436. dev->play_dma_data.dt.fifo_size = fifo_depth *
  437. (fifo_width[idx2]) >> 8;
  438. dev->play_dma_data.dt.maxburst = 16;
  439. }
  440. if (COMP1_RX_ENABLED(comp1)) {
  441. idx2 = COMP2_RX_WORDSIZE_0(comp2);
  442. dev->capability |= DWC_I2S_RECORD;
  443. dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
  444. dev->capture_dma_data.dt.addr_width = bus_widths[idx];
  445. dev->capture_dma_data.dt.chan_name = "RX";
  446. dev->capture_dma_data.dt.fifo_size = fifo_depth *
  447. (fifo_width[idx2] >> 8);
  448. dev->capture_dma_data.dt.maxburst = 16;
  449. }
  450. return 0;
  451. }
  452. static int dw_i2s_probe(struct platform_device *pdev)
  453. {
  454. const struct i2s_platform_data *pdata = pdev->dev.platform_data;
  455. struct dw_i2s_dev *dev;
  456. struct resource *res;
  457. int ret;
  458. struct snd_soc_dai_driver *dw_i2s_dai;
  459. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  460. if (!dev) {
  461. dev_warn(&pdev->dev, "kzalloc fail\n");
  462. return -ENOMEM;
  463. }
  464. dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
  465. if (!dw_i2s_dai)
  466. return -ENOMEM;
  467. dw_i2s_dai->ops = &dw_i2s_dai_ops;
  468. dw_i2s_dai->suspend = dw_i2s_suspend;
  469. dw_i2s_dai->resume = dw_i2s_resume;
  470. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  471. dev->i2s_base = devm_ioremap_resource(&pdev->dev, res);
  472. if (IS_ERR(dev->i2s_base))
  473. return PTR_ERR(dev->i2s_base);
  474. dev->dev = &pdev->dev;
  475. if (pdata) {
  476. ret = dw_configure_dai_by_pd(dev, dw_i2s_dai, res, pdata);
  477. if (ret < 0)
  478. return ret;
  479. dev->capability = pdata->cap;
  480. dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
  481. if (!dev->i2s_clk_cfg) {
  482. dev_err(&pdev->dev, "no clock configure method\n");
  483. return -ENODEV;
  484. }
  485. dev->clk = devm_clk_get(&pdev->dev, NULL);
  486. } else {
  487. ret = dw_configure_dai_by_dt(dev, dw_i2s_dai, res);
  488. if (ret < 0)
  489. return ret;
  490. dev->clk = devm_clk_get(&pdev->dev, "i2sclk");
  491. }
  492. if (IS_ERR(dev->clk))
  493. return PTR_ERR(dev->clk);
  494. ret = clk_prepare_enable(dev->clk);
  495. if (ret < 0)
  496. return ret;
  497. dev_set_drvdata(&pdev->dev, dev);
  498. ret = devm_snd_soc_register_component(&pdev->dev, &dw_i2s_component,
  499. dw_i2s_dai, 1);
  500. if (ret != 0) {
  501. dev_err(&pdev->dev, "not able to register dai\n");
  502. goto err_clk_disable;
  503. }
  504. if (!pdata) {
  505. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  506. if (ret) {
  507. dev_err(&pdev->dev,
  508. "Could not register PCM: %d\n", ret);
  509. goto err_clk_disable;
  510. }
  511. }
  512. return 0;
  513. err_clk_disable:
  514. clk_disable_unprepare(dev->clk);
  515. return ret;
  516. }
  517. static int dw_i2s_remove(struct platform_device *pdev)
  518. {
  519. struct dw_i2s_dev *dev = dev_get_drvdata(&pdev->dev);
  520. clk_disable_unprepare(dev->clk);
  521. return 0;
  522. }
  523. #ifdef CONFIG_OF
  524. static const struct of_device_id dw_i2s_of_match[] = {
  525. { .compatible = "snps,designware-i2s", },
  526. {},
  527. };
  528. MODULE_DEVICE_TABLE(of, dw_i2s_of_match);
  529. #endif
  530. static struct platform_driver dw_i2s_driver = {
  531. .probe = dw_i2s_probe,
  532. .remove = dw_i2s_remove,
  533. .driver = {
  534. .name = "designware-i2s",
  535. .of_match_table = of_match_ptr(dw_i2s_of_match),
  536. },
  537. };
  538. module_platform_driver(dw_i2s_driver);
  539. MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
  540. MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
  541. MODULE_LICENSE("GPL");
  542. MODULE_ALIAS("platform:designware_i2s");