storm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * storm.c -- ALSA SoC machine driver for QTi ipq806x-based Storm board
  14. */
  15. #include <linux/device.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/platform_device.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #define STORM_SYSCLK_MULT 4
  24. static int storm_ops_hw_params(struct snd_pcm_substream *substream,
  25. struct snd_pcm_hw_params *params)
  26. {
  27. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  28. struct snd_soc_card *card = soc_runtime->card;
  29. snd_pcm_format_t format = params_format(params);
  30. unsigned int rate = params_rate(params);
  31. unsigned int sysclk_freq;
  32. int bitwidth, ret;
  33. bitwidth = snd_pcm_format_width(format);
  34. if (bitwidth < 0) {
  35. dev_err(card->dev, "%s() invalid bit width given: %d\n",
  36. __func__, bitwidth);
  37. return bitwidth;
  38. }
  39. /*
  40. * as the CPU DAI is the I2S bus master and no system clock is needed by
  41. * the MAX98357a DAC, simply set the system clock to be a constant
  42. * multiple of the bit clock for the clock divider
  43. */
  44. sysclk_freq = rate * bitwidth * 2 * STORM_SYSCLK_MULT;
  45. ret = snd_soc_dai_set_sysclk(soc_runtime->cpu_dai, 0, sysclk_freq, 0);
  46. if (ret) {
  47. dev_err(card->dev, "%s() error setting sysclk to %u: %d\n",
  48. __func__, sysclk_freq, ret);
  49. return ret;
  50. }
  51. return 0;
  52. }
  53. static struct snd_soc_ops storm_soc_ops = {
  54. .hw_params = storm_ops_hw_params,
  55. };
  56. static struct snd_soc_dai_link storm_dai_link = {
  57. .name = "Primary",
  58. .stream_name = "Primary",
  59. .codec_dai_name = "HiFi",
  60. .ops = &storm_soc_ops,
  61. };
  62. static int storm_parse_of(struct snd_soc_card *card)
  63. {
  64. struct snd_soc_dai_link *dai_link = card->dai_link;
  65. struct device_node *np = card->dev->of_node;
  66. dai_link->cpu_of_node = of_parse_phandle(np, "cpu", 0);
  67. if (!dai_link->cpu_of_node) {
  68. dev_err(card->dev, "%s() error getting cpu phandle\n",
  69. __func__);
  70. return -EINVAL;
  71. }
  72. dai_link->platform_of_node = dai_link->cpu_of_node;
  73. dai_link->codec_of_node = of_parse_phandle(np, "codec", 0);
  74. if (!dai_link->codec_of_node) {
  75. dev_err(card->dev, "%s() error getting codec phandle\n",
  76. __func__);
  77. return -EINVAL;
  78. }
  79. return 0;
  80. }
  81. static int storm_platform_probe(struct platform_device *pdev)
  82. {
  83. struct snd_soc_card *card;
  84. int ret;
  85. card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
  86. if (!card)
  87. return -ENOMEM;
  88. card->dev = &pdev->dev;
  89. platform_set_drvdata(pdev, card);
  90. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  91. if (ret) {
  92. dev_err(&pdev->dev, "%s() error parsing card name: %d\n",
  93. __func__, ret);
  94. return ret;
  95. }
  96. card->dai_link = &storm_dai_link;
  97. card->num_links = 1;
  98. ret = storm_parse_of(card);
  99. if (ret) {
  100. dev_err(&pdev->dev, "%s() error resolving dai links: %d\n",
  101. __func__, ret);
  102. return ret;
  103. }
  104. ret = devm_snd_soc_register_card(&pdev->dev, card);
  105. if (ret)
  106. dev_err(&pdev->dev, "%s() error registering soundcard: %d\n",
  107. __func__, ret);
  108. return ret;
  109. }
  110. #ifdef CONFIG_OF
  111. static const struct of_device_id storm_device_id[] = {
  112. { .compatible = "google,storm-audio" },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, storm_device_id);
  116. #endif
  117. static struct platform_driver storm_platform_driver = {
  118. .driver = {
  119. .name = "storm-audio",
  120. .of_match_table =
  121. of_match_ptr(storm_device_id),
  122. },
  123. .probe = storm_platform_probe,
  124. };
  125. module_platform_driver(storm_platform_driver);
  126. MODULE_DESCRIPTION("QTi IPQ806x-based Storm Machine Driver");
  127. MODULE_LICENSE("GPL v2");