bcm2835-i2s.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * ALSA SoC I2S Audio Layer for Broadcom BCM2835 SoC
  3. *
  4. * Author: Florian Meier <florian.meier@koalo.de>
  5. * Copyright 2013
  6. *
  7. * Based on
  8. * Raspberry Pi PCM I2S ALSA Driver
  9. * Copyright (c) by Phil Poole 2013
  10. *
  11. * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  12. * Vladimir Barinov, <vbarinov@embeddedalley.com>
  13. * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  14. *
  15. * OMAP ALSA SoC DAI driver using McBSP port
  16. * Copyright (C) 2008 Nokia Corporation
  17. * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  18. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  19. *
  20. * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  21. * Author: Timur Tabi <timur@freescale.com>
  22. * Copyright 2007-2010 Freescale Semiconductor, Inc.
  23. *
  24. * This program is free software; you can redistribute it and/or
  25. * modify it under the terms of the GNU General Public License
  26. * version 2 as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope that it will be useful, but
  29. * WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. */
  33. #include <linux/clk.h>
  34. #include <linux/delay.h>
  35. #include <linux/device.h>
  36. #include <linux/init.h>
  37. #include <linux/io.h>
  38. #include <linux/module.h>
  39. #include <linux/of_address.h>
  40. #include <linux/slab.h>
  41. #include <sound/core.h>
  42. #include <sound/dmaengine_pcm.h>
  43. #include <sound/initval.h>
  44. #include <sound/pcm.h>
  45. #include <sound/pcm_params.h>
  46. #include <sound/soc.h>
  47. /* I2S registers */
  48. #define BCM2835_I2S_CS_A_REG 0x00
  49. #define BCM2835_I2S_FIFO_A_REG 0x04
  50. #define BCM2835_I2S_MODE_A_REG 0x08
  51. #define BCM2835_I2S_RXC_A_REG 0x0c
  52. #define BCM2835_I2S_TXC_A_REG 0x10
  53. #define BCM2835_I2S_DREQ_A_REG 0x14
  54. #define BCM2835_I2S_INTEN_A_REG 0x18
  55. #define BCM2835_I2S_INTSTC_A_REG 0x1c
  56. #define BCM2835_I2S_GRAY_REG 0x20
  57. /* I2S register settings */
  58. #define BCM2835_I2S_STBY BIT(25)
  59. #define BCM2835_I2S_SYNC BIT(24)
  60. #define BCM2835_I2S_RXSEX BIT(23)
  61. #define BCM2835_I2S_RXF BIT(22)
  62. #define BCM2835_I2S_TXE BIT(21)
  63. #define BCM2835_I2S_RXD BIT(20)
  64. #define BCM2835_I2S_TXD BIT(19)
  65. #define BCM2835_I2S_RXR BIT(18)
  66. #define BCM2835_I2S_TXW BIT(17)
  67. #define BCM2835_I2S_CS_RXERR BIT(16)
  68. #define BCM2835_I2S_CS_TXERR BIT(15)
  69. #define BCM2835_I2S_RXSYNC BIT(14)
  70. #define BCM2835_I2S_TXSYNC BIT(13)
  71. #define BCM2835_I2S_DMAEN BIT(9)
  72. #define BCM2835_I2S_RXTHR(v) ((v) << 7)
  73. #define BCM2835_I2S_TXTHR(v) ((v) << 5)
  74. #define BCM2835_I2S_RXCLR BIT(4)
  75. #define BCM2835_I2S_TXCLR BIT(3)
  76. #define BCM2835_I2S_TXON BIT(2)
  77. #define BCM2835_I2S_RXON BIT(1)
  78. #define BCM2835_I2S_EN (1)
  79. #define BCM2835_I2S_CLKDIS BIT(28)
  80. #define BCM2835_I2S_PDMN BIT(27)
  81. #define BCM2835_I2S_PDME BIT(26)
  82. #define BCM2835_I2S_FRXP BIT(25)
  83. #define BCM2835_I2S_FTXP BIT(24)
  84. #define BCM2835_I2S_CLKM BIT(23)
  85. #define BCM2835_I2S_CLKI BIT(22)
  86. #define BCM2835_I2S_FSM BIT(21)
  87. #define BCM2835_I2S_FSI BIT(20)
  88. #define BCM2835_I2S_FLEN(v) ((v) << 10)
  89. #define BCM2835_I2S_FSLEN(v) (v)
  90. #define BCM2835_I2S_CHWEX BIT(15)
  91. #define BCM2835_I2S_CHEN BIT(14)
  92. #define BCM2835_I2S_CHPOS(v) ((v) << 4)
  93. #define BCM2835_I2S_CHWID(v) (v)
  94. #define BCM2835_I2S_CH1(v) ((v) << 16)
  95. #define BCM2835_I2S_CH2(v) (v)
  96. #define BCM2835_I2S_TX_PANIC(v) ((v) << 24)
  97. #define BCM2835_I2S_RX_PANIC(v) ((v) << 16)
  98. #define BCM2835_I2S_TX(v) ((v) << 8)
  99. #define BCM2835_I2S_RX(v) (v)
  100. #define BCM2835_I2S_INT_RXERR BIT(3)
  101. #define BCM2835_I2S_INT_TXERR BIT(2)
  102. #define BCM2835_I2S_INT_RXR BIT(1)
  103. #define BCM2835_I2S_INT_TXW BIT(0)
  104. /* General device struct */
  105. struct bcm2835_i2s_dev {
  106. struct device *dev;
  107. struct snd_dmaengine_dai_dma_data dma_data[2];
  108. unsigned int fmt;
  109. unsigned int bclk_ratio;
  110. struct regmap *i2s_regmap;
  111. struct clk *clk;
  112. bool clk_prepared;
  113. };
  114. static void bcm2835_i2s_start_clock(struct bcm2835_i2s_dev *dev)
  115. {
  116. unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  117. if (dev->clk_prepared)
  118. return;
  119. switch (master) {
  120. case SND_SOC_DAIFMT_CBS_CFS:
  121. case SND_SOC_DAIFMT_CBS_CFM:
  122. clk_prepare_enable(dev->clk);
  123. dev->clk_prepared = true;
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. static void bcm2835_i2s_stop_clock(struct bcm2835_i2s_dev *dev)
  130. {
  131. if (dev->clk_prepared)
  132. clk_disable_unprepare(dev->clk);
  133. dev->clk_prepared = false;
  134. }
  135. static void bcm2835_i2s_clear_fifos(struct bcm2835_i2s_dev *dev,
  136. bool tx, bool rx)
  137. {
  138. int timeout = 1000;
  139. uint32_t syncval;
  140. uint32_t csreg;
  141. uint32_t i2s_active_state;
  142. bool clk_was_prepared;
  143. uint32_t off;
  144. uint32_t clr;
  145. off = tx ? BCM2835_I2S_TXON : 0;
  146. off |= rx ? BCM2835_I2S_RXON : 0;
  147. clr = tx ? BCM2835_I2S_TXCLR : 0;
  148. clr |= rx ? BCM2835_I2S_RXCLR : 0;
  149. /* Backup the current state */
  150. regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &csreg);
  151. i2s_active_state = csreg & (BCM2835_I2S_RXON | BCM2835_I2S_TXON);
  152. /* Start clock if not running */
  153. clk_was_prepared = dev->clk_prepared;
  154. if (!clk_was_prepared)
  155. bcm2835_i2s_start_clock(dev);
  156. /* Stop I2S module */
  157. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, off, 0);
  158. /*
  159. * Clear the FIFOs
  160. * Requires at least 2 PCM clock cycles to take effect
  161. */
  162. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, clr, clr);
  163. /* Wait for 2 PCM clock cycles */
  164. /*
  165. * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
  166. * FIXME: This does not seem to work for slave mode!
  167. */
  168. regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &syncval);
  169. syncval &= BCM2835_I2S_SYNC;
  170. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
  171. BCM2835_I2S_SYNC, ~syncval);
  172. /* Wait for the SYNC flag changing it's state */
  173. while (--timeout) {
  174. regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &csreg);
  175. if ((csreg & BCM2835_I2S_SYNC) != syncval)
  176. break;
  177. }
  178. if (!timeout)
  179. dev_err(dev->dev, "I2S SYNC error!\n");
  180. /* Stop clock if it was not running before */
  181. if (!clk_was_prepared)
  182. bcm2835_i2s_stop_clock(dev);
  183. /* Restore I2S state */
  184. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
  185. BCM2835_I2S_RXON | BCM2835_I2S_TXON, i2s_active_state);
  186. }
  187. static int bcm2835_i2s_set_dai_fmt(struct snd_soc_dai *dai,
  188. unsigned int fmt)
  189. {
  190. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  191. dev->fmt = fmt;
  192. return 0;
  193. }
  194. static int bcm2835_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  195. unsigned int ratio)
  196. {
  197. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  198. dev->bclk_ratio = ratio;
  199. return 0;
  200. }
  201. static int bcm2835_i2s_hw_params(struct snd_pcm_substream *substream,
  202. struct snd_pcm_hw_params *params,
  203. struct snd_soc_dai *dai)
  204. {
  205. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  206. unsigned int sampling_rate = params_rate(params);
  207. unsigned int data_length, data_delay, bclk_ratio;
  208. unsigned int ch1pos, ch2pos, mode, format;
  209. uint32_t csreg;
  210. /*
  211. * If a stream is already enabled,
  212. * the registers are already set properly.
  213. */
  214. regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &csreg);
  215. if (csreg & (BCM2835_I2S_TXON | BCM2835_I2S_RXON))
  216. return 0;
  217. /*
  218. * Adjust the data length according to the format.
  219. * We prefill the half frame length with an integer
  220. * divider of 2400 as explained at the clock settings.
  221. * Maybe it is overwritten there, if the Integer mode
  222. * does not apply.
  223. */
  224. switch (params_format(params)) {
  225. case SNDRV_PCM_FORMAT_S16_LE:
  226. data_length = 16;
  227. break;
  228. case SNDRV_PCM_FORMAT_S24_LE:
  229. data_length = 24;
  230. break;
  231. case SNDRV_PCM_FORMAT_S32_LE:
  232. data_length = 32;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. /* If bclk_ratio already set, use that one. */
  238. if (dev->bclk_ratio)
  239. bclk_ratio = dev->bclk_ratio;
  240. else
  241. /* otherwise calculate a fitting block ratio */
  242. bclk_ratio = 2 * data_length;
  243. /* Clock should only be set up here if CPU is clock master */
  244. switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  245. case SND_SOC_DAIFMT_CBS_CFS:
  246. case SND_SOC_DAIFMT_CBS_CFM:
  247. clk_set_rate(dev->clk, sampling_rate * bclk_ratio);
  248. break;
  249. default:
  250. break;
  251. }
  252. /* Setup the frame format */
  253. format = BCM2835_I2S_CHEN;
  254. if (data_length >= 24)
  255. format |= BCM2835_I2S_CHWEX;
  256. format |= BCM2835_I2S_CHWID((data_length-8)&0xf);
  257. switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  258. case SND_SOC_DAIFMT_I2S:
  259. data_delay = 1;
  260. break;
  261. default:
  262. /*
  263. * TODO
  264. * Others are possible but are not implemented at the moment.
  265. */
  266. dev_err(dev->dev, "%s:bad format\n", __func__);
  267. return -EINVAL;
  268. }
  269. ch1pos = data_delay;
  270. ch2pos = bclk_ratio / 2 + data_delay;
  271. switch (params_channels(params)) {
  272. case 2:
  273. format = BCM2835_I2S_CH1(format) | BCM2835_I2S_CH2(format);
  274. format |= BCM2835_I2S_CH1(BCM2835_I2S_CHPOS(ch1pos));
  275. format |= BCM2835_I2S_CH2(BCM2835_I2S_CHPOS(ch2pos));
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. /*
  281. * Set format for both streams.
  282. * We cannot set another frame length
  283. * (and therefore word length) anyway,
  284. * so the format will be the same.
  285. */
  286. regmap_write(dev->i2s_regmap, BCM2835_I2S_RXC_A_REG, format);
  287. regmap_write(dev->i2s_regmap, BCM2835_I2S_TXC_A_REG, format);
  288. /* Setup the I2S mode */
  289. mode = 0;
  290. if (data_length <= 16) {
  291. /*
  292. * Use frame packed mode (2 channels per 32 bit word)
  293. * We cannot set another frame length in the second stream
  294. * (and therefore word length) anyway,
  295. * so the format will be the same.
  296. */
  297. mode |= BCM2835_I2S_FTXP | BCM2835_I2S_FRXP;
  298. }
  299. mode |= BCM2835_I2S_FLEN(bclk_ratio - 1);
  300. mode |= BCM2835_I2S_FSLEN(bclk_ratio / 2);
  301. /* Master or slave? */
  302. switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  303. case SND_SOC_DAIFMT_CBS_CFS:
  304. /* CPU is master */
  305. break;
  306. case SND_SOC_DAIFMT_CBM_CFS:
  307. /*
  308. * CODEC is bit clock master
  309. * CPU is frame master
  310. */
  311. mode |= BCM2835_I2S_CLKM;
  312. break;
  313. case SND_SOC_DAIFMT_CBS_CFM:
  314. /*
  315. * CODEC is frame master
  316. * CPU is bit clock master
  317. */
  318. mode |= BCM2835_I2S_FSM;
  319. break;
  320. case SND_SOC_DAIFMT_CBM_CFM:
  321. /* CODEC is master */
  322. mode |= BCM2835_I2S_CLKM;
  323. mode |= BCM2835_I2S_FSM;
  324. break;
  325. default:
  326. dev_err(dev->dev, "%s:bad master\n", __func__);
  327. return -EINVAL;
  328. }
  329. /*
  330. * Invert clocks?
  331. *
  332. * The BCM approach seems to be inverted to the classical I2S approach.
  333. */
  334. switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
  335. case SND_SOC_DAIFMT_NB_NF:
  336. /* None. Therefore, both for BCM */
  337. mode |= BCM2835_I2S_CLKI;
  338. mode |= BCM2835_I2S_FSI;
  339. break;
  340. case SND_SOC_DAIFMT_IB_IF:
  341. /* Both. Therefore, none for BCM */
  342. break;
  343. case SND_SOC_DAIFMT_NB_IF:
  344. /*
  345. * Invert only frame sync. Therefore,
  346. * invert only bit clock for BCM
  347. */
  348. mode |= BCM2835_I2S_CLKI;
  349. break;
  350. case SND_SOC_DAIFMT_IB_NF:
  351. /*
  352. * Invert only bit clock. Therefore,
  353. * invert only frame sync for BCM
  354. */
  355. mode |= BCM2835_I2S_FSI;
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. regmap_write(dev->i2s_regmap, BCM2835_I2S_MODE_A_REG, mode);
  361. /* Setup the DMA parameters */
  362. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
  363. BCM2835_I2S_RXTHR(1)
  364. | BCM2835_I2S_TXTHR(1)
  365. | BCM2835_I2S_DMAEN, 0xffffffff);
  366. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_DREQ_A_REG,
  367. BCM2835_I2S_TX_PANIC(0x10)
  368. | BCM2835_I2S_RX_PANIC(0x30)
  369. | BCM2835_I2S_TX(0x30)
  370. | BCM2835_I2S_RX(0x20), 0xffffffff);
  371. /* Clear FIFOs */
  372. bcm2835_i2s_clear_fifos(dev, true, true);
  373. return 0;
  374. }
  375. static int bcm2835_i2s_prepare(struct snd_pcm_substream *substream,
  376. struct snd_soc_dai *dai)
  377. {
  378. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  379. uint32_t cs_reg;
  380. bcm2835_i2s_start_clock(dev);
  381. /*
  382. * Clear both FIFOs if the one that should be started
  383. * is not empty at the moment. This should only happen
  384. * after overrun. Otherwise, hw_params would have cleared
  385. * the FIFO.
  386. */
  387. regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &cs_reg);
  388. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  389. && !(cs_reg & BCM2835_I2S_TXE))
  390. bcm2835_i2s_clear_fifos(dev, true, false);
  391. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
  392. && (cs_reg & BCM2835_I2S_RXD))
  393. bcm2835_i2s_clear_fifos(dev, false, true);
  394. return 0;
  395. }
  396. static void bcm2835_i2s_stop(struct bcm2835_i2s_dev *dev,
  397. struct snd_pcm_substream *substream,
  398. struct snd_soc_dai *dai)
  399. {
  400. uint32_t mask;
  401. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  402. mask = BCM2835_I2S_RXON;
  403. else
  404. mask = BCM2835_I2S_TXON;
  405. regmap_update_bits(dev->i2s_regmap,
  406. BCM2835_I2S_CS_A_REG, mask, 0);
  407. /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
  408. if (!dai->active && !(dev->fmt & SND_SOC_DAIFMT_CONT))
  409. bcm2835_i2s_stop_clock(dev);
  410. }
  411. static int bcm2835_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  412. struct snd_soc_dai *dai)
  413. {
  414. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  415. uint32_t mask;
  416. switch (cmd) {
  417. case SNDRV_PCM_TRIGGER_START:
  418. case SNDRV_PCM_TRIGGER_RESUME:
  419. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  420. bcm2835_i2s_start_clock(dev);
  421. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  422. mask = BCM2835_I2S_RXON;
  423. else
  424. mask = BCM2835_I2S_TXON;
  425. regmap_update_bits(dev->i2s_regmap,
  426. BCM2835_I2S_CS_A_REG, mask, mask);
  427. break;
  428. case SNDRV_PCM_TRIGGER_STOP:
  429. case SNDRV_PCM_TRIGGER_SUSPEND:
  430. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  431. bcm2835_i2s_stop(dev, substream, dai);
  432. break;
  433. default:
  434. return -EINVAL;
  435. }
  436. return 0;
  437. }
  438. static int bcm2835_i2s_startup(struct snd_pcm_substream *substream,
  439. struct snd_soc_dai *dai)
  440. {
  441. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  442. if (dai->active)
  443. return 0;
  444. /* Should this still be running stop it */
  445. bcm2835_i2s_stop_clock(dev);
  446. /* Enable PCM block */
  447. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
  448. BCM2835_I2S_EN, BCM2835_I2S_EN);
  449. /*
  450. * Disable STBY.
  451. * Requires at least 4 PCM clock cycles to take effect.
  452. */
  453. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
  454. BCM2835_I2S_STBY, BCM2835_I2S_STBY);
  455. return 0;
  456. }
  457. static void bcm2835_i2s_shutdown(struct snd_pcm_substream *substream,
  458. struct snd_soc_dai *dai)
  459. {
  460. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  461. bcm2835_i2s_stop(dev, substream, dai);
  462. /* If both streams are stopped, disable module and clock */
  463. if (dai->active)
  464. return;
  465. /* Disable the module */
  466. regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
  467. BCM2835_I2S_EN, 0);
  468. /*
  469. * Stopping clock is necessary, because stop does
  470. * not stop the clock when SND_SOC_DAIFMT_CONT
  471. */
  472. bcm2835_i2s_stop_clock(dev);
  473. }
  474. static const struct snd_soc_dai_ops bcm2835_i2s_dai_ops = {
  475. .startup = bcm2835_i2s_startup,
  476. .shutdown = bcm2835_i2s_shutdown,
  477. .prepare = bcm2835_i2s_prepare,
  478. .trigger = bcm2835_i2s_trigger,
  479. .hw_params = bcm2835_i2s_hw_params,
  480. .set_fmt = bcm2835_i2s_set_dai_fmt,
  481. .set_bclk_ratio = bcm2835_i2s_set_dai_bclk_ratio,
  482. };
  483. static int bcm2835_i2s_dai_probe(struct snd_soc_dai *dai)
  484. {
  485. struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  486. snd_soc_dai_init_dma_data(dai,
  487. &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK],
  488. &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE]);
  489. return 0;
  490. }
  491. static struct snd_soc_dai_driver bcm2835_i2s_dai = {
  492. .name = "bcm2835-i2s",
  493. .probe = bcm2835_i2s_dai_probe,
  494. .playback = {
  495. .channels_min = 2,
  496. .channels_max = 2,
  497. .rates = SNDRV_PCM_RATE_8000_192000,
  498. .formats = SNDRV_PCM_FMTBIT_S16_LE
  499. | SNDRV_PCM_FMTBIT_S24_LE
  500. | SNDRV_PCM_FMTBIT_S32_LE
  501. },
  502. .capture = {
  503. .channels_min = 2,
  504. .channels_max = 2,
  505. .rates = SNDRV_PCM_RATE_8000_192000,
  506. .formats = SNDRV_PCM_FMTBIT_S16_LE
  507. | SNDRV_PCM_FMTBIT_S24_LE
  508. | SNDRV_PCM_FMTBIT_S32_LE
  509. },
  510. .ops = &bcm2835_i2s_dai_ops,
  511. .symmetric_rates = 1
  512. };
  513. static bool bcm2835_i2s_volatile_reg(struct device *dev, unsigned int reg)
  514. {
  515. switch (reg) {
  516. case BCM2835_I2S_CS_A_REG:
  517. case BCM2835_I2S_FIFO_A_REG:
  518. case BCM2835_I2S_INTSTC_A_REG:
  519. case BCM2835_I2S_GRAY_REG:
  520. return true;
  521. default:
  522. return false;
  523. };
  524. }
  525. static bool bcm2835_i2s_precious_reg(struct device *dev, unsigned int reg)
  526. {
  527. switch (reg) {
  528. case BCM2835_I2S_FIFO_A_REG:
  529. return true;
  530. default:
  531. return false;
  532. };
  533. }
  534. static const struct regmap_config bcm2835_regmap_config = {
  535. .reg_bits = 32,
  536. .reg_stride = 4,
  537. .val_bits = 32,
  538. .max_register = BCM2835_I2S_GRAY_REG,
  539. .precious_reg = bcm2835_i2s_precious_reg,
  540. .volatile_reg = bcm2835_i2s_volatile_reg,
  541. .cache_type = REGCACHE_RBTREE,
  542. };
  543. static const struct snd_soc_component_driver bcm2835_i2s_component = {
  544. .name = "bcm2835-i2s-comp",
  545. };
  546. static int bcm2835_i2s_probe(struct platform_device *pdev)
  547. {
  548. struct bcm2835_i2s_dev *dev;
  549. int ret;
  550. struct resource *mem;
  551. void __iomem *base;
  552. const __be32 *addr;
  553. dma_addr_t dma_base;
  554. dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
  555. GFP_KERNEL);
  556. if (!dev)
  557. return -ENOMEM;
  558. /* get the clock */
  559. dev->clk_prepared = false;
  560. dev->clk = devm_clk_get(&pdev->dev, NULL);
  561. if (IS_ERR(dev->clk)) {
  562. dev_err(&pdev->dev, "could not get clk: %ld\n",
  563. PTR_ERR(dev->clk));
  564. return PTR_ERR(dev->clk);
  565. }
  566. /* Request ioarea */
  567. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  568. base = devm_ioremap_resource(&pdev->dev, mem);
  569. if (IS_ERR(base))
  570. return PTR_ERR(base);
  571. dev->i2s_regmap = devm_regmap_init_mmio(&pdev->dev, base,
  572. &bcm2835_regmap_config);
  573. if (IS_ERR(dev->i2s_regmap))
  574. return PTR_ERR(dev->i2s_regmap);
  575. /* Set the DMA address - we have to parse DT ourselves */
  576. addr = of_get_address(pdev->dev.of_node, 0, NULL, NULL);
  577. if (!addr) {
  578. dev_err(&pdev->dev, "could not get DMA-register address\n");
  579. return -EINVAL;
  580. }
  581. dma_base = be32_to_cpup(addr);
  582. dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  583. dma_base + BCM2835_I2S_FIFO_A_REG;
  584. dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  585. dma_base + BCM2835_I2S_FIFO_A_REG;
  586. /* Set the bus width */
  587. dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
  588. DMA_SLAVE_BUSWIDTH_4_BYTES;
  589. dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
  590. DMA_SLAVE_BUSWIDTH_4_BYTES;
  591. /* Set burst */
  592. dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
  593. dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
  594. /*
  595. * Set the PACK flag to enable S16_LE support (2 S16_LE values
  596. * packed into 32-bit transfers).
  597. */
  598. dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].flags =
  599. SND_DMAENGINE_PCM_DAI_FLAG_PACK;
  600. dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].flags =
  601. SND_DMAENGINE_PCM_DAI_FLAG_PACK;
  602. /* BCLK ratio - use default */
  603. dev->bclk_ratio = 0;
  604. /* Store the pdev */
  605. dev->dev = &pdev->dev;
  606. dev_set_drvdata(&pdev->dev, dev);
  607. ret = devm_snd_soc_register_component(&pdev->dev,
  608. &bcm2835_i2s_component, &bcm2835_i2s_dai, 1);
  609. if (ret) {
  610. dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  611. return ret;
  612. }
  613. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  614. if (ret) {
  615. dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  616. return ret;
  617. }
  618. return 0;
  619. }
  620. static const struct of_device_id bcm2835_i2s_of_match[] = {
  621. { .compatible = "brcm,bcm2835-i2s", },
  622. {},
  623. };
  624. MODULE_DEVICE_TABLE(of, bcm2835_i2s_of_match);
  625. static struct platform_driver bcm2835_i2s_driver = {
  626. .probe = bcm2835_i2s_probe,
  627. .driver = {
  628. .name = "bcm2835-i2s",
  629. .of_match_table = bcm2835_i2s_of_match,
  630. },
  631. };
  632. module_platform_driver(bcm2835_i2s_driver);
  633. MODULE_ALIAS("platform:bcm2835-i2s");
  634. MODULE_DESCRIPTION("BCM2835 I2S interface");
  635. MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  636. MODULE_LICENSE("GPL v2");