storm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
  4. *
  5. * storm.c -- ALSA SoC machine driver for QTi ipq806x-based Storm board
  6. */
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/platform_device.h>
  12. #include <sound/pcm.h>
  13. #include <sound/pcm_params.h>
  14. #include <sound/soc.h>
  15. #define STORM_SYSCLK_MULT 4
  16. static int storm_ops_hw_params(struct snd_pcm_substream *substream,
  17. struct snd_pcm_hw_params *params)
  18. {
  19. struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
  20. struct snd_soc_card *card = soc_runtime->card;
  21. snd_pcm_format_t format = params_format(params);
  22. unsigned int rate = params_rate(params);
  23. unsigned int sysclk_freq;
  24. int bitwidth, ret;
  25. bitwidth = snd_pcm_format_width(format);
  26. if (bitwidth < 0) {
  27. dev_err(card->dev, "invalid bit width given: %d\n", bitwidth);
  28. return bitwidth;
  29. }
  30. /*
  31. * as the CPU DAI is the I2S bus master and no system clock is needed by
  32. * the MAX98357a DAC, simply set the system clock to be a constant
  33. * multiple of the bit clock for the clock divider
  34. */
  35. sysclk_freq = rate * bitwidth * 2 * STORM_SYSCLK_MULT;
  36. ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(soc_runtime, 0), 0, sysclk_freq, 0);
  37. if (ret) {
  38. dev_err(card->dev, "error setting sysclk to %u: %d\n",
  39. sysclk_freq, ret);
  40. return ret;
  41. }
  42. return 0;
  43. }
  44. static const struct snd_soc_ops storm_soc_ops = {
  45. .hw_params = storm_ops_hw_params,
  46. };
  47. SND_SOC_DAILINK_DEFS(hifi,
  48. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  49. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
  50. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  51. static struct snd_soc_dai_link storm_dai_link = {
  52. .name = "Primary",
  53. .stream_name = "Primary",
  54. .ops = &storm_soc_ops,
  55. SND_SOC_DAILINK_REG(hifi),
  56. };
  57. static int storm_parse_of(struct snd_soc_card *card)
  58. {
  59. struct snd_soc_dai_link *dai_link = card->dai_link;
  60. struct device_node *np = card->dev->of_node;
  61. dai_link->cpus->of_node = of_parse_phandle(np, "cpu", 0);
  62. if (!dai_link->cpus->of_node) {
  63. dev_err(card->dev, "error getting cpu phandle\n");
  64. return -EINVAL;
  65. }
  66. dai_link->platforms->of_node = dai_link->cpus->of_node;
  67. dai_link->codecs->of_node = of_parse_phandle(np, "codec", 0);
  68. if (!dai_link->codecs->of_node) {
  69. dev_err(card->dev, "error getting codec phandle\n");
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. static int storm_platform_probe(struct platform_device *pdev)
  75. {
  76. struct snd_soc_card *card;
  77. int ret;
  78. card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
  79. if (!card)
  80. return -ENOMEM;
  81. card->dev = &pdev->dev;
  82. card->owner = THIS_MODULE;
  83. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  84. if (ret) {
  85. dev_err(&pdev->dev, "error parsing card name: %d\n", ret);
  86. return ret;
  87. }
  88. card->dai_link = &storm_dai_link;
  89. card->num_links = 1;
  90. ret = storm_parse_of(card);
  91. if (ret) {
  92. dev_err(&pdev->dev, "error resolving dai links: %d\n", ret);
  93. return ret;
  94. }
  95. ret = devm_snd_soc_register_card(&pdev->dev, card);
  96. if (ret)
  97. dev_err(&pdev->dev, "error registering soundcard: %d\n", ret);
  98. return ret;
  99. }
  100. #ifdef CONFIG_OF
  101. static const struct of_device_id storm_device_id[] = {
  102. { .compatible = "google,storm-audio" },
  103. {},
  104. };
  105. MODULE_DEVICE_TABLE(of, storm_device_id);
  106. #endif
  107. static struct platform_driver storm_platform_driver = {
  108. .driver = {
  109. .name = "storm-audio",
  110. .of_match_table =
  111. of_match_ptr(storm_device_id),
  112. },
  113. .probe = storm_platform_probe,
  114. };
  115. module_platform_driver(storm_platform_driver);
  116. MODULE_DESCRIPTION("QTi IPQ806x-based Storm Machine Driver");
  117. MODULE_LICENSE("GPL v2");