|
| 1 | +/* |
| 2 | + * ASoC Driver for IQaudIO Raspberry Pi Codec board |
| 3 | + * |
| 4 | + * Author: Gordon Garrity <[email protected]> |
| 5 | + * (C) Copyright IQaudio Limited, 2017-2019 |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU General Public License |
| 9 | + * version 2 as published by the Free Software Foundation. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * General Public License for more details. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <linux/module.h> |
| 18 | +#include <linux/gpio/consumer.h> |
| 19 | +#include <linux/platform_device.h> |
| 20 | + |
| 21 | +#include <sound/core.h> |
| 22 | +#include <sound/pcm.h> |
| 23 | +#include <sound/pcm_params.h> |
| 24 | +#include <sound/soc.h> |
| 25 | +#include <sound/jack.h> |
| 26 | + |
| 27 | +#include <linux/acpi.h> |
| 28 | +#include <linux/slab.h> |
| 29 | +#include "../codecs/da7213.h" |
| 30 | + |
| 31 | +static int pll_out = DA7213_PLL_FREQ_OUT_90316800; |
| 32 | + |
| 33 | +static int snd_rpi_iqaudio_pll_control(struct snd_soc_dapm_widget *w, |
| 34 | + struct snd_kcontrol *k, int event) |
| 35 | +{ |
| 36 | + int ret = 0; |
| 37 | + struct snd_soc_dapm_context *dapm = w->dapm; |
| 38 | + struct snd_soc_card *card = dapm->card; |
| 39 | + struct snd_soc_pcm_runtime *rtd = |
| 40 | + snd_soc_get_pcm_runtime(card, card->dai_link[0].name); |
| 41 | + struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 42 | + |
| 43 | + if (SND_SOC_DAPM_EVENT_OFF(event)) { |
| 44 | + ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_MCLK, 0, |
| 45 | + 0); |
| 46 | + if (ret) |
| 47 | + dev_err(card->dev, "Failed to bypass PLL: %d\n", ret); |
| 48 | + } else if (SND_SOC_DAPM_EVENT_ON(event)) { |
| 49 | + ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_PLL, 0, |
| 50 | + pll_out); |
| 51 | + if (ret) |
| 52 | + dev_err(card->dev, "Failed to enable PLL: %d\n", ret); |
| 53 | + } |
| 54 | + |
| 55 | + return ret; |
| 56 | +} |
| 57 | + |
| 58 | +static int snd_rpi_iqaudio_post_dapm_event(struct snd_soc_dapm_widget *w, |
| 59 | + struct snd_kcontrol *kcontrol, |
| 60 | + int event) |
| 61 | +{ |
| 62 | + switch (event) { |
| 63 | + case SND_SOC_DAPM_POST_PMU: |
| 64 | + /* Delay for mic bias ramp */ |
| 65 | + msleep(1000); |
| 66 | + break; |
| 67 | + default: |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +static const struct snd_soc_dapm_widget dapm_widgets[] = { |
| 75 | + SND_SOC_DAPM_HP("HP Jack", NULL), |
| 76 | + SND_SOC_DAPM_MIC("MIC Jack", NULL), |
| 77 | + SND_SOC_DAPM_MIC("Onboard MIC", NULL), |
| 78 | + SND_SOC_DAPM_LINE("AUX Jack", NULL), |
| 79 | + SND_SOC_DAPM_SUPPLY("PLL Control", SND_SOC_NOPM, 0, 0, |
| 80 | + snd_rpi_iqaudio_pll_control, |
| 81 | + SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
| 82 | + SND_SOC_DAPM_POST("Post Power Up Event", snd_rpi_iqaudio_post_dapm_event), |
| 83 | +}; |
| 84 | + |
| 85 | +static const struct snd_soc_dapm_route audio_map[] = { |
| 86 | + {"HP Jack", NULL, "HPL"}, |
| 87 | + {"HP Jack", NULL, "HPR"}, |
| 88 | + {"HP Jack", NULL, "PLL Control"}, |
| 89 | + |
| 90 | + {"AUX Jack", NULL, "AUXR"}, |
| 91 | + {"AUX Jack", NULL, "AUXL"}, |
| 92 | + {"AUX Jack", NULL, "PLL Control"}, |
| 93 | + |
| 94 | + /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */ |
| 95 | + {"MIC Jack", NULL, "MIC1"}, |
| 96 | + {"MIC Jack", NULL, "PLL Control"}, |
| 97 | + {"Onboard MIC", NULL, "MIC2"}, |
| 98 | + {"Onboard MIC", NULL, "PLL Control"}, |
| 99 | +}; |
| 100 | + |
| 101 | +/* machine stream operations */ |
| 102 | + |
| 103 | +static int snd_rpi_iqaudio_codec_init(struct snd_soc_pcm_runtime *rtd) |
| 104 | +{ |
| 105 | + struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 106 | + struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 107 | + int ret; |
| 108 | + |
| 109 | + /* Set bclk ratio to align with codec's BCLK rate */ |
| 110 | + ret = snd_soc_dai_set_bclk_ratio(cpu_dai, 64); |
| 111 | + if (ret) { |
| 112 | + dev_err(rtd->dev, "Failed to set CPU BLCK ratio\n"); |
| 113 | + return ret; |
| 114 | + } |
| 115 | + |
| 116 | + /* Set MCLK frequency to codec, onboard 11.2896MHz clock */ |
| 117 | + return snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK, 11289600, |
| 118 | + SND_SOC_CLOCK_OUT); |
| 119 | +} |
| 120 | + |
| 121 | +static int snd_rpi_iqaudio_codec_hw_params(struct snd_pcm_substream *substream, |
| 122 | + struct snd_pcm_hw_params *params) |
| 123 | +{ |
| 124 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 125 | + unsigned int samplerate = params_rate(params); |
| 126 | + |
| 127 | + switch (samplerate) { |
| 128 | + case 8000: |
| 129 | + case 16000: |
| 130 | + case 32000: |
| 131 | + case 48000: |
| 132 | + case 96000: |
| 133 | + pll_out = DA7213_PLL_FREQ_OUT_98304000; |
| 134 | + return 0; |
| 135 | + case 44100: |
| 136 | + case 88200: |
| 137 | + pll_out = DA7213_PLL_FREQ_OUT_90316800; |
| 138 | + return 0; |
| 139 | + default: |
| 140 | + dev_err(rtd->dev,"Unsupported samplerate %d\n", samplerate); |
| 141 | + return -EINVAL; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +static const struct snd_soc_ops snd_rpi_iqaudio_codec_ops = { |
| 146 | + .hw_params = snd_rpi_iqaudio_codec_hw_params, |
| 147 | +}; |
| 148 | + |
| 149 | + |
| 150 | +static struct snd_soc_dai_link snd_rpi_iqaudio_codec_dai[] = { |
| 151 | +{ |
| 152 | + .cpu_dai_name = "bcm2708-i2s.0", |
| 153 | + .codec_dai_name = "da7213-hifi", |
| 154 | + .platform_name = "bmc2708-i2s.0", |
| 155 | + .codec_name = "da7213.1-001a", |
| 156 | + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 157 | + SND_SOC_DAIFMT_CBM_CFM, |
| 158 | + .init = snd_rpi_iqaudio_codec_init, |
| 159 | + .ops = &snd_rpi_iqaudio_codec_ops, |
| 160 | + .symmetric_rates = 1, |
| 161 | + .symmetric_channels = 1, |
| 162 | + .symmetric_samplebits = 1, |
| 163 | +}, |
| 164 | +}; |
| 165 | + |
| 166 | +/* audio machine driver */ |
| 167 | +static struct snd_soc_card snd_rpi_iqaudio_codec = { |
| 168 | + .owner = THIS_MODULE, |
| 169 | + .dai_link = snd_rpi_iqaudio_codec_dai, |
| 170 | + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_codec_dai), |
| 171 | + .dapm_widgets = dapm_widgets, |
| 172 | + .num_dapm_widgets = ARRAY_SIZE(dapm_widgets), |
| 173 | + .dapm_routes = audio_map, |
| 174 | + .num_dapm_routes = ARRAY_SIZE(audio_map), |
| 175 | +}; |
| 176 | + |
| 177 | +static int snd_rpi_iqaudio_codec_probe(struct platform_device *pdev) |
| 178 | +{ |
| 179 | + int ret = 0; |
| 180 | + |
| 181 | + snd_rpi_iqaudio_codec.dev = &pdev->dev; |
| 182 | + |
| 183 | + if (pdev->dev.of_node) { |
| 184 | + struct device_node *i2s_node; |
| 185 | + struct snd_soc_card *card = &snd_rpi_iqaudio_codec; |
| 186 | + struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_codec_dai[0]; |
| 187 | + |
| 188 | + i2s_node = of_parse_phandle(pdev->dev.of_node, |
| 189 | + "i2s-controller", 0); |
| 190 | + if (i2s_node) { |
| 191 | + dai->cpu_dai_name = NULL; |
| 192 | + dai->cpu_of_node = i2s_node; |
| 193 | + dai->platform_name = NULL; |
| 194 | + dai->platform_of_node = i2s_node; |
| 195 | + } |
| 196 | + |
| 197 | + if (of_property_read_string(pdev->dev.of_node, "card_name", |
| 198 | + &card->name)) |
| 199 | + card->name = "IQaudIOCODEC"; |
| 200 | + |
| 201 | + if (of_property_read_string(pdev->dev.of_node, "dai_name", |
| 202 | + &dai->name)) |
| 203 | + dai->name = "IQaudIO CODEC"; |
| 204 | + |
| 205 | + if (of_property_read_string(pdev->dev.of_node, |
| 206 | + "dai_stream_name", &dai->stream_name)) |
| 207 | + dai->stream_name = "IQaudIO CODEC HiFi v1.1"; |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + ret = snd_soc_register_card(&snd_rpi_iqaudio_codec); |
| 212 | + if (ret) { |
| 213 | + if (ret != -EPROBE_DEFER) |
| 214 | + dev_err(&pdev->dev, |
| 215 | + "snd_soc_register_card() failed: %d\n", ret); |
| 216 | + return ret; |
| 217 | + } |
| 218 | + |
| 219 | + return 0; |
| 220 | +} |
| 221 | + |
| 222 | +static int snd_rpi_iqaudio_codec_remove(struct platform_device *pdev) |
| 223 | +{ |
| 224 | + return snd_soc_unregister_card(&snd_rpi_iqaudio_codec); |
| 225 | +} |
| 226 | + |
| 227 | +static const struct of_device_id iqaudio_of_match[] = { |
| 228 | + { .compatible = "iqaudio,iqaudio-codec", }, |
| 229 | + {}, |
| 230 | +}; |
| 231 | + |
| 232 | +MODULE_DEVICE_TABLE(of, iqaudio_of_match); |
| 233 | + |
| 234 | +static struct platform_driver snd_rpi_iqaudio_codec_driver = { |
| 235 | + .driver = { |
| 236 | + .name = "snd-rpi-iqaudio-codec", |
| 237 | + .owner = THIS_MODULE, |
| 238 | + .of_match_table = iqaudio_of_match, |
| 239 | + }, |
| 240 | + .probe = snd_rpi_iqaudio_codec_probe, |
| 241 | + .remove = snd_rpi_iqaudio_codec_remove, |
| 242 | +}; |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | +module_platform_driver(snd_rpi_iqaudio_codec_driver); |
| 247 | + |
| 248 | +MODULE_AUTHOR( "Gordon Garrity <[email protected]>"); |
| 249 | +MODULE_DESCRIPTION("ASoC Driver for IQaudIO CODEC"); |
| 250 | +MODULE_LICENSE("GPL v2"); |
0 commit comments