1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ASoC driver for PROTO AudioCODEC (with a WM8731) 4 * 5 * Author: Florian Meier, <koalo@koalo.de> 6 * Copyright 2013 7 */ 8 9 #include <linux/module.h> 10 #include <linux/platform_device.h> 11 12 #include <sound/core.h> 13 #include <sound/pcm.h> 14 #include <sound/soc.h> 15 #include <sound/jack.h> 16 17 #include "../codecs/wm8731.h" 18 19 #define XTAL_RATE 12288000 /* This is fixed on this board */ 20 21 static int snd_proto_init(struct snd_soc_pcm_runtime *rtd) 22 { 23 struct snd_soc_card *card = rtd->card; 24 struct snd_soc_dai *codec_dai = rtd->codec_dai; 25 26 /* Set proto sysclk */ 27 int ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, 28 XTAL_RATE, SND_SOC_CLOCK_IN); 29 if (ret < 0) { 30 dev_err(card->dev, "Failed to set WM8731 SYSCLK: %d\n", 31 ret); 32 return ret; 33 } 34 35 return 0; 36 } 37 38 static const struct snd_soc_dapm_widget snd_proto_widget[] = { 39 SND_SOC_DAPM_MIC("Microphone Jack", NULL), 40 SND_SOC_DAPM_HP("Headphone Jack", NULL), 41 }; 42 43 static const struct snd_soc_dapm_route snd_proto_route[] = { 44 /* speaker connected to LHPOUT/RHPOUT */ 45 {"Headphone Jack", NULL, "LHPOUT"}, 46 {"Headphone Jack", NULL, "RHPOUT"}, 47 48 /* mic is connected to Mic Jack, with WM8731 Mic Bias */ 49 {"MICIN", NULL, "Mic Bias"}, 50 {"Mic Bias", NULL, "Microphone Jack"}, 51 }; 52 53 /* audio machine driver */ 54 static struct snd_soc_card snd_proto = { 55 .name = "snd_mikroe_proto", 56 .owner = THIS_MODULE, 57 .dapm_widgets = snd_proto_widget, 58 .num_dapm_widgets = ARRAY_SIZE(snd_proto_widget), 59 .dapm_routes = snd_proto_route, 60 .num_dapm_routes = ARRAY_SIZE(snd_proto_route), 61 }; 62 63 static int snd_proto_probe(struct platform_device *pdev) 64 { 65 struct snd_soc_dai_link *dai; 66 struct device_node *np = pdev->dev.of_node; 67 struct device_node *codec_np, *cpu_np; 68 struct device_node *bitclkmaster = NULL; 69 struct device_node *framemaster = NULL; 70 unsigned int dai_fmt; 71 int ret = 0; 72 73 if (!np) { 74 dev_err(&pdev->dev, "No device node supplied\n"); 75 return -EINVAL; 76 } 77 78 snd_proto.dev = &pdev->dev; 79 ret = snd_soc_of_parse_card_name(&snd_proto, "model"); 80 if (ret) 81 return ret; 82 83 dai = devm_kzalloc(&pdev->dev, sizeof(*dai), GFP_KERNEL); 84 if (!dai) 85 return -ENOMEM; 86 87 snd_proto.dai_link = dai; 88 snd_proto.num_links = 1; 89 90 dai->name = "WM8731"; 91 dai->stream_name = "WM8731 HiFi"; 92 dai->codec_dai_name = "wm8731-hifi"; 93 dai->init = &snd_proto_init; 94 95 codec_np = of_parse_phandle(np, "audio-codec", 0); 96 if (!codec_np) { 97 dev_err(&pdev->dev, "audio-codec node missing\n"); 98 return -EINVAL; 99 } 100 dai->codec_of_node = codec_np; 101 102 cpu_np = of_parse_phandle(np, "i2s-controller", 0); 103 if (!cpu_np) { 104 dev_err(&pdev->dev, "i2s-controller missing\n"); 105 return -EINVAL; 106 } 107 dai->cpu_of_node = cpu_np; 108 dai->platform_of_node = cpu_np; 109 110 dai_fmt = snd_soc_of_parse_daifmt(np, NULL, 111 &bitclkmaster, &framemaster); 112 if (bitclkmaster != framemaster) { 113 dev_err(&pdev->dev, "Must be the same bitclock and frame master\n"); 114 return -EINVAL; 115 } 116 if (bitclkmaster) { 117 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 118 if (codec_np == bitclkmaster) 119 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 120 else 121 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 122 } 123 of_node_put(bitclkmaster); 124 of_node_put(framemaster); 125 dai->dai_fmt = dai_fmt; 126 127 of_node_put(codec_np); 128 of_node_put(cpu_np); 129 130 ret = snd_soc_register_card(&snd_proto); 131 if (ret && ret != -EPROBE_DEFER) 132 dev_err(&pdev->dev, 133 "snd_soc_register_card() failed: %d\n", ret); 134 135 return ret; 136 } 137 138 static int snd_proto_remove(struct platform_device *pdev) 139 { 140 return snd_soc_unregister_card(&snd_proto); 141 } 142 143 static const struct of_device_id snd_proto_of_match[] = { 144 { .compatible = "mikroe,mikroe-proto", }, 145 {}, 146 }; 147 MODULE_DEVICE_TABLE(of, snd_proto_of_match); 148 149 static struct platform_driver snd_proto_driver = { 150 .driver = { 151 .name = "snd-mikroe-proto", 152 .of_match_table = snd_proto_of_match, 153 }, 154 .probe = snd_proto_probe, 155 .remove = snd_proto_remove, 156 }; 157 158 module_platform_driver(snd_proto_driver); 159 160 MODULE_AUTHOR("Florian Meier"); 161 MODULE_DESCRIPTION("ASoC Driver for PROTO board (WM8731)"); 162 MODULE_LICENSE("GPL"); 163