aiu.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. 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. 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. };
  85. static struct snd_soc_dai_driver aiu_cpu_dai_drv[] = {
  86. [CPU_I2S_FIFO] = {
  87. .name = "I2S FIFO",
  88. .playback = {
  89. .stream_name = "I2S FIFO Playback",
  90. .channels_min = 2,
  91. .channels_max = 8,
  92. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  93. .rate_min = 5512,
  94. .rate_max = 192000,
  95. .formats = AIU_FORMATS,
  96. },
  97. .ops = &aiu_fifo_i2s_dai_ops,
  98. .pcm_new = aiu_fifo_pcm_new,
  99. .probe = aiu_fifo_i2s_dai_probe,
  100. .remove = aiu_fifo_dai_remove,
  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. .pcm_new = aiu_fifo_pcm_new,
  115. .probe = aiu_fifo_spdif_dai_probe,
  116. .remove = aiu_fifo_dai_remove,
  117. },
  118. [CPU_I2S_ENCODER] = {
  119. .name = "I2S Encoder",
  120. .playback = {
  121. .stream_name = "I2S Encoder Playback",
  122. .channels_min = 2,
  123. .channels_max = 8,
  124. .rates = SNDRV_PCM_RATE_8000_192000,
  125. .formats = AIU_FORMATS,
  126. },
  127. .ops = &aiu_encoder_i2s_dai_ops,
  128. },
  129. [CPU_SPDIF_ENCODER] = {
  130. .name = "SPDIF Encoder",
  131. .playback = {
  132. .stream_name = "SPDIF Encoder Playback",
  133. .channels_min = 2,
  134. .channels_max = 2,
  135. .rates = (SNDRV_PCM_RATE_32000 |
  136. SNDRV_PCM_RATE_44100 |
  137. SNDRV_PCM_RATE_48000 |
  138. SNDRV_PCM_RATE_88200 |
  139. SNDRV_PCM_RATE_96000 |
  140. SNDRV_PCM_RATE_176400 |
  141. SNDRV_PCM_RATE_192000),
  142. .formats = AIU_FORMATS,
  143. },
  144. .ops = &aiu_encoder_spdif_dai_ops,
  145. }
  146. };
  147. static const struct regmap_config aiu_regmap_cfg = {
  148. .reg_bits = 32,
  149. .val_bits = 32,
  150. .reg_stride = 4,
  151. .max_register = 0x2ac,
  152. };
  153. static int aiu_clk_bulk_get(struct device *dev,
  154. const char * const *ids,
  155. unsigned int num,
  156. struct aiu_interface *interface)
  157. {
  158. struct clk_bulk_data *clks;
  159. int i, ret;
  160. clks = devm_kcalloc(dev, num, sizeof(*clks), GFP_KERNEL);
  161. if (!clks)
  162. return -ENOMEM;
  163. for (i = 0; i < num; i++)
  164. clks[i].id = ids[i];
  165. ret = devm_clk_bulk_get(dev, num, clks);
  166. if (ret < 0)
  167. return ret;
  168. interface->clks = clks;
  169. interface->clk_num = num;
  170. return 0;
  171. }
  172. static const char * const aiu_i2s_ids[] = {
  173. [PCLK] = "i2s_pclk",
  174. [AOCLK] = "i2s_aoclk",
  175. [MCLK] = "i2s_mclk",
  176. [MIXER] = "i2s_mixer",
  177. };
  178. static const char * const aiu_spdif_ids[] = {
  179. [PCLK] = "spdif_pclk",
  180. [AOCLK] = "spdif_aoclk",
  181. [MCLK] = "spdif_mclk_sel"
  182. };
  183. static int aiu_clk_get(struct device *dev)
  184. {
  185. struct aiu *aiu = dev_get_drvdata(dev);
  186. int ret;
  187. aiu->pclk = devm_clk_get(dev, "pclk");
  188. if (IS_ERR(aiu->pclk)) {
  189. if (PTR_ERR(aiu->pclk) != -EPROBE_DEFER)
  190. dev_err(dev, "Can't get the aiu pclk\n");
  191. return PTR_ERR(aiu->pclk);
  192. }
  193. aiu->spdif_mclk = devm_clk_get(dev, "spdif_mclk");
  194. if (IS_ERR(aiu->spdif_mclk)) {
  195. if (PTR_ERR(aiu->spdif_mclk) != -EPROBE_DEFER)
  196. dev_err(dev, "Can't get the aiu spdif master clock\n");
  197. return PTR_ERR(aiu->spdif_mclk);
  198. }
  199. ret = aiu_clk_bulk_get(dev, aiu_i2s_ids, ARRAY_SIZE(aiu_i2s_ids),
  200. &aiu->i2s);
  201. if (ret) {
  202. if (ret != -EPROBE_DEFER)
  203. dev_err(dev, "Can't get the i2s clocks\n");
  204. return ret;
  205. }
  206. ret = aiu_clk_bulk_get(dev, aiu_spdif_ids, ARRAY_SIZE(aiu_spdif_ids),
  207. &aiu->spdif);
  208. if (ret) {
  209. if (ret != -EPROBE_DEFER)
  210. dev_err(dev, "Can't get the spdif clocks\n");
  211. return ret;
  212. }
  213. ret = clk_prepare_enable(aiu->pclk);
  214. if (ret) {
  215. dev_err(dev, "peripheral clock enable failed\n");
  216. return ret;
  217. }
  218. ret = devm_add_action_or_reset(dev,
  219. (void(*)(void *))clk_disable_unprepare,
  220. aiu->pclk);
  221. if (ret)
  222. dev_err(dev, "failed to add reset action on pclk");
  223. return ret;
  224. }
  225. static int aiu_probe(struct platform_device *pdev)
  226. {
  227. struct device *dev = &pdev->dev;
  228. void __iomem *regs;
  229. struct regmap *map;
  230. struct aiu *aiu;
  231. int ret;
  232. aiu = devm_kzalloc(dev, sizeof(*aiu), GFP_KERNEL);
  233. if (!aiu)
  234. return -ENOMEM;
  235. aiu->platform = device_get_match_data(dev);
  236. if (!aiu->platform)
  237. return -ENODEV;
  238. platform_set_drvdata(pdev, aiu);
  239. ret = device_reset(dev);
  240. if (ret) {
  241. if (ret != -EPROBE_DEFER)
  242. dev_err(dev, "Failed to reset device\n");
  243. return ret;
  244. }
  245. regs = devm_platform_ioremap_resource(pdev, 0);
  246. if (IS_ERR(regs))
  247. return PTR_ERR(regs);
  248. map = devm_regmap_init_mmio(dev, regs, &aiu_regmap_cfg);
  249. if (IS_ERR(map)) {
  250. dev_err(dev, "failed to init regmap: %ld\n",
  251. PTR_ERR(map));
  252. return PTR_ERR(map);
  253. }
  254. aiu->i2s.irq = platform_get_irq_byname(pdev, "i2s");
  255. if (aiu->i2s.irq < 0)
  256. return aiu->i2s.irq;
  257. aiu->spdif.irq = platform_get_irq_byname(pdev, "spdif");
  258. if (aiu->spdif.irq < 0)
  259. return aiu->spdif.irq;
  260. ret = aiu_clk_get(dev);
  261. if (ret)
  262. return ret;
  263. /* Register the cpu component of the aiu */
  264. ret = snd_soc_register_component(dev, &aiu_cpu_component,
  265. aiu_cpu_dai_drv,
  266. ARRAY_SIZE(aiu_cpu_dai_drv));
  267. if (ret) {
  268. dev_err(dev, "Failed to register cpu component\n");
  269. return ret;
  270. }
  271. /* Register the hdmi codec control component */
  272. ret = aiu_hdmi_ctrl_register_component(dev);
  273. if (ret) {
  274. dev_err(dev, "Failed to register hdmi control component\n");
  275. goto err;
  276. }
  277. /* Register the internal dac control component on gxl */
  278. if (aiu->platform->has_acodec) {
  279. ret = aiu_acodec_ctrl_register_component(dev);
  280. if (ret) {
  281. dev_err(dev,
  282. "Failed to register acodec control component\n");
  283. goto err;
  284. }
  285. }
  286. return 0;
  287. err:
  288. snd_soc_unregister_component(dev);
  289. return ret;
  290. }
  291. static int aiu_remove(struct platform_device *pdev)
  292. {
  293. snd_soc_unregister_component(&pdev->dev);
  294. return 0;
  295. }
  296. static const struct aiu_platform_data aiu_gxbb_pdata = {
  297. .has_acodec = false,
  298. .has_clk_ctrl_more_i2s_div = true,
  299. };
  300. static const struct aiu_platform_data aiu_gxl_pdata = {
  301. .has_acodec = true,
  302. .has_clk_ctrl_more_i2s_div = true,
  303. };
  304. static const struct aiu_platform_data aiu_meson8_pdata = {
  305. .has_acodec = false,
  306. .has_clk_ctrl_more_i2s_div = false,
  307. };
  308. static const struct of_device_id aiu_of_match[] = {
  309. { .compatible = "amlogic,aiu-gxbb", .data = &aiu_gxbb_pdata },
  310. { .compatible = "amlogic,aiu-gxl", .data = &aiu_gxl_pdata },
  311. { .compatible = "amlogic,aiu-meson8", .data = &aiu_meson8_pdata },
  312. { .compatible = "amlogic,aiu-meson8b", .data = &aiu_meson8_pdata },
  313. {}
  314. };
  315. MODULE_DEVICE_TABLE(of, aiu_of_match);
  316. static struct platform_driver aiu_pdrv = {
  317. .probe = aiu_probe,
  318. .remove = aiu_remove,
  319. .driver = {
  320. .name = "meson-aiu",
  321. .of_match_table = aiu_of_match,
  322. },
  323. };
  324. module_platform_driver(aiu_pdrv);
  325. MODULE_DESCRIPTION("Meson AIU Driver");
  326. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  327. MODULE_LICENSE("GPL v2");