0411-IQaudIO-auto-mute-for-AMP-and-DigiAMP.patch 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. From 727a41c904af3f1660de44dc3309b054a1234246 Mon Sep 17 00:00:00 2001
  2. From: DigitalDreamtime <clive.messer@digitaldreamtime.co.uk>
  3. Date: Mon, 9 May 2016 20:38:08 +0100
  4. Subject: [PATCH] IQaudIO: auto-mute for AMP+ and DigiAMP+
  5. IQAudIO amplifier mute via GPIO22. Add dt params for "one-shot" unmute
  6. and auto mute.
  7. Revision 2, auto mute implementing HiassofT suggestion to mute/unmute
  8. using set_bias_level, rather than startup/shutdown....
  9. "By default DAPM waits 5 seconds (pmdown_time) before shutting down
  10. playback streams so a close/stop immediately followed by open/start
  11. doesn't trigger an amp mute+unmute."
  12. Tested on both AMP+ (via DAC+) and DigiAMP+, with both options...
  13. dtoverlay=iqaudio-dacplus,unmute_amp
  14. "one-shot" unmute when kernel module loads.
  15. dtoverlay=iqaudio-dacplus,auto_mute_amp
  16. Unmute amp when ALSA device opened by a client. Mute, with 5 second delay
  17. when ALSA device closed. (Re-opening the device within the 5 second close
  18. window, will cancel mute.)
  19. Revision 4, using gpiod.
  20. Revision 5, clean-up formatting before adding mute code.
  21. - Convert tab plus 4 space formatting to 2x tab
  22. - Remove '// NOT USED' commented code
  23. Revision 6, don't attempt to "one-shot" unmute amp, unless card is
  24. successfully registered.
  25. Signed-off-by: DigitalDreamtime <clive.messer@digitaldreamtime.co.uk>
  26. ---
  27. arch/arm/boot/dts/overlays/README | 4 +
  28. .../boot/dts/overlays/iqaudio-dacplus-overlay.dts | 7 +-
  29. sound/soc/bcm/iqaudio-dac.c | 144 ++++++++++++++++-----
  30. 3 files changed, 124 insertions(+), 31 deletions(-)
  31. --- a/arch/arm/boot/dts/overlays/README
  32. +++ b/arch/arm/boot/dts/overlays/README
  33. @@ -540,6 +540,10 @@ Params: 24db_digital_gain Allow ga
  34. responsibility of the user to ensure that
  35. the Digital volume control is set to a value
  36. that does not result in clipping/distortion!)
  37. + auto_mute_amp If specified, unmute/mute the IQaudIO amp when
  38. + starting/stopping audio playback.
  39. + unmute_amp If specified, unmute the IQaudIO amp once when
  40. + the DAC driver module loads.
  41. Name: justboom-dac
  42. --- a/arch/arm/boot/dts/overlays/iqaudio-dacplus-overlay.dts
  43. +++ b/arch/arm/boot/dts/overlays/iqaudio-dacplus-overlay.dts
  44. @@ -30,14 +30,17 @@
  45. fragment@2 {
  46. target = <&sound>;
  47. - frag2: __overlay__ {
  48. + iqaudio_dac: __overlay__ {
  49. compatible = "iqaudio,iqaudio-dac";
  50. i2s-controller = <&i2s>;
  51. + mute-gpios = <&gpio 22 0>;
  52. status = "okay";
  53. };
  54. };
  55. __overrides__ {
  56. - 24db_digital_gain = <&frag2>,"iqaudio,24db_digital_gain?";
  57. + 24db_digital_gain = <&iqaudio_dac>,"iqaudio,24db_digital_gain?";
  58. + auto_mute_amp = <&iqaudio_dac>,"iqaudio-dac,auto-mute-amp?";
  59. + unmute_amp = <&iqaudio_dac>,"iqaudio-dac,unmute-amp?";
  60. };
  61. };
  62. --- a/sound/soc/bcm/iqaudio-dac.c
  63. +++ b/sound/soc/bcm/iqaudio-dac.c
  64. @@ -15,6 +15,7 @@
  65. */
  66. #include <linux/module.h>
  67. +#include <linux/gpio/consumer.h>
  68. #include <linux/platform_device.h>
  69. #include <sound/core.h>
  70. @@ -25,6 +26,8 @@
  71. static bool digital_gain_0db_limit = true;
  72. +static struct gpio_desc *mute_gpio;
  73. +
  74. static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
  75. {
  76. if (digital_gain_0db_limit)
  77. @@ -41,11 +44,9 @@ static int snd_rpi_iqaudio_dac_init(stru
  78. }
  79. static int snd_rpi_iqaudio_dac_hw_params(struct snd_pcm_substream *substream,
  80. - struct snd_pcm_hw_params *params)
  81. + struct snd_pcm_hw_params *params)
  82. {
  83. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  84. -// NOT USED struct snd_soc_dai *codec_dai = rtd->codec_dai;
  85. -// NOT USED struct snd_soc_codec *codec = rtd->codec;
  86. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  87. unsigned int sample_bits =
  88. @@ -54,6 +55,56 @@ static int snd_rpi_iqaudio_dac_hw_params
  89. return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  90. }
  91. +static void snd_rpi_iqaudio_gpio_mute(struct snd_soc_card *card)
  92. +{
  93. + if (mute_gpio) {
  94. + dev_info(card->dev, "%s: muting amp using GPIO22\n",
  95. + __func__);
  96. + gpiod_set_value_cansleep(mute_gpio, 0);
  97. + }
  98. +}
  99. +
  100. +static void snd_rpi_iqaudio_gpio_unmute(struct snd_soc_card *card)
  101. +{
  102. + if (mute_gpio) {
  103. + dev_info(card->dev, "%s: un-muting amp using GPIO22\n",
  104. + __func__);
  105. + gpiod_set_value_cansleep(mute_gpio, 1);
  106. + }
  107. +}
  108. +
  109. +static int snd_rpi_iqaudio_set_bias_level(struct snd_soc_card *card,
  110. + struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
  111. +{
  112. + struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  113. +
  114. + if (dapm->dev != codec_dai->dev)
  115. + return 0;
  116. +
  117. + switch (level) {
  118. + case SND_SOC_BIAS_PREPARE:
  119. + if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
  120. + break;
  121. +
  122. + /* UNMUTE AMP */
  123. + snd_rpi_iqaudio_gpio_unmute(card);
  124. +
  125. + break;
  126. + case SND_SOC_BIAS_STANDBY:
  127. + if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
  128. + break;
  129. +
  130. + /* MUTE AMP */
  131. + snd_rpi_iqaudio_gpio_mute(card);
  132. +
  133. + break;
  134. + default:
  135. + break;
  136. + }
  137. +
  138. + return 0;
  139. +}
  140. +
  141. /* machine stream operations */
  142. static struct snd_soc_ops snd_rpi_iqaudio_dac_ops = {
  143. .hw_params = snd_rpi_iqaudio_dac_hw_params,
  144. @@ -82,46 +133,81 @@ static struct snd_soc_card snd_rpi_iqaud
  145. static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
  146. {
  147. int ret = 0;
  148. + bool gpio_unmute = false;
  149. snd_rpi_iqaudio_dac.dev = &pdev->dev;
  150. if (pdev->dev.of_node) {
  151. - struct device_node *i2s_node;
  152. - struct snd_soc_card *card = &snd_rpi_iqaudio_dac;
  153. - struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_dac_dai[0];
  154. - i2s_node = of_parse_phandle(pdev->dev.of_node,
  155. - "i2s-controller", 0);
  156. -
  157. - if (i2s_node) {
  158. - dai->cpu_dai_name = NULL;
  159. - dai->cpu_of_node = i2s_node;
  160. - dai->platform_name = NULL;
  161. - dai->platform_of_node = i2s_node;
  162. - }
  163. -
  164. - digital_gain_0db_limit = !of_property_read_bool(pdev->dev.of_node,
  165. - "iqaudio,24db_digital_gain");
  166. - if (of_property_read_string(pdev->dev.of_node, "card_name",
  167. - &card->name))
  168. - card->name = "IQaudIODAC";
  169. - if (of_property_read_string(pdev->dev.of_node, "dai_name",
  170. - &dai->name))
  171. - dai->name = "IQaudIO DAC";
  172. - if (of_property_read_string(pdev->dev.of_node, "dai_stream_name",
  173. - &dai->stream_name))
  174. - dai->stream_name = "IQaudIO DAC HiFi";
  175. + struct device_node *i2s_node;
  176. + struct snd_soc_card *card = &snd_rpi_iqaudio_dac;
  177. + struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_dac_dai[0];
  178. + bool auto_gpio_mute = false;
  179. +
  180. + i2s_node = of_parse_phandle(pdev->dev.of_node,
  181. + "i2s-controller", 0);
  182. + if (i2s_node) {
  183. + dai->cpu_dai_name = NULL;
  184. + dai->cpu_of_node = i2s_node;
  185. + dai->platform_name = NULL;
  186. + dai->platform_of_node = i2s_node;
  187. + }
  188. +
  189. + digital_gain_0db_limit = !of_property_read_bool(
  190. + pdev->dev.of_node, "iqaudio,24db_digital_gain");
  191. +
  192. + if (of_property_read_string(pdev->dev.of_node, "card_name",
  193. + &card->name))
  194. + card->name = "IQaudIODAC";
  195. +
  196. + if (of_property_read_string(pdev->dev.of_node, "dai_name",
  197. + &dai->name))
  198. + dai->name = "IQaudIO DAC";
  199. +
  200. + if (of_property_read_string(pdev->dev.of_node,
  201. + "dai_stream_name", &dai->stream_name))
  202. + dai->stream_name = "IQaudIO DAC HiFi";
  203. +
  204. + /* gpio_unmute - one time unmute amp using GPIO */
  205. + gpio_unmute = of_property_read_bool(pdev->dev.of_node,
  206. + "iqaudio-dac,unmute-amp");
  207. +
  208. + /* auto_gpio_mute - mute/unmute amp using GPIO */
  209. + auto_gpio_mute = of_property_read_bool(pdev->dev.of_node,
  210. + "iqaudio-dac,auto-mute-amp");
  211. +
  212. + if (auto_gpio_mute || gpio_unmute) {
  213. + mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute",
  214. + GPIOD_OUT_LOW);
  215. + if (IS_ERR(mute_gpio)) {
  216. + ret = PTR_ERR(mute_gpio);
  217. + dev_err(&pdev->dev,
  218. + "Failed to get mute gpio: %d\n", ret);
  219. + return ret;
  220. + }
  221. +
  222. + if (auto_gpio_mute && mute_gpio)
  223. + snd_rpi_iqaudio_dac.set_bias_level =
  224. + snd_rpi_iqaudio_set_bias_level;
  225. + }
  226. }
  227. ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
  228. - if (ret)
  229. + if (ret) {
  230. dev_err(&pdev->dev,
  231. "snd_soc_register_card() failed: %d\n", ret);
  232. + return ret;
  233. + }
  234. +
  235. + if (gpio_unmute && mute_gpio)
  236. + snd_rpi_iqaudio_gpio_unmute(&snd_rpi_iqaudio_dac);
  237. - return ret;
  238. + return 0;
  239. }
  240. static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
  241. {
  242. + snd_rpi_iqaudio_gpio_mute(&snd_rpi_iqaudio_dac);
  243. +
  244. return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
  245. }