storm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, "invalid bit width given: %d\n", bitwidth);
  36. return bitwidth;
  37. }
  38. /*
  39. * as the CPU DAI is the I2S bus master and no system clock is needed by
  40. * the MAX98357a DAC, simply set the system clock to be a constant
  41. * multiple of the bit clock for the clock divider
  42. */
  43. sysclk_freq = rate * bitwidth * 2 * STORM_SYSCLK_MULT;
  44. ret = snd_soc_dai_set_sysclk(soc_runtime->cpu_dai, 0, sysclk_freq, 0);
  45. if (ret) {
  46. dev_err(card->dev, "error setting sysclk to %u: %d\n",
  47. sysclk_freq, ret);
  48. return ret;
  49. }
  50. return 0;
  51. }
  52. static const struct snd_soc_ops storm_soc_ops = {
  53. .hw_params = storm_ops_hw_params,
  54. };
  55. static struct snd_soc_dai_link storm_dai_link = {
  56. .name = "Primary",
  57. .stream_name = "Primary",
  58. .codec_dai_name = "HiFi",
  59. .ops = &storm_soc_ops,
  60. };
  61. static int storm_parse_of(struct snd_soc_card *card)
  62. {
  63. struct snd_soc_dai_link *dai_link = card->dai_link;
  64. struct device_node *np = card->dev->of_node;
  65. dai_link->cpu_of_node = of_parse_phandle(np, "cpu", 0);
  66. if (!dai_link->cpu_of_node) {
  67. dev_err(card->dev, "error getting cpu phandle\n");
  68. return -EINVAL;
  69. }
  70. dai_link->platform_of_node = dai_link->cpu_of_node;
  71. dai_link->codec_of_node = of_parse_phandle(np, "codec", 0);
  72. if (!dai_link->codec_of_node) {
  73. dev_err(card->dev, "error getting codec phandle\n");
  74. return -EINVAL;
  75. }
  76. return 0;
  77. }
  78. static int storm_platform_probe(struct platform_device *pdev)
  79. {
  80. struct snd_soc_card *card;
  81. int ret;
  82. card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
  83. if (!card)
  84. return -ENOMEM;
  85. card->dev = &pdev->dev;
  86. card->owner = THIS_MODULE;
  87. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  88. if (ret) {
  89. dev_err(&pdev->dev, "error parsing card name: %d\n", ret);
  90. return ret;
  91. }
  92. card->dai_link = &storm_dai_link;
  93. card->num_links = 1;
  94. ret = storm_parse_of(card);
  95. if (ret) {
  96. dev_err(&pdev->dev, "error resolving dai links: %d\n", ret);
  97. return ret;
  98. }
  99. ret = devm_snd_soc_register_card(&pdev->dev, card);
  100. if (ret)
  101. dev_err(&pdev->dev, "error registering soundcard: %d\n", ret);
  102. return ret;
  103. }
  104. #ifdef CONFIG_OF
  105. static const struct of_device_id storm_device_id[] = {
  106. { .compatible = "google,storm-audio" },
  107. {},
  108. };
  109. MODULE_DEVICE_TABLE(of, storm_device_id);
  110. #endif
  111. static struct platform_driver storm_platform_driver = {
  112. .driver = {
  113. .name = "storm-audio",
  114. .of_match_table =
  115. of_match_ptr(storm_device_id),
  116. },
  117. .probe = storm_platform_probe,
  118. };
  119. module_platform_driver(storm_platform_driver);
  120. MODULE_DESCRIPTION("QTi IPQ806x-based Storm Machine Driver");
  121. MODULE_LICENSE("GPL v2");