950-0070-Add-IQaudIO-Sound-Card-support-for-Raspberry-Pi.patch 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. From fcfe9087b79176b5d8afd0ecc4c66013866ef239 Mon Sep 17 00:00:00 2001
  2. From: Gordon Garrity <[email protected]>
  3. Date: Sat, 8 Mar 2014 16:56:57 +0000
  4. Subject: [PATCH] Add IQaudIO Sound Card support for Raspberry Pi
  5. Set a limit of 0dB on Digital Volume Control
  6. The main volume control in the PCM512x DAC has a range up to
  7. +24dB. This is dangerously loud and can potentially cause massive
  8. clipping in the output stages. Therefore this sets a sensible
  9. limit of 0dB for this control.
  10. Allow up to 24dB digital gain to be applied when using IQAudIO DAC+
  11. 24db_digital_gain DT param can be used to specify that PCM512x
  12. codec "Digital" volume control should not be limited to 0dB gain,
  13. and if specified will allow the full 24dB gain.
  14. Modify IQAudIO DAC+ ASoC driver to set card/dai config from dt
  15. Add the ability to set the card name, dai name and dai stream name, from
  16. dt config.
  17. Signed-off-by: DigitalDreamtime <[email protected]>
  18. IQaudIO: auto-mute for AMP+ and DigiAMP+
  19. IQAudIO amplifier mute via GPIO22. Add dt params for "one-shot" unmute
  20. and auto mute.
  21. Revision 2, auto mute implementing HiassofT suggestion to mute/unmute
  22. using set_bias_level, rather than startup/shutdown....
  23. "By default DAPM waits 5 seconds (pmdown_time) before shutting down
  24. playback streams so a close/stop immediately followed by open/start
  25. doesn't trigger an amp mute+unmute."
  26. Tested on both AMP+ (via DAC+) and DigiAMP+, with both options...
  27. dtoverlay=iqaudio-dacplus,unmute_amp
  28. "one-shot" unmute when kernel module loads.
  29. dtoverlay=iqaudio-dacplus,auto_mute_amp
  30. Unmute amp when ALSA device opened by a client. Mute, with 5 second delay
  31. when ALSA device closed. (Re-opening the device within the 5 second close
  32. window, will cancel mute.)
  33. Revision 4, using gpiod.
  34. Revision 5, clean-up formatting before adding mute code.
  35. - Convert tab plus 4 space formatting to 2x tab
  36. - Remove '// NOT USED' commented code
  37. Revision 6, don't attempt to "one-shot" unmute amp, unless card is
  38. successfully registered.
  39. Signed-off-by: DigitalDreamtime <[email protected]>
  40. ---
  41. sound/soc/bcm/Kconfig | 7 ++
  42. sound/soc/bcm/Makefile | 2 +
  43. sound/soc/bcm/iqaudio-dac.c | 234 ++++++++++++++++++++++++++++++++++++++++++++
  44. 3 files changed, 243 insertions(+)
  45. create mode 100644 sound/soc/bcm/iqaudio-dac.c
  46. --- a/sound/soc/bcm/Kconfig
  47. +++ b/sound/soc/bcm/Kconfig
  48. @@ -37,3 +37,10 @@ config SND_BCM2708_SOC_RPI_DAC
  49. select SND_SOC_PCM1794A
  50. help
  51. Say Y or M if you want to add support for RPi-DAC.
  52. +
  53. +config SND_BCM2708_SOC_IQAUDIO_DAC
  54. + tristate "Support for IQaudIO-DAC"
  55. + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
  56. + select SND_SOC_PCM512x_I2C
  57. + help
  58. + Say Y or M if you want to add support for IQaudIO-DAC.
  59. --- a/sound/soc/bcm/Makefile
  60. +++ b/sound/soc/bcm/Makefile
  61. @@ -12,7 +12,9 @@ obj-$(CONFIG_SND_SOC_CYGNUS) += snd-soc-
  62. snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  63. snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  64. snd-soc-rpi-dac-objs := rpi-dac.o
  65. +snd-soc-iqaudio-dac-objs := iqaudio-dac.o
  66. obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  67. obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  68. obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  69. +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
  70. --- /dev/null
  71. +++ b/sound/soc/bcm/iqaudio-dac.c
  72. @@ -0,0 +1,234 @@
  73. +/*
  74. + * ASoC Driver for IQaudIO DAC
  75. + *
  76. + * Author: Florian Meier <[email protected]>
  77. + * Copyright 2013
  78. + *
  79. + * This program is free software; you can redistribute it and/or
  80. + * modify it under the terms of the GNU General Public License
  81. + * version 2 as published by the Free Software Foundation.
  82. + *
  83. + * This program is distributed in the hope that it will be useful, but
  84. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  85. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  86. + * General Public License for more details.
  87. + */
  88. +
  89. +#include <linux/module.h>
  90. +#include <linux/gpio/consumer.h>
  91. +#include <linux/platform_device.h>
  92. +
  93. +#include <sound/core.h>
  94. +#include <sound/pcm.h>
  95. +#include <sound/pcm_params.h>
  96. +#include <sound/soc.h>
  97. +#include <sound/jack.h>
  98. +
  99. +static bool digital_gain_0db_limit = true;
  100. +
  101. +static struct gpio_desc *mute_gpio;
  102. +
  103. +static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
  104. +{
  105. + if (digital_gain_0db_limit)
  106. + {
  107. + int ret;
  108. + struct snd_soc_card *card = rtd->card;
  109. +
  110. + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
  111. + if (ret < 0)
  112. + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
  113. + }
  114. +
  115. + return 0;
  116. +}
  117. +
  118. +static int snd_rpi_iqaudio_dac_hw_params(struct snd_pcm_substream *substream,
  119. + struct snd_pcm_hw_params *params)
  120. +{
  121. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  122. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  123. +
  124. + unsigned int sample_bits =
  125. + snd_pcm_format_physical_width(params_format(params));
  126. +
  127. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  128. +}
  129. +
  130. +static void snd_rpi_iqaudio_gpio_mute(struct snd_soc_card *card)
  131. +{
  132. + if (mute_gpio) {
  133. + dev_info(card->dev, "%s: muting amp using GPIO22\n",
  134. + __func__);
  135. + gpiod_set_value_cansleep(mute_gpio, 0);
  136. + }
  137. +}
  138. +
  139. +static void snd_rpi_iqaudio_gpio_unmute(struct snd_soc_card *card)
  140. +{
  141. + if (mute_gpio) {
  142. + dev_info(card->dev, "%s: un-muting amp using GPIO22\n",
  143. + __func__);
  144. + gpiod_set_value_cansleep(mute_gpio, 1);
  145. + }
  146. +}
  147. +
  148. +static int snd_rpi_iqaudio_set_bias_level(struct snd_soc_card *card,
  149. + struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
  150. +{
  151. + struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  152. +
  153. + if (dapm->dev != codec_dai->dev)
  154. + return 0;
  155. +
  156. + switch (level) {
  157. + case SND_SOC_BIAS_PREPARE:
  158. + if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
  159. + break;
  160. +
  161. + /* UNMUTE AMP */
  162. + snd_rpi_iqaudio_gpio_unmute(card);
  163. +
  164. + break;
  165. + case SND_SOC_BIAS_STANDBY:
  166. + if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
  167. + break;
  168. +
  169. + /* MUTE AMP */
  170. + snd_rpi_iqaudio_gpio_mute(card);
  171. +
  172. + break;
  173. + default:
  174. + break;
  175. + }
  176. +
  177. + return 0;
  178. +}
  179. +
  180. +/* machine stream operations */
  181. +static struct snd_soc_ops snd_rpi_iqaudio_dac_ops = {
  182. + .hw_params = snd_rpi_iqaudio_dac_hw_params,
  183. +};
  184. +
  185. +static struct snd_soc_dai_link snd_rpi_iqaudio_dac_dai[] = {
  186. +{
  187. + .cpu_dai_name = "bcm2708-i2s.0",
  188. + .codec_dai_name = "pcm512x-hifi",
  189. + .platform_name = "bcm2708-i2s.0",
  190. + .codec_name = "pcm512x.1-004c",
  191. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  192. + SND_SOC_DAIFMT_CBS_CFS,
  193. + .ops = &snd_rpi_iqaudio_dac_ops,
  194. + .init = snd_rpi_iqaudio_dac_init,
  195. +},
  196. +};
  197. +
  198. +/* audio machine driver */
  199. +static struct snd_soc_card snd_rpi_iqaudio_dac = {
  200. + .owner = THIS_MODULE,
  201. + .dai_link = snd_rpi_iqaudio_dac_dai,
  202. + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_dac_dai),
  203. +};
  204. +
  205. +static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
  206. +{
  207. + int ret = 0;
  208. + bool gpio_unmute = false;
  209. +
  210. + snd_rpi_iqaudio_dac.dev = &pdev->dev;
  211. +
  212. + if (pdev->dev.of_node) {
  213. + struct device_node *i2s_node;
  214. + struct snd_soc_card *card = &snd_rpi_iqaudio_dac;
  215. + struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_dac_dai[0];
  216. + bool auto_gpio_mute = false;
  217. +
  218. + i2s_node = of_parse_phandle(pdev->dev.of_node,
  219. + "i2s-controller", 0);
  220. + if (i2s_node) {
  221. + dai->cpu_dai_name = NULL;
  222. + dai->cpu_of_node = i2s_node;
  223. + dai->platform_name = NULL;
  224. + dai->platform_of_node = i2s_node;
  225. + }
  226. +
  227. + digital_gain_0db_limit = !of_property_read_bool(
  228. + pdev->dev.of_node, "iqaudio,24db_digital_gain");
  229. +
  230. + if (of_property_read_string(pdev->dev.of_node, "card_name",
  231. + &card->name))
  232. + card->name = "IQaudIODAC";
  233. +
  234. + if (of_property_read_string(pdev->dev.of_node, "dai_name",
  235. + &dai->name))
  236. + dai->name = "IQaudIO DAC";
  237. +
  238. + if (of_property_read_string(pdev->dev.of_node,
  239. + "dai_stream_name", &dai->stream_name))
  240. + dai->stream_name = "IQaudIO DAC HiFi";
  241. +
  242. + /* gpio_unmute - one time unmute amp using GPIO */
  243. + gpio_unmute = of_property_read_bool(pdev->dev.of_node,
  244. + "iqaudio-dac,unmute-amp");
  245. +
  246. + /* auto_gpio_mute - mute/unmute amp using GPIO */
  247. + auto_gpio_mute = of_property_read_bool(pdev->dev.of_node,
  248. + "iqaudio-dac,auto-mute-amp");
  249. +
  250. + if (auto_gpio_mute || gpio_unmute) {
  251. + mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute",
  252. + GPIOD_OUT_LOW);
  253. + if (IS_ERR(mute_gpio)) {
  254. + ret = PTR_ERR(mute_gpio);
  255. + dev_err(&pdev->dev,
  256. + "Failed to get mute gpio: %d\n", ret);
  257. + return ret;
  258. + }
  259. +
  260. + if (auto_gpio_mute && mute_gpio)
  261. + snd_rpi_iqaudio_dac.set_bias_level =
  262. + snd_rpi_iqaudio_set_bias_level;
  263. + }
  264. + }
  265. +
  266. + ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
  267. + if (ret) {
  268. + dev_err(&pdev->dev,
  269. + "snd_soc_register_card() failed: %d\n", ret);
  270. + return ret;
  271. + }
  272. +
  273. + if (gpio_unmute && mute_gpio)
  274. + snd_rpi_iqaudio_gpio_unmute(&snd_rpi_iqaudio_dac);
  275. +
  276. + return 0;
  277. +}
  278. +
  279. +static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
  280. +{
  281. + snd_rpi_iqaudio_gpio_mute(&snd_rpi_iqaudio_dac);
  282. +
  283. + return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
  284. +}
  285. +
  286. +static const struct of_device_id iqaudio_of_match[] = {
  287. + { .compatible = "iqaudio,iqaudio-dac", },
  288. + {},
  289. +};
  290. +MODULE_DEVICE_TABLE(of, iqaudio_of_match);
  291. +
  292. +static struct platform_driver snd_rpi_iqaudio_dac_driver = {
  293. + .driver = {
  294. + .name = "snd-rpi-iqaudio-dac",
  295. + .owner = THIS_MODULE,
  296. + .of_match_table = iqaudio_of_match,
  297. + },
  298. + .probe = snd_rpi_iqaudio_dac_probe,
  299. + .remove = snd_rpi_iqaudio_dac_remove,
  300. +};
  301. +
  302. +module_platform_driver(snd_rpi_iqaudio_dac_driver);
  303. +
  304. +MODULE_AUTHOR("Florian Meier <[email protected]>");
  305. +MODULE_DESCRIPTION("ASoC Driver for IQAudio DAC");
  306. +MODULE_LICENSE("GPL v2");