aiu.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/module.h>
  8. #include <linux/of_platform.h>
  9. #include <linux/regmap.h>
  10. #include <linux/reset.h>
  11. #include <sound/soc.h>
  12. #include <sound/soc-dai.h>
  13. #include <dt-bindings/sound/meson-aiu.h>
  14. #include "aiu.h"
  15. #include "aiu-fifo.h"
  16. #define AIU_I2S_MISC_958_SRC_SHIFT 3
  17. static const char * const aiu_spdif_encode_sel_texts[] = {
  18. "SPDIF", "I2S",
  19. };
  20. static SOC_ENUM_SINGLE_DECL(aiu_spdif_encode_sel_enum, AIU_I2S_MISC,
  21. AIU_I2S_MISC_958_SRC_SHIFT,
  22. aiu_spdif_encode_sel_texts);
  23. static const struct snd_kcontrol_new aiu_spdif_encode_mux =
  24. SOC_DAPM_ENUM("SPDIF Buffer Src", aiu_spdif_encode_sel_enum);
  25. static const struct snd_soc_dapm_widget aiu_cpu_dapm_widgets[] = {
  26. SND_SOC_DAPM_MUX("SPDIF SRC SEL", SND_SOC_NOPM, 0, 0,
  27. &aiu_spdif_encode_mux),
  28. };
  29. static const struct snd_soc_dapm_route aiu_cpu_dapm_routes[] = {
  30. { "I2S Encoder Playback", NULL, "I2S FIFO Playback" },
  31. { "SPDIF SRC SEL", "SPDIF", "SPDIF FIFO Playback" },
  32. { "SPDIF SRC SEL", "I2S", "I2S FIFO Playback" },
  33. { "SPDIF Encoder Playback", NULL, "SPDIF SRC SEL" },
  34. };
  35. int aiu_of_xlate_dai_name(struct snd_soc_component *component,
  36. const struct of_phandle_args *args,
  37. const char **dai_name,
  38. unsigned int component_id)
  39. {
  40. struct snd_soc_dai *dai;
  41. int id;
  42. if (args->args_count != 2)
  43. return -EINVAL;
  44. if (args->args[0] != component_id)
  45. return -EINVAL;
  46. id = args->args[1];
  47. if (id < 0 || id >= component->num_dai)
  48. return -EINVAL;
  49. for_each_component_dais(component, dai) {
  50. if (id == 0)
  51. break;
  52. id--;
  53. }
  54. *dai_name = dai->driver->name;
  55. return 0;
  56. }
  57. static int aiu_cpu_of_xlate_dai_name(struct snd_soc_component *component,
  58. const struct of_phandle_args *args,
  59. const char **dai_name)
  60. {
  61. return aiu_of_xlate_dai_name(component, args, dai_name, AIU_CPU);
  62. }
  63. static int aiu_cpu_component_probe(struct snd_soc_component *component)
  64. {
  65. struct aiu *aiu = snd_soc_component_get_drvdata(component);
  66. /* Required for the SPDIF Source control operation */
  67. return clk_prepare_enable(aiu->i2s.clks[PCLK].clk);
  68. }
  69. static void aiu_cpu_component_remove(struct snd_soc_component *component)
  70. {
  71. struct aiu *aiu = snd_soc_component_get_drvdata(component);
  72. clk_disable_unprepare(aiu->i2s.clks[PCLK].clk);
  73. }
  74. static const struct snd_soc_component_driver aiu_cpu_component = {
  75. .name = "AIU CPU",
  76. .dapm_widgets = aiu_cpu_dapm_widgets,
  77. .num_dapm_widgets = ARRAY_SIZE(aiu_cpu_dapm_widgets),
  78. .dapm_routes = aiu_cpu_dapm_routes,
  79. .num_dapm_routes = ARRAY_SIZE(aiu_cpu_dapm_routes),
  80. .of_xlate_dai_name = aiu_cpu_of_xlate_dai_name,
  81. .pointer = aiu_fifo_pointer,
  82. .probe = aiu_cpu_component_probe,
  83. .remove = aiu_cpu_component_remove,
  84. #ifdef CONFIG_DEBUG_FS
  85. .debugfs_prefix = "cpu",
  86. #endif
  87. };
  88. static struct snd_soc_dai_driver aiu_cpu_dai_drv[] = {
  89. [CPU_I2S_FIFO] = {
  90. .name = "I2S FIFO",
  91. .playback = {
  92. .stream_name = "I2S FIFO Playback",
  93. .channels_min = 2,
  94. .channels_max = 8,
  95. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  96. .rate_min = 5512,
  97. .rate_max = 192000,
  98. .formats = AIU_FORMATS,
  99. },
  100. .ops = &aiu_fifo_i2s_dai_ops,
  101. },
  102. [CPU_SPDIF_FIFO] = {
  103. .name = "SPDIF FIFO",
  104. .playback = {
  105. .stream_name = "SPDIF FIFO Playback",
  106. .channels_min = 2,
  107. .channels_max = 2,
  108. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  109. .rate_min = 5512,
  110. .rate_max = 192000,
  111. .formats = AIU_FORMATS,
  112. },
  113. .ops = &aiu_fifo_spdif_dai_ops,
  114. },
  115. [CPU_I2S_ENCODER] = {
  116. .name = "I2S Encoder",
  117. .playback = {
  118. .stream_name = "I2S Encoder Playback",
  119. .channels_min = 2,
  120. .channels_max = 8,
  121. .rates = SNDRV_PCM_RATE_8000_192000,
  122. .formats = AIU_FORMATS,
  123. },
  124. .ops = &aiu_encoder_i2s_dai_ops,
  125. },
  126. [CPU_SPDIF_ENCODER] = {
  127. .name = "SPDIF Encoder",
  128. .playback = {
  129. .stream_name = "SPDIF Encoder Playback",
  130. .channels_min = 2,
  131. .channels_max = 2,
  132. .rates = (SNDRV_PCM_RATE_32000 |
  133. SNDRV_PCM_RATE_44100 |
  134. SNDRV_PCM_RATE_48000 |
  135. SNDRV_PCM_RATE_88200 |
  136. SNDRV_PCM_RATE_96000 |
  137. SNDRV_PCM_RATE_176400 |
  138. SNDRV_PCM_RATE_192000),
  139. .formats = AIU_FORMATS,
  140. },
  141. .ops = &aiu_encoder_spdif_dai_ops,
  142. }
  143. };
  144. static const struct regmap_config aiu_regmap_cfg = {
  145. .reg_bits = 32,
  146. .val_bits = 32,
  147. .reg_stride = 4,
  148. .max_register = 0x2ac,
  149. };
  150. static int aiu_clk_bulk_get(struct device *dev,
  151. const char * const *ids,
  152. unsigned int num,
  153. struct aiu_interface *interface)
  154. {
  155. struct clk_bulk_data *clks;
  156. int i, ret;
  157. clks = devm_kcalloc(dev, num, sizeof(*clks), GFP_KERNEL);
  158. if (!clks)
  159. return -ENOMEM;
  160. for (i = 0; i < num; i++)
  161. clks[i].id = ids[i];
  162. ret = devm_clk_bulk_get(dev, num, clks);
  163. if (ret < 0)
  164. return ret;
  165. interface->clks = clks;
  166. interface->clk_num = num;
  167. return 0;
  168. }
  169. static const char * const aiu_i2s_ids[] = {
  170. [PCLK] = "i2s_pclk",
  171. [AOCLK] = "i2s_aoclk",
  172. [MCLK] = "i2s_mclk",
  173. [MIXER] = "i2s_mixer",
  174. };
  175. static const char * const aiu_spdif_ids[] = {
  176. [PCLK] = "spdif_pclk",
  177. [AOCLK] = "spdif_aoclk",
  178. [MCLK] = "spdif_mclk_sel"
  179. };
  180. static int aiu_clk_get(struct device *dev)
  181. {
  182. struct aiu *aiu = dev_get_drvdata(dev);
  183. struct clk *pclk;
  184. int ret;
  185. pclk = devm_clk_get_enabled(dev, "pclk");
  186. if (IS_ERR(pclk))
  187. return dev_err_probe(dev, PTR_ERR(pclk), "Can't get the aiu pclk\n");
  188. aiu->spdif_mclk = devm_clk_get(dev, "spdif_mclk");
  189. if (IS_ERR(aiu->spdif_mclk))
  190. return dev_err_probe(dev, PTR_ERR(aiu->spdif_mclk),
  191. "Can't get the aiu spdif master clock\n");
  192. ret = aiu_clk_bulk_get(dev, aiu_i2s_ids, ARRAY_SIZE(aiu_i2s_ids),
  193. &aiu->i2s);
  194. if (ret)
  195. return dev_err_probe(dev, ret, "Can't get the i2s clocks\n");
  196. ret = aiu_clk_bulk_get(dev, aiu_spdif_ids, ARRAY_SIZE(aiu_spdif_ids),
  197. &aiu->spdif);
  198. if (ret)
  199. return dev_err_probe(dev, ret, "Can't get the spdif clocks\n");
  200. return ret;
  201. }
  202. static int aiu_probe(struct platform_device *pdev)
  203. {
  204. struct device *dev = &pdev->dev;
  205. void __iomem *regs;
  206. struct regmap *map;
  207. struct aiu *aiu;
  208. int ret;
  209. aiu = devm_kzalloc(dev, sizeof(*aiu), GFP_KERNEL);
  210. if (!aiu)
  211. return -ENOMEM;
  212. aiu->platform = device_get_match_data(dev);
  213. if (!aiu->platform)
  214. return -ENODEV;
  215. platform_set_drvdata(pdev, aiu);
  216. ret = device_reset(dev);
  217. if (ret)
  218. return dev_err_probe(dev, ret, "Failed to reset device\n");
  219. regs = devm_platform_ioremap_resource(pdev, 0);
  220. if (IS_ERR(regs))
  221. return PTR_ERR(regs);
  222. map = devm_regmap_init_mmio(dev, regs, &aiu_regmap_cfg);
  223. if (IS_ERR(map)) {
  224. dev_err(dev, "failed to init regmap: %ld\n",
  225. PTR_ERR(map));
  226. return PTR_ERR(map);
  227. }
  228. aiu->i2s.irq = platform_get_irq_byname(pdev, "i2s");
  229. if (aiu->i2s.irq < 0)
  230. return aiu->i2s.irq;
  231. aiu->spdif.irq = platform_get_irq_byname(pdev, "spdif");
  232. if (aiu->spdif.irq < 0)
  233. return aiu->spdif.irq;
  234. ret = aiu_clk_get(dev);
  235. if (ret)
  236. return ret;
  237. /* Register the cpu component of the aiu */
  238. ret = snd_soc_register_component(dev, &aiu_cpu_component,
  239. aiu_cpu_dai_drv,
  240. ARRAY_SIZE(aiu_cpu_dai_drv));
  241. if (ret) {
  242. dev_err(dev, "Failed to register cpu component\n");
  243. return ret;
  244. }
  245. /* Register the hdmi codec control component */
  246. ret = aiu_hdmi_ctrl_register_component(dev);
  247. if (ret) {
  248. dev_err(dev, "Failed to register hdmi control component\n");
  249. goto err;
  250. }
  251. /* Register the internal dac control component on gxl */
  252. if (aiu->platform->has_acodec) {
  253. ret = aiu_acodec_ctrl_register_component(dev);
  254. if (ret) {
  255. dev_err(dev,
  256. "Failed to register acodec control component\n");
  257. goto err;
  258. }
  259. }
  260. return 0;
  261. err:
  262. snd_soc_unregister_component(dev);
  263. return ret;
  264. }
  265. static void aiu_remove(struct platform_device *pdev)
  266. {
  267. snd_soc_unregister_component(&pdev->dev);
  268. }
  269. static const struct aiu_platform_data aiu_gxbb_pdata = {
  270. .has_acodec = false,
  271. .has_clk_ctrl_more_i2s_div = true,
  272. };
  273. static const struct aiu_platform_data aiu_gxl_pdata = {
  274. .has_acodec = true,
  275. .has_clk_ctrl_more_i2s_div = true,
  276. };
  277. static const struct aiu_platform_data aiu_meson8_pdata = {
  278. .has_acodec = false,
  279. .has_clk_ctrl_more_i2s_div = false,
  280. };
  281. static const struct of_device_id aiu_of_match[] = {
  282. { .compatible = "amlogic,aiu-gxbb", .data = &aiu_gxbb_pdata },
  283. { .compatible = "amlogic,aiu-gxl", .data = &aiu_gxl_pdata },
  284. { .compatible = "amlogic,aiu-meson8", .data = &aiu_meson8_pdata },
  285. { .compatible = "amlogic,aiu-meson8b", .data = &aiu_meson8_pdata },
  286. {}
  287. };
  288. MODULE_DEVICE_TABLE(of, aiu_of_match);
  289. static struct platform_driver aiu_pdrv = {
  290. .probe = aiu_probe,
  291. .remove = aiu_remove,
  292. .driver = {
  293. .name = "meson-aiu",
  294. .of_match_table = aiu_of_match,
  295. },
  296. };
  297. module_platform_driver(aiu_pdrv);
  298. MODULE_DESCRIPTION("Meson AIU Driver");
  299. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  300. MODULE_LICENSE("GPL v2");