1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ASoC driver for TI DAVINCI EVM platform 4 * 5 * Author: Vladimir Barinov, <vbarinov@embeddedalley.com> 6 * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/moduleparam.h> 11 #include <linux/timer.h> 12 #include <linux/interrupt.h> 13 #include <linux/platform_device.h> 14 #include <linux/i2c.h> 15 #include <linux/of_platform.h> 16 #include <linux/clk.h> 17 #include <sound/core.h> 18 #include <sound/pcm.h> 19 #include <sound/soc.h> 20 21 #include <asm/dma.h> 22 #include <asm/mach-types.h> 23 24 struct snd_soc_card_drvdata_davinci { 25 struct clk *mclk; 26 unsigned sysclk; 27 }; 28 29 static int evm_startup(struct snd_pcm_substream *substream) 30 { 31 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 32 struct snd_soc_card *soc_card = rtd->card; 33 struct snd_soc_card_drvdata_davinci *drvdata = 34 snd_soc_card_get_drvdata(soc_card); 35 36 if (drvdata->mclk) 37 return clk_prepare_enable(drvdata->mclk); 38 39 return 0; 40 } 41 42 static void evm_shutdown(struct snd_pcm_substream *substream) 43 { 44 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 45 struct snd_soc_card *soc_card = rtd->card; 46 struct snd_soc_card_drvdata_davinci *drvdata = 47 snd_soc_card_get_drvdata(soc_card); 48 49 clk_disable_unprepare(drvdata->mclk); 50 } 51 52 static int evm_hw_params(struct snd_pcm_substream *substream, 53 struct snd_pcm_hw_params *params) 54 { 55 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 56 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 57 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 58 struct snd_soc_card *soc_card = rtd->card; 59 int ret = 0; 60 unsigned sysclk = ((struct snd_soc_card_drvdata_davinci *) 61 snd_soc_card_get_drvdata(soc_card))->sysclk; 62 63 /* set the codec system clock */ 64 ret = snd_soc_dai_set_sysclk(codec_dai, 0, sysclk, SND_SOC_CLOCK_OUT); 65 if (ret < 0) 66 return ret; 67 68 /* set the CPU system clock */ 69 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, sysclk, SND_SOC_CLOCK_OUT); 70 if (ret < 0 && ret != -ENOTSUPP) 71 return ret; 72 73 return 0; 74 } 75 76 static const struct snd_soc_ops evm_ops = { 77 .startup = evm_startup, 78 .shutdown = evm_shutdown, 79 .hw_params = evm_hw_params, 80 }; 81 82 /* davinci-evm machine dapm widgets */ 83 static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = { 84 SND_SOC_DAPM_HP("Headphone Jack", NULL), 85 SND_SOC_DAPM_LINE("Line Out", NULL), 86 SND_SOC_DAPM_MIC("Mic Jack", NULL), 87 SND_SOC_DAPM_LINE("Line In", NULL), 88 }; 89 90 /* davinci-evm machine audio_mapnections to the codec pins */ 91 static const struct snd_soc_dapm_route audio_map[] = { 92 /* Headphone connected to HPLOUT, HPROUT */ 93 {"Headphone Jack", NULL, "HPLOUT"}, 94 {"Headphone Jack", NULL, "HPROUT"}, 95 96 /* Line Out connected to LLOUT, RLOUT */ 97 {"Line Out", NULL, "LLOUT"}, 98 {"Line Out", NULL, "RLOUT"}, 99 100 /* Mic connected to (MIC3L | MIC3R) */ 101 {"MIC3L", NULL, "Mic Bias"}, 102 {"MIC3R", NULL, "Mic Bias"}, 103 {"Mic Bias", NULL, "Mic Jack"}, 104 105 /* Line In connected to (LINE1L | LINE2L), (LINE1R | LINE2R) */ 106 {"LINE1L", NULL, "Line In"}, 107 {"LINE2L", NULL, "Line In"}, 108 {"LINE1R", NULL, "Line In"}, 109 {"LINE2R", NULL, "Line In"}, 110 }; 111 112 /* Logic for a aic3x as connected on a davinci-evm */ 113 static int evm_aic3x_init(struct snd_soc_pcm_runtime *rtd) 114 { 115 struct snd_soc_card *card = rtd->card; 116 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(rtd->card); 117 struct device_node *np = card->dev->of_node; 118 int ret; 119 120 /* Add davinci-evm specific widgets */ 121 snd_soc_dapm_new_controls(dapm, aic3x_dapm_widgets, 122 ARRAY_SIZE(aic3x_dapm_widgets)); 123 124 if (np) { 125 ret = snd_soc_of_parse_audio_routing(card, "ti,audio-routing"); 126 if (ret) 127 return ret; 128 } else { 129 /* Set up davinci-evm specific audio path audio_map */ 130 snd_soc_dapm_add_routes(dapm, audio_map, 131 ARRAY_SIZE(audio_map)); 132 } 133 134 /* not connected */ 135 snd_soc_dapm_disable_pin(dapm, "MONO_LOUT"); 136 snd_soc_dapm_disable_pin(dapm, "HPLCOM"); 137 snd_soc_dapm_disable_pin(dapm, "HPRCOM"); 138 139 return 0; 140 } 141 142 /* 143 * The struct is used as place holder. It will be completely 144 * filled with data from dt node. 145 */ 146 SND_SOC_DAILINK_DEFS(evm, 147 DAILINK_COMP_ARRAY(COMP_EMPTY()), 148 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic3x-hifi")), 149 DAILINK_COMP_ARRAY(COMP_EMPTY())); 150 151 static struct snd_soc_dai_link evm_dai_tlv320aic3x = { 152 .name = "TLV320AIC3X", 153 .stream_name = "AIC3X", 154 .ops = &evm_ops, 155 .init = evm_aic3x_init, 156 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBP_CFP | 157 SND_SOC_DAIFMT_IB_NF, 158 SND_SOC_DAILINK_REG(evm), 159 }; 160 161 static const struct of_device_id davinci_evm_dt_ids[] = { 162 { 163 .compatible = "ti,da830-evm-audio", 164 .data = (void *) &evm_dai_tlv320aic3x, 165 }, 166 { /* sentinel */ } 167 }; 168 MODULE_DEVICE_TABLE(of, davinci_evm_dt_ids); 169 170 /* davinci evm audio machine driver */ 171 static struct snd_soc_card evm_soc_card = { 172 .owner = THIS_MODULE, 173 .num_links = 1, 174 }; 175 176 static int davinci_evm_probe(struct platform_device *pdev) 177 { 178 struct device_node *np = pdev->dev.of_node; 179 struct snd_soc_dai_link *dai; 180 struct snd_soc_card_drvdata_davinci *drvdata = NULL; 181 struct clk *mclk; 182 int ret = 0; 183 184 dai = (struct snd_soc_dai_link *) device_get_match_data(&pdev->dev); 185 if (!dai) { 186 dev_err(&pdev->dev, "Error: No device match found\n"); 187 return -ENODEV; 188 } 189 190 evm_soc_card.dai_link = dai; 191 192 dai->codecs->of_node = of_parse_phandle(np, "ti,audio-codec", 0); 193 if (!dai->codecs->of_node) 194 return -EINVAL; 195 196 dai->cpus->of_node = of_parse_phandle(np, "ti,mcasp-controller", 0); 197 if (!dai->cpus->of_node) { 198 ret = -EINVAL; 199 goto err_put; 200 } 201 202 dai->platforms->of_node = dai->cpus->of_node; 203 204 evm_soc_card.dev = &pdev->dev; 205 ret = snd_soc_of_parse_card_name(&evm_soc_card, "ti,model"); 206 if (ret) 207 goto err_put; 208 209 mclk = devm_clk_get(&pdev->dev, "mclk"); 210 if (PTR_ERR(mclk) == -EPROBE_DEFER) { 211 ret = -EPROBE_DEFER; 212 goto err_put; 213 } else if (IS_ERR(mclk)) { 214 dev_dbg(&pdev->dev, "mclk not found.\n"); 215 mclk = NULL; 216 } 217 218 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); 219 if (!drvdata) { 220 ret = -ENOMEM; 221 goto err_put; 222 } 223 224 drvdata->mclk = mclk; 225 226 ret = of_property_read_u32(np, "ti,codec-clock-rate", &drvdata->sysclk); 227 228 if (ret < 0) { 229 if (!drvdata->mclk) { 230 dev_err(&pdev->dev, 231 "No clock or clock rate defined.\n"); 232 ret = -EINVAL; 233 goto err_put; 234 } 235 drvdata->sysclk = clk_get_rate(drvdata->mclk); 236 } else if (drvdata->mclk) { 237 unsigned int requestd_rate = drvdata->sysclk; 238 clk_set_rate(drvdata->mclk, drvdata->sysclk); 239 drvdata->sysclk = clk_get_rate(drvdata->mclk); 240 if (drvdata->sysclk != requestd_rate) 241 dev_warn(&pdev->dev, 242 "Could not get requested rate %u using %u.\n", 243 requestd_rate, drvdata->sysclk); 244 } 245 246 snd_soc_card_set_drvdata(&evm_soc_card, drvdata); 247 ret = devm_snd_soc_register_card(&pdev->dev, &evm_soc_card); 248 249 if (ret) { 250 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret); 251 goto err_put; 252 } 253 254 return ret; 255 256 err_put: 257 dai->platforms->of_node = NULL; 258 259 if (dai->cpus->of_node) { 260 of_node_put(dai->cpus->of_node); 261 dai->cpus->of_node = NULL; 262 } 263 264 if (dai->codecs->of_node) { 265 of_node_put(dai->codecs->of_node); 266 dai->codecs->of_node = NULL; 267 } 268 269 return ret; 270 } 271 272 static struct platform_driver davinci_evm_driver = { 273 .probe = davinci_evm_probe, 274 .driver = { 275 .name = "davinci_evm", 276 .pm = &snd_soc_pm_ops, 277 .of_match_table = davinci_evm_dt_ids, 278 }, 279 }; 280 281 module_platform_driver(davinci_evm_driver); 282 283 MODULE_AUTHOR("Vladimir Barinov"); 284 MODULE_DESCRIPTION("TI DAVINCI EVM ASoC driver"); 285 MODULE_LICENSE("GPL"); 286