mtk-afe-platform-driver.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * mtk-afe-platform-driver.c -- Mediatek afe platform driver
  3. *
  4. * Copyright (c) 2016 MediaTek Inc.
  5. * Author: Garlic Tseng <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/dma-mapping.h>
  18. #include <sound/soc.h>
  19. #include "mtk-afe-platform-driver.h"
  20. #include "mtk-base-afe.h"
  21. static snd_pcm_uframes_t mtk_afe_pcm_pointer
  22. (struct snd_pcm_substream *substream)
  23. {
  24. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  25. struct mtk_base_afe *afe = snd_soc_platform_get_drvdata(rtd->platform);
  26. struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
  27. const struct mtk_base_memif_data *memif_data = memif->data;
  28. struct regmap *regmap = afe->regmap;
  29. struct device *dev = afe->dev;
  30. int reg_ofs_base = memif_data->reg_ofs_base;
  31. int reg_ofs_cur = memif_data->reg_ofs_cur;
  32. unsigned int hw_ptr = 0, hw_base = 0;
  33. int ret, pcm_ptr_bytes;
  34. ret = regmap_read(regmap, reg_ofs_cur, &hw_ptr);
  35. if (ret || hw_ptr == 0) {
  36. dev_err(dev, "%s hw_ptr err\n", __func__);
  37. pcm_ptr_bytes = 0;
  38. goto POINTER_RETURN_FRAMES;
  39. }
  40. ret = regmap_read(regmap, reg_ofs_base, &hw_base);
  41. if (ret || hw_base == 0) {
  42. dev_err(dev, "%s hw_ptr err\n", __func__);
  43. pcm_ptr_bytes = 0;
  44. goto POINTER_RETURN_FRAMES;
  45. }
  46. pcm_ptr_bytes = hw_ptr - hw_base;
  47. POINTER_RETURN_FRAMES:
  48. return bytes_to_frames(substream->runtime, pcm_ptr_bytes);
  49. }
  50. static const struct snd_pcm_ops mtk_afe_pcm_ops = {
  51. .ioctl = snd_pcm_lib_ioctl,
  52. .pointer = mtk_afe_pcm_pointer,
  53. };
  54. static int mtk_afe_pcm_new(struct snd_soc_pcm_runtime *rtd)
  55. {
  56. size_t size;
  57. struct snd_card *card = rtd->card->snd_card;
  58. struct snd_pcm *pcm = rtd->pcm;
  59. struct mtk_base_afe *afe = snd_soc_platform_get_drvdata(rtd->platform);
  60. size = afe->mtk_afe_hardware->buffer_bytes_max;
  61. return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  62. card->dev, size, size);
  63. }
  64. static void mtk_afe_pcm_free(struct snd_pcm *pcm)
  65. {
  66. snd_pcm_lib_preallocate_free_for_all(pcm);
  67. }
  68. const struct snd_soc_platform_driver mtk_afe_pcm_platform = {
  69. .ops = &mtk_afe_pcm_ops,
  70. .pcm_new = mtk_afe_pcm_new,
  71. .pcm_free = mtk_afe_pcm_free,
  72. };
  73. EXPORT_SYMBOL_GPL(mtk_afe_pcm_platform);
  74. MODULE_DESCRIPTION("Mediatek simple platform driver");
  75. MODULE_AUTHOR("Garlic Tseng <[email protected]>");
  76. MODULE_LICENSE("GPL v2");