sof_wm8804.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (c) 2018-2020, Intel Corporation
  3. //
  4. // sof-wm8804.c - ASoC machine driver for Up and Up2 board
  5. // based on WM8804/Hifiberry Digi+
  6. #include <linux/acpi.h>
  7. #include <linux/dmi.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/gpio/machine.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/soc.h>
  16. #include <sound/soc-acpi.h>
  17. #include "../../codecs/wm8804.h"
  18. struct sof_card_private {
  19. struct gpio_desc *gpio_44;
  20. struct gpio_desc *gpio_48;
  21. int sample_rate;
  22. };
  23. #define SOF_WM8804_UP2_QUIRK BIT(0)
  24. static unsigned long sof_wm8804_quirk;
  25. static int sof_wm8804_quirk_cb(const struct dmi_system_id *id)
  26. {
  27. sof_wm8804_quirk = (unsigned long)id->driver_data;
  28. return 1;
  29. }
  30. static const struct dmi_system_id sof_wm8804_quirk_table[] = {
  31. {
  32. .callback = sof_wm8804_quirk_cb,
  33. .matches = {
  34. DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
  35. DMI_MATCH(DMI_PRODUCT_NAME, "UP-APL01"),
  36. },
  37. .driver_data = (void *)SOF_WM8804_UP2_QUIRK,
  38. },
  39. {}
  40. };
  41. static int sof_wm8804_hw_params(struct snd_pcm_substream *substream,
  42. struct snd_pcm_hw_params *params)
  43. {
  44. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  45. struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
  46. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  47. struct snd_soc_component *codec = codec_dai->component;
  48. const int sysclk = 27000000; /* This is fixed on this board */
  49. int samplerate;
  50. long mclk_freq;
  51. int mclk_div;
  52. int sampling_freq;
  53. bool clk_44;
  54. int ret;
  55. samplerate = params_rate(params);
  56. if (samplerate == ctx->sample_rate)
  57. return 0;
  58. ctx->sample_rate = 0;
  59. if (samplerate <= 96000) {
  60. mclk_freq = samplerate * 256;
  61. mclk_div = WM8804_MCLKDIV_256FS;
  62. } else {
  63. mclk_freq = samplerate * 128;
  64. mclk_div = WM8804_MCLKDIV_128FS;
  65. }
  66. switch (samplerate) {
  67. case 32000:
  68. sampling_freq = 0x03;
  69. break;
  70. case 44100:
  71. sampling_freq = 0x00;
  72. break;
  73. case 48000:
  74. sampling_freq = 0x02;
  75. break;
  76. case 88200:
  77. sampling_freq = 0x08;
  78. break;
  79. case 96000:
  80. sampling_freq = 0x0a;
  81. break;
  82. case 176400:
  83. sampling_freq = 0x0c;
  84. break;
  85. case 192000:
  86. sampling_freq = 0x0e;
  87. break;
  88. default:
  89. dev_err(rtd->card->dev,
  90. "unsupported samplerate %d\n", samplerate);
  91. return -EINVAL;
  92. }
  93. if (samplerate % 16000)
  94. clk_44 = true; /* use 44.1 kHz root frequency */
  95. else
  96. clk_44 = false;
  97. if (!(IS_ERR_OR_NULL(ctx->gpio_44) ||
  98. IS_ERR_OR_NULL(ctx->gpio_48))) {
  99. /*
  100. * ensure both GPIOs are LOW first, then drive the
  101. * relevant one to HIGH
  102. */
  103. if (clk_44) {
  104. gpiod_set_value_cansleep(ctx->gpio_48, !clk_44);
  105. gpiod_set_value_cansleep(ctx->gpio_44, clk_44);
  106. } else {
  107. gpiod_set_value_cansleep(ctx->gpio_44, clk_44);
  108. gpiod_set_value_cansleep(ctx->gpio_48, !clk_44);
  109. }
  110. }
  111. snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  112. ret = snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  113. if (ret < 0) {
  114. dev_err(rtd->card->dev, "Failed to set WM8804 PLL\n");
  115. return ret;
  116. }
  117. ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  118. sysclk, SND_SOC_CLOCK_OUT);
  119. if (ret < 0) {
  120. dev_err(rtd->card->dev,
  121. "Failed to set WM8804 SYSCLK: %d\n", ret);
  122. return ret;
  123. }
  124. /* set sampling frequency status bits */
  125. snd_soc_component_update_bits(codec, WM8804_SPDTX4, 0x0f,
  126. sampling_freq);
  127. ctx->sample_rate = samplerate;
  128. return 0;
  129. }
  130. /* machine stream operations */
  131. static struct snd_soc_ops sof_wm8804_ops = {
  132. .hw_params = sof_wm8804_hw_params,
  133. };
  134. SND_SOC_DAILINK_DEF(ssp5_pin,
  135. DAILINK_COMP_ARRAY(COMP_CPU("SSP5 Pin")));
  136. SND_SOC_DAILINK_DEF(ssp5_codec,
  137. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-1AEC8804:00", "wm8804-spdif")));
  138. SND_SOC_DAILINK_DEF(platform,
  139. DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:0e.0")));
  140. static struct snd_soc_dai_link dailink[] = {
  141. /* back ends */
  142. {
  143. .name = "SSP5-Codec",
  144. .id = 0,
  145. .no_pcm = 1,
  146. .dpcm_playback = 1,
  147. .dpcm_capture = 1,
  148. .ops = &sof_wm8804_ops,
  149. SND_SOC_DAILINK_REG(ssp5_pin, ssp5_codec, platform),
  150. },
  151. };
  152. /* SoC card */
  153. static struct snd_soc_card sof_wm8804_card = {
  154. .name = "wm8804", /* sof- prefix added automatically */
  155. .owner = THIS_MODULE,
  156. .dai_link = dailink,
  157. .num_links = ARRAY_SIZE(dailink),
  158. };
  159. /* i2c-<HID>:00 with HID being 8 chars */
  160. static char codec_name[SND_ACPI_I2C_ID_LEN];
  161. /*
  162. * to control the HifiBerry Digi+ PRO, it's required to toggle GPIO to
  163. * select the clock source. On the Up2 board, this means
  164. * Pin29/BCM5/Linux GPIO 430 and Pin 31/BCM6/ Linux GPIO 404.
  165. *
  166. * Using the ACPI device name is not very nice, but since we only use
  167. * the value for the Up2 board there is no risk of conflict with other
  168. * platforms.
  169. */
  170. static struct gpiod_lookup_table up2_gpios_table = {
  171. /* .dev_id is set during probe */
  172. .table = {
  173. GPIO_LOOKUP("INT3452:01", 73, "BCM-GPIO5", GPIO_ACTIVE_HIGH),
  174. GPIO_LOOKUP("INT3452:01", 74, "BCM-GPIO6", GPIO_ACTIVE_HIGH),
  175. { },
  176. },
  177. };
  178. static int sof_wm8804_probe(struct platform_device *pdev)
  179. {
  180. struct snd_soc_card *card;
  181. struct snd_soc_acpi_mach *mach;
  182. struct sof_card_private *ctx;
  183. struct acpi_device *adev;
  184. int dai_index = 0;
  185. int ret;
  186. int i;
  187. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  188. if (!ctx)
  189. return -ENOMEM;
  190. mach = pdev->dev.platform_data;
  191. card = &sof_wm8804_card;
  192. card->dev = &pdev->dev;
  193. dmi_check_system(sof_wm8804_quirk_table);
  194. if (sof_wm8804_quirk & SOF_WM8804_UP2_QUIRK) {
  195. up2_gpios_table.dev_id = dev_name(&pdev->dev);
  196. gpiod_add_lookup_table(&up2_gpios_table);
  197. /*
  198. * The gpios are required for specific boards with
  199. * local oscillators, and optional in other cases.
  200. * Since we can't identify when they are needed, use
  201. * the GPIO as non-optional
  202. */
  203. ctx->gpio_44 = devm_gpiod_get(&pdev->dev, "BCM-GPIO5",
  204. GPIOD_OUT_LOW);
  205. if (IS_ERR(ctx->gpio_44)) {
  206. ret = PTR_ERR(ctx->gpio_44);
  207. dev_err(&pdev->dev,
  208. "could not get BCM-GPIO5: %d\n",
  209. ret);
  210. return ret;
  211. }
  212. ctx->gpio_48 = devm_gpiod_get(&pdev->dev, "BCM-GPIO6",
  213. GPIOD_OUT_LOW);
  214. if (IS_ERR(ctx->gpio_48)) {
  215. ret = PTR_ERR(ctx->gpio_48);
  216. dev_err(&pdev->dev,
  217. "could not get BCM-GPIO6: %d\n",
  218. ret);
  219. return ret;
  220. }
  221. }
  222. /* fix index of codec dai */
  223. for (i = 0; i < ARRAY_SIZE(dailink); i++) {
  224. if (!strcmp(dailink[i].codecs->name, "i2c-1AEC8804:00")) {
  225. dai_index = i;
  226. break;
  227. }
  228. }
  229. /* fixup codec name based on HID */
  230. adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
  231. if (adev) {
  232. snprintf(codec_name, sizeof(codec_name),
  233. "%s%s", "i2c-", acpi_dev_name(adev));
  234. put_device(&adev->dev);
  235. dailink[dai_index].codecs->name = codec_name;
  236. }
  237. snd_soc_card_set_drvdata(card, ctx);
  238. return devm_snd_soc_register_card(&pdev->dev, card);
  239. }
  240. static int sof_wm8804_remove(struct platform_device *pdev)
  241. {
  242. if (sof_wm8804_quirk & SOF_WM8804_UP2_QUIRK)
  243. gpiod_remove_lookup_table(&up2_gpios_table);
  244. return 0;
  245. }
  246. static struct platform_driver sof_wm8804_driver = {
  247. .driver = {
  248. .name = "sof-wm8804",
  249. .pm = &snd_soc_pm_ops,
  250. },
  251. .probe = sof_wm8804_probe,
  252. .remove = sof_wm8804_remove,
  253. };
  254. module_platform_driver(sof_wm8804_driver);
  255. MODULE_DESCRIPTION("ASoC Intel(R) SOF + WM8804 Machine driver");
  256. MODULE_AUTHOR("Pierre-Louis Bossart");
  257. MODULE_LICENSE("GPL v2");
  258. MODULE_ALIAS("platform:sof-wm8804");