sun4i-i2s.c 18 KB

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