0066-ASoC-BCM-Add-support-for-HiFiBerry-Digi.-Driver-is-b.patch 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. From fb61baf20c890884a9e4f55da0f287f27f81a474 Mon Sep 17 00:00:00 2001
  2. From: Daniel Matuschek <info@crazy-audio.com>
  3. Date: Wed, 15 Jan 2014 21:42:08 +0100
  4. Subject: [PATCH 066/381] ASoC: BCM:Add support for HiFiBerry Digi. Driver is
  5. based on the patched WM8804 driver.
  6. Signed-off-by: Daniel Matuschek <daniel@matuschek.net>
  7. Add a parameter to turn off SPDIF output if no audio is playing
  8. This patch adds the paramater auto_shutdown_output to the kernel module.
  9. Default behaviour of the module is the same, but when auto_shutdown_output
  10. is set to 1, the SPDIF oputput will shutdown if no stream is playing.
  11. bugfix for 32kHz sample rate, was missing
  12. HiFiBerry Digi: set SPDIF status bits for sample rate
  13. The HiFiBerry Digi driver did not signal the sample rate in the SPDIF status bits.
  14. While this is optional, some DACs and receivers do not accept this signal. This patch
  15. adds the sample rate bits in the SPDIF status block.
  16. ---
  17. sound/soc/bcm/Kconfig | 7 ++
  18. sound/soc/bcm/Makefile | 2 +
  19. sound/soc/bcm/hifiberry_digi.c | 223 +++++++++++++++++++++++++++++++++++++++++
  20. 3 files changed, 232 insertions(+)
  21. create mode 100644 sound/soc/bcm/hifiberry_digi.c
  22. --- a/sound/soc/bcm/Kconfig
  23. +++ b/sound/soc/bcm/Kconfig
  24. @@ -15,6 +15,13 @@ config SND_BCM2708_SOC_HIFIBERRY_DAC
  25. help
  26. Say Y or M if you want to add support for HifiBerry DAC.
  27. +config SND_BCM2708_SOC_HIFIBERRY_DIGI
  28. + tristate "Support for HifiBerry Digi"
  29. + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
  30. + select SND_SOC_WM8804
  31. + help
  32. + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
  33. +
  34. config SND_BCM2708_SOC_RPI_DAC
  35. tristate "Support for RPi-DAC"
  36. depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
  37. --- a/sound/soc/bcm/Makefile
  38. +++ b/sound/soc/bcm/Makefile
  39. @@ -5,7 +5,9 @@ obj-$(CONFIG_SND_BCM2835_SOC_I2S) += snd
  40. # BCM2708 Machine Support
  41. snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  42. +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  43. snd-soc-rpi-dac-objs := rpi-dac.o
  44. obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  45. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  46. obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  47. --- /dev/null
  48. +++ b/sound/soc/bcm/hifiberry_digi.c
  49. @@ -0,0 +1,223 @@
  50. +/*
  51. + * ASoC Driver for HifiBerry Digi
  52. + *
  53. + * Author: Daniel Matuschek <info@crazy-audio.com>
  54. + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
  55. + * Copyright 2013
  56. + *
  57. + * This program is free software; you can redistribute it and/or
  58. + * modify it under the terms of the GNU General Public License
  59. + * version 2 as published by the Free Software Foundation.
  60. + *
  61. + * This program is distributed in the hope that it will be useful, but
  62. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  63. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  64. + * General Public License for more details.
  65. + */
  66. +
  67. +#include <linux/module.h>
  68. +#include <linux/platform_device.h>
  69. +
  70. +#include <sound/core.h>
  71. +#include <sound/pcm.h>
  72. +#include <sound/pcm_params.h>
  73. +#include <sound/soc.h>
  74. +#include <sound/jack.h>
  75. +
  76. +#include "../codecs/wm8804.h"
  77. +
  78. +static short int auto_shutdown_output = 0;
  79. +module_param(auto_shutdown_output, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  80. +MODULE_PARM_DESC(auto_shutdown_output, "Shutdown SP/DIF output if playback is stopped");
  81. +
  82. +
  83. +static int samplerate=44100;
  84. +
  85. +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
  86. +{
  87. + struct snd_soc_codec *codec = rtd->codec;
  88. +
  89. + /* enable TX output */
  90. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  91. +
  92. + return 0;
  93. +}
  94. +
  95. +static int snd_rpi_hifiberry_digi_startup(struct snd_pcm_substream *substream) {
  96. + /* turn on digital output */
  97. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  98. + struct snd_soc_codec *codec = rtd->codec;
  99. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x00);
  100. + return 0;
  101. +}
  102. +
  103. +static void snd_rpi_hifiberry_digi_shutdown(struct snd_pcm_substream *substream) {
  104. + /* turn off output */
  105. + if (auto_shutdown_output) {
  106. + /* turn off output */
  107. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108. + struct snd_soc_codec *codec = rtd->codec;
  109. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x3c);
  110. + }
  111. +}
  112. +
  113. +
  114. +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
  115. + struct snd_pcm_hw_params *params)
  116. +{
  117. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  118. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  119. + struct snd_soc_codec *codec = rtd->codec;
  120. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  121. +
  122. + int sysclk = 27000000; /* This is fixed on this board */
  123. +
  124. + long mclk_freq=0;
  125. + int mclk_div=1;
  126. + int sampling_freq=1;
  127. +
  128. + int ret;
  129. +
  130. + samplerate = params_rate(params);
  131. +
  132. + if (samplerate<=96000) {
  133. + mclk_freq=samplerate*256;
  134. + mclk_div=WM8804_MCLKDIV_256FS;
  135. + } else {
  136. + mclk_freq=samplerate*128;
  137. + mclk_div=WM8804_MCLKDIV_128FS;
  138. + }
  139. +
  140. + switch (samplerate) {
  141. + case 32000:
  142. + sampling_freq=0x03;
  143. + break;
  144. + case 44100:
  145. + sampling_freq=0x00;
  146. + break;
  147. + case 48000:
  148. + sampling_freq=0x02;
  149. + break;
  150. + case 88200:
  151. + sampling_freq=0x08;
  152. + break;
  153. + case 96000:
  154. + sampling_freq=0x0a;
  155. + break;
  156. + case 176400:
  157. + sampling_freq=0x0c;
  158. + break;
  159. + case 192000:
  160. + sampling_freq=0x0e;
  161. + break;
  162. + default:
  163. + dev_err(codec->dev,
  164. + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
  165. + samplerate);
  166. + }
  167. +
  168. + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  169. + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  170. +
  171. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  172. + sysclk, SND_SOC_CLOCK_OUT);
  173. + if (ret < 0) {
  174. + dev_err(codec->dev,
  175. + "Failed to set WM8804 SYSCLK: %d\n", ret);
  176. + return ret;
  177. + }
  178. +
  179. + /* Enable TX output */
  180. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  181. +
  182. + /* Power on */
  183. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
  184. +
  185. + /* set sampling frequency status bits */
  186. + snd_soc_update_bits(codec, WM8804_SPDTX4, 0x0f, sampling_freq);
  187. +
  188. + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
  189. +}
  190. +
  191. +/* machine stream operations */
  192. +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
  193. + .hw_params = snd_rpi_hifiberry_digi_hw_params,
  194. + .startup = snd_rpi_hifiberry_digi_startup,
  195. + .shutdown = snd_rpi_hifiberry_digi_shutdown,
  196. +};
  197. +
  198. +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
  199. +{
  200. + .name = "HifiBerry Digi",
  201. + .stream_name = "HifiBerry Digi HiFi",
  202. + .cpu_dai_name = "bcm2708-i2s.0",
  203. + .codec_dai_name = "wm8804-spdif",
  204. + .platform_name = "bcm2708-i2s.0",
  205. + .codec_name = "wm8804.1-003b",
  206. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  207. + SND_SOC_DAIFMT_CBM_CFM,
  208. + .ops = &snd_rpi_hifiberry_digi_ops,
  209. + .init = snd_rpi_hifiberry_digi_init,
  210. +},
  211. +};
  212. +
  213. +/* audio machine driver */
  214. +static struct snd_soc_card snd_rpi_hifiberry_digi = {
  215. + .name = "snd_rpi_hifiberry_digi",
  216. + .dai_link = snd_rpi_hifiberry_digi_dai,
  217. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
  218. +};
  219. +
  220. +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
  221. +{
  222. + int ret = 0;
  223. +
  224. + snd_rpi_hifiberry_digi.dev = &pdev->dev;
  225. +
  226. + if (pdev->dev.of_node) {
  227. + struct device_node *i2s_node;
  228. + struct snd_soc_dai_link *dai = &snd_rpi_hifiberry_digi_dai[0];
  229. + i2s_node = of_parse_phandle(pdev->dev.of_node,
  230. + "i2s-controller", 0);
  231. +
  232. + if (i2s_node) {
  233. + dai->cpu_dai_name = NULL;
  234. + dai->cpu_of_node = i2s_node;
  235. + dai->platform_name = NULL;
  236. + dai->platform_of_node = i2s_node;
  237. + }
  238. + }
  239. +
  240. + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
  241. + if (ret)
  242. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  243. +
  244. + return ret;
  245. +}
  246. +
  247. +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
  248. +{
  249. + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
  250. +}
  251. +
  252. +static const struct of_device_id snd_rpi_hifiberry_digi_of_match[] = {
  253. + { .compatible = "hifiberry,hifiberry-digi", },
  254. + {},
  255. +};
  256. +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_digi_of_match);
  257. +
  258. +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
  259. + .driver = {
  260. + .name = "snd-hifiberry-digi",
  261. + .owner = THIS_MODULE,
  262. + .of_match_table = snd_rpi_hifiberry_digi_of_match,
  263. + },
  264. + .probe = snd_rpi_hifiberry_digi_probe,
  265. + .remove = snd_rpi_hifiberry_digi_remove,
  266. +};
  267. +
  268. +module_platform_driver(snd_rpi_hifiberry_digi_driver);
  269. +
  270. +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
  271. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
  272. +MODULE_LICENSE("GPL v2");