b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | From 1deb712a4899ba8f394908b75274943e12a6d65a Mon Sep 17 00:00:00 2001 |
| 2 | From: b-ak <anur.bhargav@gmail.com> |
| 3 | Date: Thu, 3 Jan 2019 00:01:08 +0530 |
| 4 | Subject: [PATCH] ASoC: Add support for AudioSense-Pi add-on soundcard |
| 5 | |
| 6 | AudioSense-Pi is a RPi HAT based on a TI's TLV320AIC32x4 stereo codec |
| 7 | |
| 8 | This hardware provides multiple audio I/O capabilities to the RPi. |
| 9 | The codec connects to the RPi's SoC through the I2S Bus. |
| 10 | |
| 11 | The following devices can be connected through a 3.5mm jack |
| 12 | 1. Line-In: Plain old audio in from mobile phones, PCs, etc., |
| 13 | 2. Mic-In: Connect a microphone |
| 14 | 3. Line-Out: Connect the output to a speaker |
| 15 | 4. Headphones: Connect a Headphone w or w/o microphones |
| 16 | |
| 17 | Multiple Inputs: |
| 18 | It supports the following combinations |
| 19 | 1. Two stereo Line-Inputs and a microphone |
| 20 | 2. One stereo Line-Input and two microphones |
| 21 | 3. Two stereo Line-Inputs, a microphone and |
| 22 | one mono line-input (with h/w hack) |
| 23 | 4. One stereo Line-Input, two microphones and |
| 24 | one mono line-input (with h/w hack) |
| 25 | |
| 26 | Multiple Outputs: |
| 27 | Audio output can be routed to the headphones or |
| 28 | speakers (with additional hardware) |
| 29 | |
| 30 | Signed-off-by: b-ak <anur.bhargav@gmail.com> |
| 31 | |
| 32 | ASoC: audiosense-pi: use modern dai_link style |
| 33 | |
| 34 | Signed-off-by: Hui Wang <hui.wang@canonical.com> |
| 35 | --- |
| 36 | sound/soc/bcm/Kconfig | 7 + |
| 37 | sound/soc/bcm/Makefile | 2 + |
| 38 | sound/soc/bcm/audiosense-pi.c | 248 ++++++++++++++++++++++++++++++++++ |
| 39 | 3 files changed, 257 insertions(+) |
| 40 | create mode 100644 sound/soc/bcm/audiosense-pi.c |
| 41 | |
| 42 | --- a/sound/soc/bcm/Kconfig |
| 43 | +++ b/sound/soc/bcm/Kconfig |
| 44 | @@ -132,6 +132,13 @@ config SND_AUDIOINJECTOR_OCTO_SOUNDCARD |
| 45 | help |
| 46 | Say Y or M if you want to add support for audioinjector.net octo add on |
| 47 | |
| 48 | +config SND_AUDIOSENSE_PI |
| 49 | + tristate "Support for AudioSense Add-On Soundcard" |
| 50 | + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S |
| 51 | + select SND_SOC_TLV320AIC32X4_I2C |
| 52 | + help |
| 53 | + Say Y or M if you want to add support for tlv320aic32x4 add-on |
| 54 | + |
| 55 | config SND_DIGIDAC1_SOUNDCARD |
| 56 | tristate "Support for Red Rocks Audio DigiDAC1" |
| 57 | depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S |
| 58 | --- a/sound/soc/bcm/Makefile |
| 59 | +++ b/sound/soc/bcm/Makefile |
| 60 | @@ -20,6 +20,7 @@ snd-soc-rpi-proto-objs := rpi-proto.o |
| 61 | snd-soc-iqaudio-dac-objs := iqaudio-dac.o |
| 62 | snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o |
| 63 | snd-soc-audioinjector-octo-soundcard-objs := audioinjector-octo-soundcard.o |
| 64 | +snd-soc-audiosense-pi-objs := audiosense-pi.o |
| 65 | snd-soc-digidac1-soundcard-objs := digidac1-soundcard.o |
| 66 | snd-soc-dionaudio-loco-objs := dionaudio_loco.o |
| 67 | snd-soc-dionaudio-loco-v2-objs := dionaudio_loco-v2.o |
| 68 | @@ -40,6 +41,7 @@ obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) |
| 69 | obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o |
| 70 | obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o |
| 71 | obj-$(CONFIG_SND_AUDIOINJECTOR_OCTO_SOUNDCARD) += snd-soc-audioinjector-octo-soundcard.o |
| 72 | +obj-$(CONFIG_SND_AUDIOSENSE_PI) += snd-soc-audiosense-pi.o |
| 73 | obj-$(CONFIG_SND_DIGIDAC1_SOUNDCARD) += snd-soc-digidac1-soundcard.o |
| 74 | obj-$(CONFIG_SND_BCM2708_SOC_DIONAUDIO_LOCO) += snd-soc-dionaudio-loco.o |
| 75 | obj-$(CONFIG_SND_BCM2708_SOC_DIONAUDIO_LOCO_V2) += snd-soc-dionaudio-loco-v2.o |
| 76 | --- /dev/null |
| 77 | +++ b/sound/soc/bcm/audiosense-pi.c |
| 78 | @@ -0,0 +1,248 @@ |
| 79 | +/* |
| 80 | + * ASoC Driver for AudioSense add on soundcard |
| 81 | + * Author: |
| 82 | + * Bhargav A K <anur.bhargav@gmail.com> |
| 83 | + * Copyright 2017 |
| 84 | + * |
| 85 | + * This program is free software; you can redistribute it and/or |
| 86 | + * modify it under the terms of the GNU General Public License |
| 87 | + * version 2 as published by the Free Software Foundation. |
| 88 | + * |
| 89 | + * This program is distributed in the hope that it will be useful, but |
| 90 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 91 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 92 | + * General Public License for more details. |
| 93 | + */ |
| 94 | + |
| 95 | +#include <linux/module.h> |
| 96 | +#include <linux/platform_device.h> |
| 97 | +#include <linux/clk.h> |
| 98 | +#include <linux/i2c.h> |
| 99 | +#include <sound/core.h> |
| 100 | +#include <sound/pcm.h> |
| 101 | +#include <sound/pcm_params.h> |
| 102 | +#include <sound/soc.h> |
| 103 | +#include <sound/jack.h> |
| 104 | +#include <sound/control.h> |
| 105 | + |
| 106 | +#include <sound/tlv320aic32x4.h> |
| 107 | +#include "../codecs/tlv320aic32x4.h" |
| 108 | + |
| 109 | +#define AIC32X4_SYSCLK_XTAL 0x00 |
| 110 | + |
| 111 | +/* |
| 112 | + * Setup Codec Sample Rates and Channels |
| 113 | + * Supported Rates: |
| 114 | + * 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200, 96000, |
| 115 | + */ |
| 116 | +static const unsigned int audiosense_pi_rate[] = { |
| 117 | + 48000, |
| 118 | +}; |
| 119 | + |
| 120 | +static struct snd_pcm_hw_constraint_list audiosense_constraints_rates = { |
| 121 | + .list = audiosense_pi_rate, |
| 122 | + .count = ARRAY_SIZE(audiosense_pi_rate), |
| 123 | +}; |
| 124 | + |
| 125 | +static const unsigned int audiosense_pi_channels[] = { |
| 126 | + 2, |
| 127 | +}; |
| 128 | + |
| 129 | +static struct snd_pcm_hw_constraint_list audiosense_constraints_ch = { |
| 130 | + .count = ARRAY_SIZE(audiosense_pi_channels), |
| 131 | + .list = audiosense_pi_channels, |
| 132 | + .mask = 0, |
| 133 | +}; |
| 134 | + |
| 135 | +/* Setup DAPM widgets and paths */ |
| 136 | +static const struct snd_soc_dapm_widget audiosense_pi_dapm_widgets[] = { |
| 137 | + SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 138 | + SND_SOC_DAPM_LINE("Line Out", NULL), |
| 139 | + SND_SOC_DAPM_LINE("Line In", NULL), |
| 140 | + SND_SOC_DAPM_INPUT("CM_L"), |
| 141 | + SND_SOC_DAPM_INPUT("CM_R"), |
| 142 | +}; |
| 143 | + |
| 144 | +static const struct snd_soc_dapm_route audiosense_pi_audio_map[] = { |
| 145 | + /* Line Inputs are connected to |
| 146 | + * (IN1_L | IN1_R) |
| 147 | + * (IN2_L | IN2_R) |
| 148 | + * (IN3_L | IN3_R) |
| 149 | + */ |
| 150 | + {"IN1_L", NULL, "Line In"}, |
| 151 | + {"IN1_R", NULL, "Line In"}, |
| 152 | + {"IN2_L", NULL, "Line In"}, |
| 153 | + {"IN2_R", NULL, "Line In"}, |
| 154 | + {"IN3_L", NULL, "Line In"}, |
| 155 | + {"IN3_R", NULL, "Line In"}, |
| 156 | + |
| 157 | + /* Mic is connected to IN2_L and IN2_R */ |
| 158 | + {"Left ADC", NULL, "Mic Bias"}, |
| 159 | + {"Right ADC", NULL, "Mic Bias"}, |
| 160 | + |
| 161 | + /* Headphone connected to HPL, HPR */ |
| 162 | + {"Headphone Jack", NULL, "HPL"}, |
| 163 | + {"Headphone Jack", NULL, "HPR"}, |
| 164 | + |
| 165 | + /* Speakers connected to LOL and LOR */ |
| 166 | + {"Line Out", NULL, "LOL"}, |
| 167 | + {"Line Out", NULL, "LOR"}, |
| 168 | +}; |
| 169 | + |
| 170 | +static int audiosense_pi_card_init(struct snd_soc_pcm_runtime *rtd) |
| 171 | +{ |
| 172 | + /* TODO: init of the codec specific dapm data, ignore suspend/resume */ |
| 173 | + struct snd_soc_component *component = rtd->codec_dai->component; |
| 174 | + |
| 175 | + snd_soc_component_update_bits(component, AIC32X4_MICBIAS, 0x78, |
| 176 | + AIC32X4_MICBIAS_LDOIN | |
| 177 | + AIC32X4_MICBIAS_2075V); |
| 178 | + snd_soc_component_update_bits(component, AIC32X4_PWRCFG, 0x08, |
| 179 | + AIC32X4_AVDDWEAKDISABLE); |
| 180 | + snd_soc_component_update_bits(component, AIC32X4_LDOCTL, 0x01, |
| 181 | + AIC32X4_LDOCTLEN); |
| 182 | + |
| 183 | + return 0; |
| 184 | +} |
| 185 | + |
| 186 | +static int audiosense_pi_card_hw_params( |
| 187 | + struct snd_pcm_substream *substream, |
| 188 | + struct snd_pcm_hw_params *params) |
| 189 | +{ |
| 190 | + int ret = 0; |
| 191 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 192 | + struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 193 | + |
| 194 | + /* Set the codec system clock, there is a 12 MHz XTAL on the board */ |
| 195 | + ret = snd_soc_dai_set_sysclk(codec_dai, AIC32X4_SYSCLK_XTAL, |
| 196 | + 12000000, SND_SOC_CLOCK_IN); |
| 197 | + if (ret) { |
| 198 | + dev_err(rtd->card->dev, |
| 199 | + "could not set codec driver clock params\n"); |
| 200 | + return ret; |
| 201 | + } |
| 202 | + return 0; |
| 203 | +} |
| 204 | + |
| 205 | +static int audiosense_pi_card_startup(struct snd_pcm_substream *substream) |
| 206 | +{ |
| 207 | + struct snd_pcm_runtime *runtime = substream->runtime; |
| 208 | + |
| 209 | + /* |
| 210 | + * Set codec to 48Khz Sampling, Stereo I/O and 16 bit audio |
| 211 | + */ |
| 212 | + runtime->hw.channels_max = 2; |
| 213 | + snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 214 | + &audiosense_constraints_ch); |
| 215 | + |
| 216 | + runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; |
| 217 | + snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16); |
| 218 | + |
| 219 | + |
| 220 | + snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 221 | + SNDRV_PCM_HW_PARAM_RATE, |
| 222 | + &audiosense_constraints_rates); |
| 223 | + return 0; |
| 224 | +} |
| 225 | + |
| 226 | +static struct snd_soc_ops audiosense_pi_card_ops = { |
| 227 | + .startup = audiosense_pi_card_startup, |
| 228 | + .hw_params = audiosense_pi_card_hw_params, |
| 229 | +}; |
| 230 | + |
| 231 | +SND_SOC_DAILINK_DEFS(audiosense_pi, |
| 232 | + DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")), |
| 233 | + DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic32x4.1-0018", "tlv320aic32x4-hifi")), |
| 234 | + DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0"))); |
| 235 | + |
| 236 | +static struct snd_soc_dai_link audiosense_pi_card_dai[] = { |
| 237 | + { |
| 238 | + .name = "TLV320AIC3204 Audio", |
| 239 | + .stream_name = "TLV320AIC3204 Hifi Audio", |
| 240 | + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 241 | + SND_SOC_DAIFMT_CBM_CFM, |
| 242 | + .ops = &audiosense_pi_card_ops, |
| 243 | + .init = audiosense_pi_card_init, |
| 244 | + SND_SOC_DAILINK_REG(audiosense_pi), |
| 245 | + }, |
| 246 | +}; |
| 247 | + |
| 248 | +static struct snd_soc_card audiosense_pi_card = { |
| 249 | + .name = "audiosense-pi", |
| 250 | + .driver_name = "audiosense-pi", |
| 251 | + .dai_link = audiosense_pi_card_dai, |
| 252 | + .owner = THIS_MODULE, |
| 253 | + .num_links = ARRAY_SIZE(audiosense_pi_card_dai), |
| 254 | + .dapm_widgets = audiosense_pi_dapm_widgets, |
| 255 | + .num_dapm_widgets = ARRAY_SIZE(audiosense_pi_dapm_widgets), |
| 256 | + .dapm_routes = audiosense_pi_audio_map, |
| 257 | + .num_dapm_routes = ARRAY_SIZE(audiosense_pi_audio_map), |
| 258 | +}; |
| 259 | + |
| 260 | +static int audiosense_pi_card_probe(struct platform_device *pdev) |
| 261 | +{ |
| 262 | + int ret = 0; |
| 263 | + struct snd_soc_card *card = &audiosense_pi_card; |
| 264 | + struct snd_soc_dai_link *dai = &audiosense_pi_card_dai[0]; |
| 265 | + struct device_node *i2s_node = pdev->dev.of_node; |
| 266 | + |
| 267 | + card->dev = &pdev->dev; |
| 268 | + |
| 269 | + if (!dai) { |
| 270 | + dev_err(&pdev->dev, "DAI not found. Missing or Invalid\n"); |
| 271 | + return -EINVAL; |
| 272 | + } |
| 273 | + |
| 274 | + i2s_node = of_parse_phandle(pdev->dev.of_node, "i2s-controller", 0); |
| 275 | + if (!i2s_node) { |
| 276 | + dev_err(&pdev->dev, |
| 277 | + "Property 'i2s-controller' missing or invalid\n"); |
| 278 | + return -EINVAL; |
| 279 | + } |
| 280 | + |
| 281 | + dai->cpus->dai_name = NULL; |
| 282 | + dai->cpus->of_node = i2s_node; |
| 283 | + dai->platforms->name = NULL; |
| 284 | + dai->platforms->of_node = i2s_node; |
| 285 | + |
| 286 | + of_node_put(i2s_node); |
| 287 | + |
| 288 | + ret = snd_soc_register_card(card); |
| 289 | + if (ret && ret != -EPROBE_DEFER) |
| 290 | + dev_err(&pdev->dev, |
| 291 | + "snd_soc_register_card() failed: %d\n", ret); |
| 292 | + |
| 293 | + return ret; |
| 294 | +} |
| 295 | + |
| 296 | +static int audiosense_pi_card_remove(struct platform_device *pdev) |
| 297 | +{ |
| 298 | + struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 299 | + |
| 300 | + return snd_soc_unregister_card(card); |
| 301 | + |
| 302 | +} |
| 303 | + |
| 304 | +static const struct of_device_id audiosense_pi_card_of_match[] = { |
| 305 | + { .compatible = "as,audiosense-pi", }, |
| 306 | + {}, |
| 307 | +}; |
| 308 | +MODULE_DEVICE_TABLE(of, audiosense_pi_card_of_match); |
| 309 | + |
| 310 | +static struct platform_driver audiosense_pi_card_driver = { |
| 311 | + .driver = { |
| 312 | + .name = "audiosense-snd-card", |
| 313 | + .owner = THIS_MODULE, |
| 314 | + .of_match_table = audiosense_pi_card_of_match, |
| 315 | + }, |
| 316 | + .probe = audiosense_pi_card_probe, |
| 317 | + .remove = audiosense_pi_card_remove, |
| 318 | +}; |
| 319 | + |
| 320 | +module_platform_driver(audiosense_pi_card_driver); |
| 321 | + |
| 322 | +MODULE_AUTHOR("Bhargav AK <anur.bhargav@gmail.com>"); |
| 323 | +MODULE_DESCRIPTION("ASoC Driver for TLV320AIC3204 Audio"); |
| 324 | +MODULE_LICENSE("GPL v2"); |
| 325 | +MODULE_ALIAS("platform:audiosense-pi"); |
| 326 | + |