sun50i-dmic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // This driver supports the DMIC in Allwinner's H6 SoCs.
  4. //
  5. // Copyright 2021 Ban Tao <[email protected]>
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/of_device.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/reset.h>
  13. #include <sound/dmaengine_pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/soc.h>
  16. #define SUN50I_DMIC_EN_CTL (0x00)
  17. #define SUN50I_DMIC_EN_CTL_GLOBE BIT(8)
  18. #define SUN50I_DMIC_EN_CTL_CHAN(v) ((v) << 0)
  19. #define SUN50I_DMIC_EN_CTL_CHAN_MASK GENMASK(7, 0)
  20. #define SUN50I_DMIC_SR (0x04)
  21. #define SUN50I_DMIC_SR_SAMPLE_RATE(v) ((v) << 0)
  22. #define SUN50I_DMIC_SR_SAMPLE_RATE_MASK GENMASK(2, 0)
  23. #define SUN50I_DMIC_CTL (0x08)
  24. #define SUN50I_DMIC_CTL_OVERSAMPLE_RATE BIT(0)
  25. #define SUN50I_DMIC_DATA (0x10)
  26. #define SUN50I_DMIC_INTC (0x14)
  27. #define SUN50I_DMIC_FIFO_DRQ_EN BIT(2)
  28. #define SUN50I_DMIC_INT_STA (0x18)
  29. #define SUN50I_DMIC_INT_STA_OVERRUN_IRQ_PENDING BIT(1)
  30. #define SUN50I_DMIC_INT_STA_DATA_IRQ_PENDING BIT(0)
  31. #define SUN50I_DMIC_RXFIFO_CTL (0x1c)
  32. #define SUN50I_DMIC_RXFIFO_CTL_FLUSH BIT(31)
  33. #define SUN50I_DMIC_RXFIFO_CTL_MODE_MASK BIT(9)
  34. #define SUN50I_DMIC_RXFIFO_CTL_MODE_LSB (0 << 9)
  35. #define SUN50I_DMIC_RXFIFO_CTL_MODE_MSB (1 << 9)
  36. #define SUN50I_DMIC_RXFIFO_CTL_SAMPLE_MASK BIT(8)
  37. #define SUN50I_DMIC_RXFIFO_CTL_SAMPLE_16 (0 << 8)
  38. #define SUN50I_DMIC_RXFIFO_CTL_SAMPLE_24 (1 << 8)
  39. #define SUN50I_DMIC_CH_NUM (0x24)
  40. #define SUN50I_DMIC_CH_NUM_N(v) ((v) << 0)
  41. #define SUN50I_DMIC_CH_NUM_N_MASK GENMASK(2, 0)
  42. #define SUN50I_DMIC_CNT (0x2c)
  43. #define SUN50I_DMIC_CNT_N (1 << 0)
  44. #define SUN50I_DMIC_HPF_CTRL (0x38)
  45. #define SUN50I_DMIC_VERSION (0x50)
  46. struct sun50i_dmic_dev {
  47. struct clk *dmic_clk;
  48. struct clk *bus_clk;
  49. struct reset_control *rst;
  50. struct regmap *regmap;
  51. struct snd_dmaengine_dai_dma_data dma_params_rx;
  52. };
  53. struct dmic_rate {
  54. unsigned int samplerate;
  55. unsigned int rate_bit;
  56. };
  57. static const struct dmic_rate dmic_rate_s[] = {
  58. {48000, 0x0},
  59. {44100, 0x0},
  60. {32000, 0x1},
  61. {24000, 0x2},
  62. {22050, 0x2},
  63. {16000, 0x3},
  64. {12000, 0x4},
  65. {11025, 0x4},
  66. {8000, 0x5},
  67. };
  68. static int sun50i_dmic_startup(struct snd_pcm_substream *substream,
  69. struct snd_soc_dai *cpu_dai)
  70. {
  71. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  72. struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
  73. /* only support capture */
  74. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  75. return -EINVAL;
  76. regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
  77. SUN50I_DMIC_RXFIFO_CTL_FLUSH,
  78. SUN50I_DMIC_RXFIFO_CTL_FLUSH);
  79. regmap_write(host->regmap, SUN50I_DMIC_CNT, SUN50I_DMIC_CNT_N);
  80. return 0;
  81. }
  82. static int sun50i_dmic_hw_params(struct snd_pcm_substream *substream,
  83. struct snd_pcm_hw_params *params,
  84. struct snd_soc_dai *cpu_dai)
  85. {
  86. int i = 0;
  87. unsigned long rate = params_rate(params);
  88. unsigned int mclk = 0;
  89. unsigned int channels = params_channels(params);
  90. unsigned int chan_en = (1 << channels) - 1;
  91. struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  92. /* DMIC num is N+1 */
  93. regmap_update_bits(host->regmap, SUN50I_DMIC_CH_NUM,
  94. SUN50I_DMIC_CH_NUM_N_MASK,
  95. SUN50I_DMIC_CH_NUM_N(channels - 1));
  96. regmap_write(host->regmap, SUN50I_DMIC_HPF_CTRL, chan_en);
  97. regmap_update_bits(host->regmap, SUN50I_DMIC_EN_CTL,
  98. SUN50I_DMIC_EN_CTL_CHAN_MASK,
  99. SUN50I_DMIC_EN_CTL_CHAN(chan_en));
  100. switch (params_format(params)) {
  101. case SNDRV_PCM_FORMAT_S16_LE:
  102. regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
  103. SUN50I_DMIC_RXFIFO_CTL_SAMPLE_MASK,
  104. SUN50I_DMIC_RXFIFO_CTL_SAMPLE_16);
  105. break;
  106. case SNDRV_PCM_FORMAT_S24_LE:
  107. regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
  108. SUN50I_DMIC_RXFIFO_CTL_SAMPLE_MASK,
  109. SUN50I_DMIC_RXFIFO_CTL_SAMPLE_24);
  110. break;
  111. default:
  112. dev_err(cpu_dai->dev, "Invalid format!\n");
  113. return -EINVAL;
  114. }
  115. /* The hardware supports FIFO mode 1 for 24-bit samples */
  116. regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
  117. SUN50I_DMIC_RXFIFO_CTL_MODE_MASK,
  118. SUN50I_DMIC_RXFIFO_CTL_MODE_MSB);
  119. switch (rate) {
  120. case 11025:
  121. case 22050:
  122. case 44100:
  123. mclk = 22579200;
  124. break;
  125. case 8000:
  126. case 12000:
  127. case 16000:
  128. case 24000:
  129. case 32000:
  130. case 48000:
  131. mclk = 24576000;
  132. break;
  133. default:
  134. dev_err(cpu_dai->dev, "Invalid rate!\n");
  135. return -EINVAL;
  136. }
  137. if (clk_set_rate(host->dmic_clk, mclk)) {
  138. dev_err(cpu_dai->dev, "mclk : %u not support\n", mclk);
  139. return -EINVAL;
  140. }
  141. for (i = 0; i < ARRAY_SIZE(dmic_rate_s); i++) {
  142. if (dmic_rate_s[i].samplerate == rate) {
  143. regmap_update_bits(host->regmap, SUN50I_DMIC_SR,
  144. SUN50I_DMIC_SR_SAMPLE_RATE_MASK,
  145. SUN50I_DMIC_SR_SAMPLE_RATE(dmic_rate_s[i].rate_bit));
  146. break;
  147. }
  148. }
  149. switch (params_physical_width(params)) {
  150. case 16:
  151. host->dma_params_rx.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  152. break;
  153. case 32:
  154. host->dma_params_rx.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  155. break;
  156. default:
  157. dev_err(cpu_dai->dev, "Unsupported physical sample width: %d\n",
  158. params_physical_width(params));
  159. return -EINVAL;
  160. }
  161. /* oversamplerate adjust */
  162. if (params_rate(params) >= 24000)
  163. regmap_update_bits(host->regmap, SUN50I_DMIC_CTL,
  164. SUN50I_DMIC_CTL_OVERSAMPLE_RATE,
  165. SUN50I_DMIC_CTL_OVERSAMPLE_RATE);
  166. else
  167. regmap_update_bits(host->regmap, SUN50I_DMIC_CTL,
  168. SUN50I_DMIC_CTL_OVERSAMPLE_RATE, 0);
  169. return 0;
  170. }
  171. static int sun50i_dmic_trigger(struct snd_pcm_substream *substream, int cmd,
  172. struct snd_soc_dai *dai)
  173. {
  174. int ret = 0;
  175. struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(dai);
  176. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  177. return -EINVAL;
  178. switch (cmd) {
  179. case SNDRV_PCM_TRIGGER_START:
  180. case SNDRV_PCM_TRIGGER_RESUME:
  181. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  182. /* DRQ ENABLE */
  183. regmap_update_bits(host->regmap, SUN50I_DMIC_INTC,
  184. SUN50I_DMIC_FIFO_DRQ_EN,
  185. SUN50I_DMIC_FIFO_DRQ_EN);
  186. /* Global enable */
  187. regmap_update_bits(host->regmap, SUN50I_DMIC_EN_CTL,
  188. SUN50I_DMIC_EN_CTL_GLOBE,
  189. SUN50I_DMIC_EN_CTL_GLOBE);
  190. break;
  191. case SNDRV_PCM_TRIGGER_STOP:
  192. case SNDRV_PCM_TRIGGER_SUSPEND:
  193. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  194. /* DRQ DISABLE */
  195. regmap_update_bits(host->regmap, SUN50I_DMIC_INTC,
  196. SUN50I_DMIC_FIFO_DRQ_EN, 0);
  197. /* Global disable */
  198. regmap_update_bits(host->regmap, SUN50I_DMIC_EN_CTL,
  199. SUN50I_DMIC_EN_CTL_GLOBE, 0);
  200. break;
  201. default:
  202. ret = -EINVAL;
  203. break;
  204. }
  205. return ret;
  206. }
  207. static int sun50i_dmic_soc_dai_probe(struct snd_soc_dai *dai)
  208. {
  209. struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(dai);
  210. snd_soc_dai_init_dma_data(dai, NULL, &host->dma_params_rx);
  211. return 0;
  212. }
  213. static const struct snd_soc_dai_ops sun50i_dmic_dai_ops = {
  214. .startup = sun50i_dmic_startup,
  215. .trigger = sun50i_dmic_trigger,
  216. .hw_params = sun50i_dmic_hw_params,
  217. };
  218. static const struct regmap_config sun50i_dmic_regmap_config = {
  219. .reg_bits = 32,
  220. .reg_stride = 4,
  221. .val_bits = 32,
  222. .max_register = SUN50I_DMIC_VERSION,
  223. .cache_type = REGCACHE_NONE,
  224. };
  225. #define SUN50I_DMIC_RATES (SNDRV_PCM_RATE_8000_48000)
  226. #define SUN50I_DMIC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
  227. static struct snd_soc_dai_driver sun50i_dmic_dai = {
  228. .capture = {
  229. .channels_min = 1,
  230. .channels_max = 8,
  231. .rates = SUN50I_DMIC_RATES,
  232. .formats = SUN50I_DMIC_FORMATS,
  233. .sig_bits = 21,
  234. },
  235. .probe = sun50i_dmic_soc_dai_probe,
  236. .ops = &sun50i_dmic_dai_ops,
  237. .name = "dmic",
  238. };
  239. static const struct of_device_id sun50i_dmic_of_match[] = {
  240. {
  241. .compatible = "allwinner,sun50i-h6-dmic",
  242. },
  243. { /* sentinel */ }
  244. };
  245. MODULE_DEVICE_TABLE(of, sun50i_dmic_of_match);
  246. static const struct snd_soc_component_driver sun50i_dmic_component = {
  247. .name = "sun50i-dmic",
  248. };
  249. static int sun50i_dmic_runtime_suspend(struct device *dev)
  250. {
  251. struct sun50i_dmic_dev *host = dev_get_drvdata(dev);
  252. clk_disable_unprepare(host->dmic_clk);
  253. clk_disable_unprepare(host->bus_clk);
  254. return 0;
  255. }
  256. static int sun50i_dmic_runtime_resume(struct device *dev)
  257. {
  258. struct sun50i_dmic_dev *host = dev_get_drvdata(dev);
  259. int ret;
  260. ret = clk_prepare_enable(host->dmic_clk);
  261. if (ret)
  262. return ret;
  263. ret = clk_prepare_enable(host->bus_clk);
  264. if (ret) {
  265. clk_disable_unprepare(host->dmic_clk);
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. static int sun50i_dmic_probe(struct platform_device *pdev)
  271. {
  272. struct sun50i_dmic_dev *host;
  273. struct resource *res;
  274. int ret;
  275. void __iomem *base;
  276. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  277. if (!host)
  278. return -ENOMEM;
  279. /* Get the addresses */
  280. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  281. base = devm_ioremap_resource(&pdev->dev, res);
  282. if (IS_ERR(base))
  283. return dev_err_probe(&pdev->dev, PTR_ERR(base),
  284. "get resource failed.\n");
  285. host->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  286. &sun50i_dmic_regmap_config);
  287. /* Clocks */
  288. host->bus_clk = devm_clk_get(&pdev->dev, "bus");
  289. if (IS_ERR(host->bus_clk))
  290. return dev_err_probe(&pdev->dev, PTR_ERR(host->bus_clk),
  291. "failed to get bus clock.\n");
  292. host->dmic_clk = devm_clk_get(&pdev->dev, "mod");
  293. if (IS_ERR(host->dmic_clk))
  294. return dev_err_probe(&pdev->dev, PTR_ERR(host->dmic_clk),
  295. "failed to get dmic clock.\n");
  296. host->dma_params_rx.addr = res->start + SUN50I_DMIC_DATA;
  297. host->dma_params_rx.maxburst = 8;
  298. platform_set_drvdata(pdev, host);
  299. host->rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
  300. if (IS_ERR(host->rst))
  301. return dev_err_probe(&pdev->dev, PTR_ERR(host->rst),
  302. "Failed to get reset.\n");
  303. reset_control_deassert(host->rst);
  304. ret = devm_snd_soc_register_component(&pdev->dev, &sun50i_dmic_component,
  305. &sun50i_dmic_dai, 1);
  306. if (ret)
  307. return dev_err_probe(&pdev->dev, ret,
  308. "failed to register component.\n");
  309. pm_runtime_enable(&pdev->dev);
  310. if (!pm_runtime_enabled(&pdev->dev)) {
  311. ret = sun50i_dmic_runtime_resume(&pdev->dev);
  312. if (ret)
  313. goto err_disable_runtime_pm;
  314. }
  315. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  316. if (ret)
  317. goto err_suspend;
  318. return 0;
  319. err_suspend:
  320. if (!pm_runtime_status_suspended(&pdev->dev))
  321. sun50i_dmic_runtime_suspend(&pdev->dev);
  322. err_disable_runtime_pm:
  323. pm_runtime_disable(&pdev->dev);
  324. return ret;
  325. }
  326. static int sun50i_dmic_remove(struct platform_device *pdev)
  327. {
  328. pm_runtime_disable(&pdev->dev);
  329. if (!pm_runtime_status_suspended(&pdev->dev))
  330. sun50i_dmic_runtime_suspend(&pdev->dev);
  331. return 0;
  332. }
  333. static const struct dev_pm_ops sun50i_dmic_pm = {
  334. SET_RUNTIME_PM_OPS(sun50i_dmic_runtime_suspend,
  335. sun50i_dmic_runtime_resume, NULL)
  336. };
  337. static struct platform_driver sun50i_dmic_driver = {
  338. .driver = {
  339. .name = "sun50i-dmic",
  340. .of_match_table = of_match_ptr(sun50i_dmic_of_match),
  341. .pm = &sun50i_dmic_pm,
  342. },
  343. .probe = sun50i_dmic_probe,
  344. .remove = sun50i_dmic_remove,
  345. };
  346. module_platform_driver(sun50i_dmic_driver);
  347. MODULE_DESCRIPTION("Allwinner sun50i DMIC SoC Interface");
  348. MODULE_AUTHOR("Ban Tao <[email protected]>");
  349. MODULE_LICENSE("GPL");
  350. MODULE_ALIAS("platform:sun50i-dmic");