sun4i-i2s.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Copyright (C) 2015 Andrea Venturi
  3. * Andrea Venturi <be17068@iperbole.bo.it>
  4. *
  5. * Copyright (C) 2016 Maxime Ripard
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regmap.h>
  19. #include <sound/dmaengine_pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dai.h>
  23. #define SUN4I_I2S_CTRL_REG 0x00
  24. #define SUN4I_I2S_CTRL_SDO_EN_MASK GENMASK(11, 8)
  25. #define SUN4I_I2S_CTRL_SDO_EN(sdo) BIT(8 + (sdo))
  26. #define SUN4I_I2S_CTRL_MODE_MASK BIT(5)
  27. #define SUN4I_I2S_CTRL_MODE_SLAVE (1 << 5)
  28. #define SUN4I_I2S_CTRL_MODE_MASTER (0 << 5)
  29. #define SUN4I_I2S_CTRL_TX_EN BIT(2)
  30. #define SUN4I_I2S_CTRL_RX_EN BIT(1)
  31. #define SUN4I_I2S_CTRL_GL_EN BIT(0)
  32. #define SUN4I_I2S_FMT0_REG 0x04
  33. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK BIT(7)
  34. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED (1 << 7)
  35. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL (0 << 7)
  36. #define SUN4I_I2S_FMT0_BCLK_POLARITY_MASK BIT(6)
  37. #define SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED (1 << 6)
  38. #define SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL (0 << 6)
  39. #define SUN4I_I2S_FMT0_SR_MASK GENMASK(5, 4)
  40. #define SUN4I_I2S_FMT0_SR(sr) ((sr) << 4)
  41. #define SUN4I_I2S_FMT0_WSS_MASK GENMASK(3, 2)
  42. #define SUN4I_I2S_FMT0_WSS(wss) ((wss) << 2)
  43. #define SUN4I_I2S_FMT0_FMT_MASK GENMASK(1, 0)
  44. #define SUN4I_I2S_FMT0_FMT_RIGHT_J (2 << 0)
  45. #define SUN4I_I2S_FMT0_FMT_LEFT_J (1 << 0)
  46. #define SUN4I_I2S_FMT0_FMT_I2S (0 << 0)
  47. #define SUN4I_I2S_FMT1_REG 0x08
  48. #define SUN4I_I2S_FIFO_TX_REG 0x0c
  49. #define SUN4I_I2S_FIFO_RX_REG 0x10
  50. #define SUN4I_I2S_FIFO_CTRL_REG 0x14
  51. #define SUN4I_I2S_FIFO_CTRL_FLUSH_TX BIT(25)
  52. #define SUN4I_I2S_FIFO_CTRL_FLUSH_RX BIT(24)
  53. #define SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK BIT(2)
  54. #define SUN4I_I2S_FIFO_CTRL_TX_MODE(mode) ((mode) << 2)
  55. #define SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK GENMASK(1, 0)
  56. #define SUN4I_I2S_FIFO_CTRL_RX_MODE(mode) (mode)
  57. #define SUN4I_I2S_FIFO_STA_REG 0x18
  58. #define SUN4I_I2S_DMA_INT_CTRL_REG 0x1c
  59. #define SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN BIT(7)
  60. #define SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN BIT(3)
  61. #define SUN4I_I2S_INT_STA_REG 0x20
  62. #define SUN4I_I2S_CLK_DIV_REG 0x24
  63. #define SUN4I_I2S_CLK_DIV_MCLK_EN BIT(7)
  64. #define SUN4I_I2S_CLK_DIV_BCLK_MASK GENMASK(6, 4)
  65. #define SUN4I_I2S_CLK_DIV_BCLK(bclk) ((bclk) << 4)
  66. #define SUN4I_I2S_CLK_DIV_MCLK_MASK GENMASK(3, 0)
  67. #define SUN4I_I2S_CLK_DIV_MCLK(mclk) ((mclk) << 0)
  68. #define SUN4I_I2S_RX_CNT_REG 0x28
  69. #define SUN4I_I2S_TX_CNT_REG 0x2c
  70. #define SUN4I_I2S_TX_CHAN_SEL_REG 0x30
  71. #define SUN4I_I2S_TX_CHAN_SEL(num_chan) (((num_chan) - 1) << 0)
  72. #define SUN4I_I2S_TX_CHAN_MAP_REG 0x34
  73. #define SUN4I_I2S_TX_CHAN_MAP(chan, sample) ((sample) << (chan << 2))
  74. #define SUN4I_I2S_RX_CHAN_SEL_REG 0x38
  75. #define SUN4I_I2S_RX_CHAN_MAP_REG 0x3c
  76. struct sun4i_i2s {
  77. struct clk *bus_clk;
  78. struct clk *mod_clk;
  79. struct regmap *regmap;
  80. unsigned int mclk_freq;
  81. struct snd_dmaengine_dai_dma_data capture_dma_data;
  82. struct snd_dmaengine_dai_dma_data playback_dma_data;
  83. };
  84. struct sun4i_i2s_clk_div {
  85. u8 div;
  86. u8 val;
  87. };
  88. static const struct sun4i_i2s_clk_div sun4i_i2s_bclk_div[] = {
  89. { .div = 2, .val = 0 },
  90. { .div = 4, .val = 1 },
  91. { .div = 6, .val = 2 },
  92. { .div = 8, .val = 3 },
  93. { .div = 12, .val = 4 },
  94. { .div = 16, .val = 5 },
  95. };
  96. static const struct sun4i_i2s_clk_div sun4i_i2s_mclk_div[] = {
  97. { .div = 1, .val = 0 },
  98. { .div = 2, .val = 1 },
  99. { .div = 4, .val = 2 },
  100. { .div = 6, .val = 3 },
  101. { .div = 8, .val = 4 },
  102. { .div = 12, .val = 5 },
  103. { .div = 16, .val = 6 },
  104. { .div = 24, .val = 7 },
  105. };
  106. static int sun4i_i2s_get_bclk_div(struct sun4i_i2s *i2s,
  107. unsigned int oversample_rate,
  108. unsigned int word_size)
  109. {
  110. int div = oversample_rate / word_size / 2;
  111. int i;
  112. for (i = 0; i < ARRAY_SIZE(sun4i_i2s_bclk_div); i++) {
  113. const struct sun4i_i2s_clk_div *bdiv = &sun4i_i2s_bclk_div[i];
  114. if (bdiv->div == div)
  115. return bdiv->val;
  116. }
  117. return -EINVAL;
  118. }
  119. static int sun4i_i2s_get_mclk_div(struct sun4i_i2s *i2s,
  120. unsigned int oversample_rate,
  121. unsigned int module_rate,
  122. unsigned int sampling_rate)
  123. {
  124. int div = module_rate / sampling_rate / oversample_rate;
  125. int i;
  126. for (i = 0; i < ARRAY_SIZE(sun4i_i2s_mclk_div); i++) {
  127. const struct sun4i_i2s_clk_div *mdiv = &sun4i_i2s_mclk_div[i];
  128. if (mdiv->div == div)
  129. return mdiv->val;
  130. }
  131. return -EINVAL;
  132. }
  133. static int sun4i_i2s_oversample_rates[] = { 128, 192, 256, 384, 512, 768 };
  134. static bool sun4i_i2s_oversample_is_valid(unsigned int oversample)
  135. {
  136. int i;
  137. for (i = 0; i < ARRAY_SIZE(sun4i_i2s_oversample_rates); i++)
  138. if (sun4i_i2s_oversample_rates[i] == oversample)
  139. return true;
  140. return false;
  141. }
  142. static int sun4i_i2s_set_clk_rate(struct sun4i_i2s *i2s,
  143. unsigned int rate,
  144. unsigned int word_size)
  145. {
  146. unsigned int oversample_rate, clk_rate;
  147. int bclk_div, mclk_div;
  148. int ret;
  149. switch (rate) {
  150. case 176400:
  151. case 88200:
  152. case 44100:
  153. case 22050:
  154. case 11025:
  155. clk_rate = 22579200;
  156. break;
  157. case 192000:
  158. case 128000:
  159. case 96000:
  160. case 64000:
  161. case 48000:
  162. case 32000:
  163. case 24000:
  164. case 16000:
  165. case 12000:
  166. case 8000:
  167. clk_rate = 24576000;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. ret = clk_set_rate(i2s->mod_clk, clk_rate);
  173. if (ret)
  174. return ret;
  175. oversample_rate = i2s->mclk_freq / rate;
  176. if (!sun4i_i2s_oversample_is_valid(oversample_rate))
  177. return -EINVAL;
  178. bclk_div = sun4i_i2s_get_bclk_div(i2s, oversample_rate,
  179. word_size);
  180. if (bclk_div < 0)
  181. return -EINVAL;
  182. mclk_div = sun4i_i2s_get_mclk_div(i2s, oversample_rate,
  183. clk_rate, rate);
  184. if (mclk_div < 0)
  185. return -EINVAL;
  186. regmap_write(i2s->regmap, SUN4I_I2S_CLK_DIV_REG,
  187. SUN4I_I2S_CLK_DIV_BCLK(bclk_div) |
  188. SUN4I_I2S_CLK_DIV_MCLK(mclk_div) |
  189. SUN4I_I2S_CLK_DIV_MCLK_EN);
  190. return 0;
  191. }
  192. static int sun4i_i2s_hw_params(struct snd_pcm_substream *substream,
  193. struct snd_pcm_hw_params *params,
  194. struct snd_soc_dai *dai)
  195. {
  196. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  197. int sr, wss;
  198. u32 width;
  199. if (params_channels(params) != 2)
  200. return -EINVAL;
  201. switch (params_physical_width(params)) {
  202. case 16:
  203. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. i2s->playback_dma_data.addr_width = width;
  209. switch (params_width(params)) {
  210. case 16:
  211. sr = 0;
  212. wss = 0;
  213. break;
  214. default:
  215. return -EINVAL;
  216. }
  217. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  218. SUN4I_I2S_FMT0_WSS_MASK | SUN4I_I2S_FMT0_SR_MASK,
  219. SUN4I_I2S_FMT0_WSS(wss) | SUN4I_I2S_FMT0_SR(sr));
  220. return sun4i_i2s_set_clk_rate(i2s, params_rate(params),
  221. params_width(params));
  222. }
  223. static int sun4i_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  224. {
  225. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  226. u32 val;
  227. /* DAI Mode */
  228. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  229. case SND_SOC_DAIFMT_I2S:
  230. val = SUN4I_I2S_FMT0_FMT_I2S;
  231. break;
  232. case SND_SOC_DAIFMT_LEFT_J:
  233. val = SUN4I_I2S_FMT0_FMT_LEFT_J;
  234. break;
  235. case SND_SOC_DAIFMT_RIGHT_J:
  236. val = SUN4I_I2S_FMT0_FMT_RIGHT_J;
  237. break;
  238. default:
  239. return -EINVAL;
  240. }
  241. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  242. SUN4I_I2S_FMT0_FMT_MASK,
  243. val);
  244. /* DAI clock polarity */
  245. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  246. case SND_SOC_DAIFMT_IB_IF:
  247. /* Invert both clocks */
  248. val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED |
  249. SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
  250. break;
  251. case SND_SOC_DAIFMT_IB_NF:
  252. /* Invert bit clock */
  253. val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED |
  254. SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL;
  255. break;
  256. case SND_SOC_DAIFMT_NB_IF:
  257. /* Invert frame clock */
  258. val = SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED |
  259. SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL;
  260. break;
  261. case SND_SOC_DAIFMT_NB_NF:
  262. /* Nothing to do for both normal cases */
  263. val = SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL |
  264. SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL;
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  270. SUN4I_I2S_FMT0_BCLK_POLARITY_MASK |
  271. SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK,
  272. val);
  273. /* DAI clock master masks */
  274. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  275. case SND_SOC_DAIFMT_CBS_CFS:
  276. /* BCLK and LRCLK master */
  277. val = SUN4I_I2S_CTRL_MODE_MASTER;
  278. break;
  279. case SND_SOC_DAIFMT_CBM_CFM:
  280. /* BCLK and LRCLK slave */
  281. val = SUN4I_I2S_CTRL_MODE_SLAVE;
  282. break;
  283. default:
  284. return -EINVAL;
  285. }
  286. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  287. SUN4I_I2S_CTRL_MODE_MASK,
  288. val);
  289. /* Set significant bits in our FIFOs */
  290. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  291. SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK |
  292. SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK,
  293. SUN4I_I2S_FIFO_CTRL_TX_MODE(1) |
  294. SUN4I_I2S_FIFO_CTRL_RX_MODE(1));
  295. return 0;
  296. }
  297. static void sun4i_i2s_start_capture(struct sun4i_i2s *i2s)
  298. {
  299. /* Flush RX FIFO */
  300. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  301. SUN4I_I2S_FIFO_CTRL_FLUSH_RX,
  302. SUN4I_I2S_FIFO_CTRL_FLUSH_RX);
  303. /* Clear RX counter */
  304. regmap_write(i2s->regmap, SUN4I_I2S_RX_CNT_REG, 0);
  305. /* Enable RX Block */
  306. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  307. SUN4I_I2S_CTRL_RX_EN,
  308. SUN4I_I2S_CTRL_RX_EN);
  309. /* Enable RX DRQ */
  310. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  311. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
  312. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN);
  313. }
  314. static void sun4i_i2s_start_playback(struct sun4i_i2s *i2s)
  315. {
  316. /* Flush TX FIFO */
  317. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  318. SUN4I_I2S_FIFO_CTRL_FLUSH_TX,
  319. SUN4I_I2S_FIFO_CTRL_FLUSH_TX);
  320. /* Clear TX counter */
  321. regmap_write(i2s->regmap, SUN4I_I2S_TX_CNT_REG, 0);
  322. /* Enable TX Block */
  323. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  324. SUN4I_I2S_CTRL_TX_EN,
  325. SUN4I_I2S_CTRL_TX_EN);
  326. /* Enable TX DRQ */
  327. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  328. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
  329. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN);
  330. }
  331. static void sun4i_i2s_stop_capture(struct sun4i_i2s *i2s)
  332. {
  333. /* Disable RX Block */
  334. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  335. SUN4I_I2S_CTRL_RX_EN,
  336. 0);
  337. /* Disable RX DRQ */
  338. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  339. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
  340. 0);
  341. }
  342. static void sun4i_i2s_stop_playback(struct sun4i_i2s *i2s)
  343. {
  344. /* Disable TX Block */
  345. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  346. SUN4I_I2S_CTRL_TX_EN,
  347. 0);
  348. /* Disable TX DRQ */
  349. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  350. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
  351. 0);
  352. }
  353. static int sun4i_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  354. struct snd_soc_dai *dai)
  355. {
  356. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  357. switch (cmd) {
  358. case SNDRV_PCM_TRIGGER_START:
  359. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  360. case SNDRV_PCM_TRIGGER_RESUME:
  361. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  362. sun4i_i2s_start_playback(i2s);
  363. else
  364. sun4i_i2s_start_capture(i2s);
  365. break;
  366. case SNDRV_PCM_TRIGGER_STOP:
  367. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  368. case SNDRV_PCM_TRIGGER_SUSPEND:
  369. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  370. sun4i_i2s_stop_playback(i2s);
  371. else
  372. sun4i_i2s_stop_capture(i2s);
  373. break;
  374. default:
  375. return -EINVAL;
  376. }
  377. return 0;
  378. }
  379. static int sun4i_i2s_startup(struct snd_pcm_substream *substream,
  380. struct snd_soc_dai *dai)
  381. {
  382. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  383. /* Enable the whole hardware block */
  384. regmap_write(i2s->regmap, SUN4I_I2S_CTRL_REG,
  385. SUN4I_I2S_CTRL_GL_EN);
  386. /* Enable the first output line */
  387. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  388. SUN4I_I2S_CTRL_SDO_EN_MASK,
  389. SUN4I_I2S_CTRL_SDO_EN(0));
  390. /* Enable the first two channels */
  391. regmap_write(i2s->regmap, SUN4I_I2S_TX_CHAN_SEL_REG,
  392. SUN4I_I2S_TX_CHAN_SEL(2));
  393. /* Map them to the two first samples coming in */
  394. regmap_write(i2s->regmap, SUN4I_I2S_TX_CHAN_MAP_REG,
  395. SUN4I_I2S_TX_CHAN_MAP(0, 0) | SUN4I_I2S_TX_CHAN_MAP(1, 1));
  396. return clk_prepare_enable(i2s->mod_clk);
  397. }
  398. static void sun4i_i2s_shutdown(struct snd_pcm_substream *substream,
  399. struct snd_soc_dai *dai)
  400. {
  401. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  402. clk_disable_unprepare(i2s->mod_clk);
  403. /* Disable our output lines */
  404. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  405. SUN4I_I2S_CTRL_SDO_EN_MASK, 0);
  406. /* Disable the whole hardware block */
  407. regmap_write(i2s->regmap, SUN4I_I2S_CTRL_REG, 0);
  408. }
  409. static int sun4i_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  410. unsigned int freq, int dir)
  411. {
  412. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  413. if (clk_id != 0)
  414. return -EINVAL;
  415. i2s->mclk_freq = freq;
  416. return 0;
  417. }
  418. static const struct snd_soc_dai_ops sun4i_i2s_dai_ops = {
  419. .hw_params = sun4i_i2s_hw_params,
  420. .set_fmt = sun4i_i2s_set_fmt,
  421. .set_sysclk = sun4i_i2s_set_sysclk,
  422. .shutdown = sun4i_i2s_shutdown,
  423. .startup = sun4i_i2s_startup,
  424. .trigger = sun4i_i2s_trigger,
  425. };
  426. static int sun4i_i2s_dai_probe(struct snd_soc_dai *dai)
  427. {
  428. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  429. snd_soc_dai_init_dma_data(dai,
  430. &i2s->playback_dma_data,
  431. &i2s->capture_dma_data);
  432. snd_soc_dai_set_drvdata(dai, i2s);
  433. return 0;
  434. }
  435. static struct snd_soc_dai_driver sun4i_i2s_dai = {
  436. .probe = sun4i_i2s_dai_probe,
  437. .capture = {
  438. .stream_name = "Capture",
  439. .channels_min = 2,
  440. .channels_max = 2,
  441. .rates = SNDRV_PCM_RATE_8000_192000,
  442. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  443. },
  444. .playback = {
  445. .stream_name = "Playback",
  446. .channels_min = 2,
  447. .channels_max = 2,
  448. .rates = SNDRV_PCM_RATE_8000_192000,
  449. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  450. },
  451. .ops = &sun4i_i2s_dai_ops,
  452. .symmetric_rates = 1,
  453. };
  454. static const struct snd_soc_component_driver sun4i_i2s_component = {
  455. .name = "sun4i-dai",
  456. };
  457. static bool sun4i_i2s_rd_reg(struct device *dev, unsigned int reg)
  458. {
  459. switch (reg) {
  460. case SUN4I_I2S_FIFO_TX_REG:
  461. return false;
  462. default:
  463. return true;
  464. }
  465. }
  466. static bool sun4i_i2s_wr_reg(struct device *dev, unsigned int reg)
  467. {
  468. switch (reg) {
  469. case SUN4I_I2S_FIFO_RX_REG:
  470. case SUN4I_I2S_FIFO_STA_REG:
  471. return false;
  472. default:
  473. return true;
  474. }
  475. }
  476. static bool sun4i_i2s_volatile_reg(struct device *dev, unsigned int reg)
  477. {
  478. switch (reg) {
  479. case SUN4I_I2S_FIFO_RX_REG:
  480. case SUN4I_I2S_INT_STA_REG:
  481. case SUN4I_I2S_RX_CNT_REG:
  482. case SUN4I_I2S_TX_CNT_REG:
  483. return true;
  484. default:
  485. return false;
  486. }
  487. }
  488. static const struct reg_default sun4i_i2s_reg_defaults[] = {
  489. { SUN4I_I2S_CTRL_REG, 0x00000000 },
  490. { SUN4I_I2S_FMT0_REG, 0x0000000c },
  491. { SUN4I_I2S_FMT1_REG, 0x00004020 },
  492. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  493. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  494. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  495. { SUN4I_I2S_TX_CHAN_SEL_REG, 0x00000001 },
  496. { SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210 },
  497. { SUN4I_I2S_RX_CHAN_SEL_REG, 0x00000001 },
  498. { SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210 },
  499. };
  500. static const struct regmap_config sun4i_i2s_regmap_config = {
  501. .reg_bits = 32,
  502. .reg_stride = 4,
  503. .val_bits = 32,
  504. .max_register = SUN4I_I2S_RX_CHAN_MAP_REG,
  505. .cache_type = REGCACHE_FLAT,
  506. .reg_defaults = sun4i_i2s_reg_defaults,
  507. .num_reg_defaults = ARRAY_SIZE(sun4i_i2s_reg_defaults),
  508. .writeable_reg = sun4i_i2s_wr_reg,
  509. .readable_reg = sun4i_i2s_rd_reg,
  510. .volatile_reg = sun4i_i2s_volatile_reg,
  511. };
  512. static int sun4i_i2s_runtime_resume(struct device *dev)
  513. {
  514. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  515. int ret;
  516. ret = clk_prepare_enable(i2s->bus_clk);
  517. if (ret) {
  518. dev_err(dev, "Failed to enable bus clock\n");
  519. return ret;
  520. }
  521. regcache_cache_only(i2s->regmap, false);
  522. regcache_mark_dirty(i2s->regmap);
  523. ret = regcache_sync(i2s->regmap);
  524. if (ret) {
  525. dev_err(dev, "Failed to sync regmap cache\n");
  526. goto err_disable_clk;
  527. }
  528. return 0;
  529. err_disable_clk:
  530. clk_disable_unprepare(i2s->bus_clk);
  531. return ret;
  532. }
  533. static int sun4i_i2s_runtime_suspend(struct device *dev)
  534. {
  535. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  536. regcache_cache_only(i2s->regmap, true);
  537. clk_disable_unprepare(i2s->bus_clk);
  538. return 0;
  539. }
  540. static int sun4i_i2s_probe(struct platform_device *pdev)
  541. {
  542. struct sun4i_i2s *i2s;
  543. struct resource *res;
  544. void __iomem *regs;
  545. int irq, ret;
  546. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  547. if (!i2s)
  548. return -ENOMEM;
  549. platform_set_drvdata(pdev, i2s);
  550. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  551. regs = devm_ioremap_resource(&pdev->dev, res);
  552. if (IS_ERR(regs))
  553. return PTR_ERR(regs);
  554. irq = platform_get_irq(pdev, 0);
  555. if (irq < 0) {
  556. dev_err(&pdev->dev, "Can't retrieve our interrupt\n");
  557. return irq;
  558. }
  559. i2s->bus_clk = devm_clk_get(&pdev->dev, "apb");
  560. if (IS_ERR(i2s->bus_clk)) {
  561. dev_err(&pdev->dev, "Can't get our bus clock\n");
  562. return PTR_ERR(i2s->bus_clk);
  563. }
  564. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  565. &sun4i_i2s_regmap_config);
  566. if (IS_ERR(i2s->regmap)) {
  567. dev_err(&pdev->dev, "Regmap initialisation failed\n");
  568. return PTR_ERR(i2s->regmap);
  569. }
  570. i2s->mod_clk = devm_clk_get(&pdev->dev, "mod");
  571. if (IS_ERR(i2s->mod_clk)) {
  572. dev_err(&pdev->dev, "Can't get our mod clock\n");
  573. return PTR_ERR(i2s->mod_clk);
  574. }
  575. i2s->playback_dma_data.addr = res->start + SUN4I_I2S_FIFO_TX_REG;
  576. i2s->playback_dma_data.maxburst = 4;
  577. i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG;
  578. i2s->capture_dma_data.maxburst = 4;
  579. pm_runtime_enable(&pdev->dev);
  580. if (!pm_runtime_enabled(&pdev->dev)) {
  581. ret = sun4i_i2s_runtime_resume(&pdev->dev);
  582. if (ret)
  583. goto err_pm_disable;
  584. }
  585. ret = devm_snd_soc_register_component(&pdev->dev,
  586. &sun4i_i2s_component,
  587. &sun4i_i2s_dai, 1);
  588. if (ret) {
  589. dev_err(&pdev->dev, "Could not register DAI\n");
  590. goto err_suspend;
  591. }
  592. ret = snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  593. if (ret) {
  594. dev_err(&pdev->dev, "Could not register PCM\n");
  595. goto err_suspend;
  596. }
  597. return 0;
  598. err_suspend:
  599. if (!pm_runtime_status_suspended(&pdev->dev))
  600. sun4i_i2s_runtime_suspend(&pdev->dev);
  601. err_pm_disable:
  602. pm_runtime_disable(&pdev->dev);
  603. return ret;
  604. }
  605. static int sun4i_i2s_remove(struct platform_device *pdev)
  606. {
  607. snd_dmaengine_pcm_unregister(&pdev->dev);
  608. pm_runtime_disable(&pdev->dev);
  609. if (!pm_runtime_status_suspended(&pdev->dev))
  610. sun4i_i2s_runtime_suspend(&pdev->dev);
  611. return 0;
  612. }
  613. static const struct of_device_id sun4i_i2s_match[] = {
  614. { .compatible = "allwinner,sun4i-a10-i2s", },
  615. {}
  616. };
  617. MODULE_DEVICE_TABLE(of, sun4i_i2s_match);
  618. static const struct dev_pm_ops sun4i_i2s_pm_ops = {
  619. .runtime_resume = sun4i_i2s_runtime_resume,
  620. .runtime_suspend = sun4i_i2s_runtime_suspend,
  621. };
  622. static struct platform_driver sun4i_i2s_driver = {
  623. .probe = sun4i_i2s_probe,
  624. .remove = sun4i_i2s_remove,
  625. .driver = {
  626. .name = "sun4i-i2s",
  627. .of_match_table = sun4i_i2s_match,
  628. .pm = &sun4i_i2s_pm_ops,
  629. },
  630. };
  631. module_platform_driver(sun4i_i2s_driver);
  632. MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
  633. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  634. MODULE_DESCRIPTION("Allwinner A10 I2S driver");
  635. MODULE_LICENSE("GPL");