designware_i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * ALSA SoC Synopsys I2S Audio Layer
  3. *
  4. * sound/soc/spear/designware_i2s.c
  5. *
  6. * Copyright (C) 2010 ST Microelectronics
  7. * Rajeev Kumar <rajeev-dlh.kumar@st.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. /* common register for all channel */
  25. #define IER 0x000
  26. #define IRER 0x004
  27. #define ITER 0x008
  28. #define CER 0x00C
  29. #define CCR 0x010
  30. #define RXFFR 0x014
  31. #define TXFFR 0x018
  32. /* I2STxRxRegisters for all channels */
  33. #define LRBR_LTHR(x) (0x40 * x + 0x020)
  34. #define RRBR_RTHR(x) (0x40 * x + 0x024)
  35. #define RER(x) (0x40 * x + 0x028)
  36. #define TER(x) (0x40 * x + 0x02C)
  37. #define RCR(x) (0x40 * x + 0x030)
  38. #define TCR(x) (0x40 * x + 0x034)
  39. #define ISR(x) (0x40 * x + 0x038)
  40. #define IMR(x) (0x40 * x + 0x03C)
  41. #define ROR(x) (0x40 * x + 0x040)
  42. #define TOR(x) (0x40 * x + 0x044)
  43. #define RFCR(x) (0x40 * x + 0x048)
  44. #define TFCR(x) (0x40 * x + 0x04C)
  45. #define RFF(x) (0x40 * x + 0x050)
  46. #define TFF(x) (0x40 * x + 0x054)
  47. /* I2SCOMPRegisters */
  48. #define I2S_COMP_PARAM_2 0x01F0
  49. #define I2S_COMP_PARAM_1 0x01F4
  50. #define I2S_COMP_VERSION 0x01F8
  51. #define I2S_COMP_TYPE 0x01FC
  52. #define MAX_CHANNEL_NUM 8
  53. #define MIN_CHANNEL_NUM 2
  54. struct dw_i2s_dev {
  55. void __iomem *i2s_base;
  56. struct clk *clk;
  57. int active;
  58. unsigned int capability;
  59. struct device *dev;
  60. /* data related to DMA transfers b/w i2s and DMAC */
  61. struct i2s_dma_data play_dma_data;
  62. struct i2s_dma_data capture_dma_data;
  63. struct i2s_clk_config_data config;
  64. int (*i2s_clk_cfg)(struct i2s_clk_config_data *config);
  65. };
  66. static inline void i2s_write_reg(void __iomem *io_base, int reg, u32 val)
  67. {
  68. writel(val, io_base + reg);
  69. }
  70. static inline u32 i2s_read_reg(void __iomem *io_base, int reg)
  71. {
  72. return readl(io_base + reg);
  73. }
  74. static inline void i2s_disable_channels(struct dw_i2s_dev *dev, u32 stream)
  75. {
  76. u32 i = 0;
  77. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  78. for (i = 0; i < 4; i++)
  79. i2s_write_reg(dev->i2s_base, TER(i), 0);
  80. } else {
  81. for (i = 0; i < 4; i++)
  82. i2s_write_reg(dev->i2s_base, RER(i), 0);
  83. }
  84. }
  85. static inline void i2s_clear_irqs(struct dw_i2s_dev *dev, u32 stream)
  86. {
  87. u32 i = 0;
  88. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  89. for (i = 0; i < 4; i++)
  90. i2s_write_reg(dev->i2s_base, TOR(i), 0);
  91. } else {
  92. for (i = 0; i < 4; i++)
  93. i2s_write_reg(dev->i2s_base, ROR(i), 0);
  94. }
  95. }
  96. static void i2s_start(struct dw_i2s_dev *dev,
  97. struct snd_pcm_substream *substream)
  98. {
  99. i2s_write_reg(dev->i2s_base, IER, 1);
  100. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  101. i2s_write_reg(dev->i2s_base, ITER, 1);
  102. else
  103. i2s_write_reg(dev->i2s_base, IRER, 1);
  104. i2s_write_reg(dev->i2s_base, CER, 1);
  105. }
  106. static void i2s_stop(struct dw_i2s_dev *dev,
  107. struct snd_pcm_substream *substream)
  108. {
  109. u32 i = 0, irq;
  110. i2s_clear_irqs(dev, substream->stream);
  111. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  112. i2s_write_reg(dev->i2s_base, ITER, 0);
  113. for (i = 0; i < 4; i++) {
  114. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  115. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x30);
  116. }
  117. } else {
  118. i2s_write_reg(dev->i2s_base, IRER, 0);
  119. for (i = 0; i < 4; i++) {
  120. irq = i2s_read_reg(dev->i2s_base, IMR(i));
  121. i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x03);
  122. }
  123. }
  124. if (!dev->active) {
  125. i2s_write_reg(dev->i2s_base, CER, 0);
  126. i2s_write_reg(dev->i2s_base, IER, 0);
  127. }
  128. }
  129. static int dw_i2s_startup(struct snd_pcm_substream *substream,
  130. struct snd_soc_dai *cpu_dai)
  131. {
  132. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  133. struct i2s_dma_data *dma_data = NULL;
  134. if (!(dev->capability & DWC_I2S_RECORD) &&
  135. (substream->stream == SNDRV_PCM_STREAM_CAPTURE))
  136. return -EINVAL;
  137. if (!(dev->capability & DWC_I2S_PLAY) &&
  138. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  139. return -EINVAL;
  140. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  141. dma_data = &dev->play_dma_data;
  142. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  143. dma_data = &dev->capture_dma_data;
  144. snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)dma_data);
  145. return 0;
  146. }
  147. static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
  148. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  149. {
  150. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  151. struct i2s_clk_config_data *config = &dev->config;
  152. u32 ccr, xfer_resolution, ch_reg, irq;
  153. int ret;
  154. switch (params_format(params)) {
  155. case SNDRV_PCM_FORMAT_S16_LE:
  156. config->data_width = 16;
  157. ccr = 0x00;
  158. xfer_resolution = 0x02;
  159. break;
  160. case SNDRV_PCM_FORMAT_S24_LE:
  161. config->data_width = 24;
  162. ccr = 0x08;
  163. xfer_resolution = 0x04;
  164. break;
  165. case SNDRV_PCM_FORMAT_S32_LE:
  166. config->data_width = 32;
  167. ccr = 0x10;
  168. xfer_resolution = 0x05;
  169. break;
  170. default:
  171. dev_err(dev->dev, "designware-i2s: unsuppted PCM fmt");
  172. return -EINVAL;
  173. }
  174. config->chan_nr = params_channels(params);
  175. switch (config->chan_nr) {
  176. case EIGHT_CHANNEL_SUPPORT:
  177. ch_reg = 3;
  178. break;
  179. case SIX_CHANNEL_SUPPORT:
  180. ch_reg = 2;
  181. break;
  182. case FOUR_CHANNEL_SUPPORT:
  183. ch_reg = 1;
  184. break;
  185. case TWO_CHANNEL_SUPPORT:
  186. ch_reg = 0;
  187. break;
  188. default:
  189. dev_err(dev->dev, "channel not supported\n");
  190. return -EINVAL;
  191. }
  192. i2s_disable_channels(dev, substream->stream);
  193. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  194. i2s_write_reg(dev->i2s_base, TCR(ch_reg), xfer_resolution);
  195. i2s_write_reg(dev->i2s_base, TFCR(ch_reg), 0x02);
  196. irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
  197. i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x30);
  198. i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
  199. } else {
  200. i2s_write_reg(dev->i2s_base, RCR(ch_reg), xfer_resolution);
  201. i2s_write_reg(dev->i2s_base, RFCR(ch_reg), 0x07);
  202. irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
  203. i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x03);
  204. i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
  205. }
  206. i2s_write_reg(dev->i2s_base, CCR, ccr);
  207. config->sample_rate = params_rate(params);
  208. if (!dev->i2s_clk_cfg)
  209. return -EINVAL;
  210. ret = dev->i2s_clk_cfg(config);
  211. if (ret < 0) {
  212. dev_err(dev->dev, "runtime audio clk config fail\n");
  213. return ret;
  214. }
  215. return 0;
  216. }
  217. static void dw_i2s_shutdown(struct snd_pcm_substream *substream,
  218. struct snd_soc_dai *dai)
  219. {
  220. snd_soc_dai_set_dma_data(dai, substream, NULL);
  221. }
  222. static int dw_i2s_trigger(struct snd_pcm_substream *substream,
  223. int cmd, struct snd_soc_dai *dai)
  224. {
  225. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  226. int ret = 0;
  227. switch (cmd) {
  228. case SNDRV_PCM_TRIGGER_START:
  229. case SNDRV_PCM_TRIGGER_RESUME:
  230. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  231. dev->active++;
  232. i2s_start(dev, substream);
  233. break;
  234. case SNDRV_PCM_TRIGGER_STOP:
  235. case SNDRV_PCM_TRIGGER_SUSPEND:
  236. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  237. dev->active--;
  238. i2s_stop(dev, substream);
  239. break;
  240. default:
  241. ret = -EINVAL;
  242. break;
  243. }
  244. return ret;
  245. }
  246. static struct snd_soc_dai_ops dw_i2s_dai_ops = {
  247. .startup = dw_i2s_startup,
  248. .shutdown = dw_i2s_shutdown,
  249. .hw_params = dw_i2s_hw_params,
  250. .trigger = dw_i2s_trigger,
  251. };
  252. #ifdef CONFIG_PM
  253. static int dw_i2s_suspend(struct snd_soc_dai *dai)
  254. {
  255. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  256. clk_disable(dev->clk);
  257. return 0;
  258. }
  259. static int dw_i2s_resume(struct snd_soc_dai *dai)
  260. {
  261. struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  262. clk_enable(dev->clk);
  263. return 0;
  264. }
  265. #else
  266. #define dw_i2s_suspend NULL
  267. #define dw_i2s_resume NULL
  268. #endif
  269. static int dw_i2s_probe(struct platform_device *pdev)
  270. {
  271. const struct i2s_platform_data *pdata = pdev->dev.platform_data;
  272. struct dw_i2s_dev *dev;
  273. struct resource *res;
  274. int ret;
  275. unsigned int cap;
  276. struct snd_soc_dai_driver *dw_i2s_dai;
  277. if (!pdata) {
  278. dev_err(&pdev->dev, "Invalid platform data\n");
  279. return -EINVAL;
  280. }
  281. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  282. if (!res) {
  283. dev_err(&pdev->dev, "no i2s resource defined\n");
  284. return -ENODEV;
  285. }
  286. if (!devm_request_mem_region(&pdev->dev, res->start,
  287. resource_size(res), pdev->name)) {
  288. dev_err(&pdev->dev, "i2s region already claimed\n");
  289. return -EBUSY;
  290. }
  291. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  292. if (!dev) {
  293. dev_warn(&pdev->dev, "kzalloc fail\n");
  294. return -ENOMEM;
  295. }
  296. dev->i2s_base = devm_ioremap(&pdev->dev, res->start,
  297. resource_size(res));
  298. if (!dev->i2s_base) {
  299. dev_err(&pdev->dev, "ioremap fail for i2s_region\n");
  300. return -ENOMEM;
  301. }
  302. cap = pdata->cap;
  303. dev->capability = cap;
  304. dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
  305. /* Set DMA slaves info */
  306. dev->play_dma_data.data = pdata->play_dma_data;
  307. dev->capture_dma_data.data = pdata->capture_dma_data;
  308. dev->play_dma_data.addr = res->start + I2S_TXDMA;
  309. dev->capture_dma_data.addr = res->start + I2S_RXDMA;
  310. dev->play_dma_data.max_burst = 16;
  311. dev->capture_dma_data.max_burst = 16;
  312. dev->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  313. dev->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  314. dev->play_dma_data.filter = pdata->filter;
  315. dev->capture_dma_data.filter = pdata->filter;
  316. dev->clk = clk_get(&pdev->dev, NULL);
  317. if (IS_ERR(dev->clk))
  318. return PTR_ERR(dev->clk);
  319. ret = clk_enable(dev->clk);
  320. if (ret < 0)
  321. goto err_clk_put;
  322. dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
  323. if (!dw_i2s_dai) {
  324. dev_err(&pdev->dev, "mem allocation failed for dai driver\n");
  325. ret = -ENOMEM;
  326. goto err_clk_disable;
  327. }
  328. if (cap & DWC_I2S_PLAY) {
  329. dev_dbg(&pdev->dev, " SPEAr: play supported\n");
  330. dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
  331. dw_i2s_dai->playback.channels_max = pdata->channel;
  332. dw_i2s_dai->playback.formats = pdata->snd_fmts;
  333. dw_i2s_dai->playback.rates = pdata->snd_rates;
  334. }
  335. if (cap & DWC_I2S_RECORD) {
  336. dev_dbg(&pdev->dev, "SPEAr: record supported\n");
  337. dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
  338. dw_i2s_dai->capture.channels_max = pdata->channel;
  339. dw_i2s_dai->capture.formats = pdata->snd_fmts;
  340. dw_i2s_dai->capture.rates = pdata->snd_rates;
  341. }
  342. dw_i2s_dai->ops = &dw_i2s_dai_ops;
  343. dw_i2s_dai->suspend = dw_i2s_suspend;
  344. dw_i2s_dai->resume = dw_i2s_resume;
  345. dev->dev = &pdev->dev;
  346. dev_set_drvdata(&pdev->dev, dev);
  347. ret = snd_soc_register_dai(&pdev->dev, dw_i2s_dai);
  348. if (ret != 0) {
  349. dev_err(&pdev->dev, "not able to register dai\n");
  350. goto err_set_drvdata;
  351. }
  352. return 0;
  353. err_set_drvdata:
  354. dev_set_drvdata(&pdev->dev, NULL);
  355. err_clk_disable:
  356. clk_disable(dev->clk);
  357. err_clk_put:
  358. clk_put(dev->clk);
  359. return ret;
  360. }
  361. static int dw_i2s_remove(struct platform_device *pdev)
  362. {
  363. struct dw_i2s_dev *dev = dev_get_drvdata(&pdev->dev);
  364. snd_soc_unregister_dai(&pdev->dev);
  365. dev_set_drvdata(&pdev->dev, NULL);
  366. clk_put(dev->clk);
  367. return 0;
  368. }
  369. static struct platform_driver dw_i2s_driver = {
  370. .probe = dw_i2s_probe,
  371. .remove = dw_i2s_remove,
  372. .driver = {
  373. .name = "designware-i2s",
  374. .owner = THIS_MODULE,
  375. },
  376. };
  377. module_platform_driver(dw_i2s_driver);
  378. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  379. MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
  380. MODULE_LICENSE("GPL");
  381. MODULE_ALIAS("platform:designware_i2s");