1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Rockchip machine ASoC driver for boards using a MAX90809 CODEC. 4 * 5 * Copyright (c) 2014, ROCKCHIP CORPORATION. All rights reserved. 6 */ 7 8 #include <linux/module.h> 9 #include <linux/platform_device.h> 10 #include <linux/slab.h> 11 #include <linux/gpio.h> 12 #include <linux/of_gpio.h> 13 #include <sound/core.h> 14 #include <sound/jack.h> 15 #include <sound/pcm.h> 16 #include <sound/pcm_params.h> 17 #include <sound/soc.h> 18 19 #include "rockchip_i2s.h" 20 #include "../codecs/ts3a227e.h" 21 22 #define DRV_NAME "rockchip-snd-max98090" 23 24 static struct snd_soc_jack headset_jack; 25 26 /* Headset jack detection DAPM pins */ 27 static struct snd_soc_jack_pin headset_jack_pins[] = { 28 { 29 .pin = "Headphone", 30 .mask = SND_JACK_HEADPHONE, 31 }, 32 { 33 .pin = "Headset Mic", 34 .mask = SND_JACK_MICROPHONE, 35 }, 36 37 }; 38 39 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = { 40 SND_SOC_DAPM_HP("Headphone", NULL), 41 SND_SOC_DAPM_MIC("Headset Mic", NULL), 42 SND_SOC_DAPM_MIC("Int Mic", NULL), 43 SND_SOC_DAPM_SPK("Speaker", NULL), 44 }; 45 46 static const struct snd_soc_dapm_route rk_audio_map[] = { 47 {"IN34", NULL, "Headset Mic"}, 48 {"IN34", NULL, "MICBIAS"}, 49 {"Headset Mic", NULL, "MICBIAS"}, 50 {"DMICL", NULL, "Int Mic"}, 51 {"Headphone", NULL, "HPL"}, 52 {"Headphone", NULL, "HPR"}, 53 {"Speaker", NULL, "SPKL"}, 54 {"Speaker", NULL, "SPKR"}, 55 }; 56 57 static const struct snd_kcontrol_new rk_mc_controls[] = { 58 SOC_DAPM_PIN_SWITCH("Headphone"), 59 SOC_DAPM_PIN_SWITCH("Headset Mic"), 60 SOC_DAPM_PIN_SWITCH("Int Mic"), 61 SOC_DAPM_PIN_SWITCH("Speaker"), 62 }; 63 64 static int rk_aif1_hw_params(struct snd_pcm_substream *substream, 65 struct snd_pcm_hw_params *params) 66 { 67 int ret = 0; 68 struct snd_soc_pcm_runtime *rtd = substream->private_data; 69 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 70 struct snd_soc_dai *codec_dai = rtd->codec_dai; 71 int mclk; 72 73 switch (params_rate(params)) { 74 case 8000: 75 case 16000: 76 case 24000: 77 case 32000: 78 case 48000: 79 case 64000: 80 case 96000: 81 mclk = 12288000; 82 break; 83 case 11025: 84 case 22050: 85 case 44100: 86 case 88200: 87 mclk = 11289600; 88 break; 89 default: 90 return -EINVAL; 91 } 92 93 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, 94 SND_SOC_CLOCK_OUT); 95 if (ret < 0) { 96 dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret); 97 return ret; 98 } 99 100 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, 101 SND_SOC_CLOCK_IN); 102 if (ret < 0) { 103 dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret); 104 return ret; 105 } 106 107 return ret; 108 } 109 110 static const struct snd_soc_ops rk_aif1_ops = { 111 .hw_params = rk_aif1_hw_params, 112 }; 113 114 static struct snd_soc_dai_link rk_dailink = { 115 .name = "max98090", 116 .stream_name = "Audio", 117 .codec_dai_name = "HiFi", 118 .ops = &rk_aif1_ops, 119 /* set max98090 as slave */ 120 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 121 SND_SOC_DAIFMT_CBS_CFS, 122 }; 123 124 static int rk_98090_headset_init(struct snd_soc_component *component); 125 126 static struct snd_soc_aux_dev rk_98090_headset_dev = { 127 .name = "Headset Chip", 128 .init = rk_98090_headset_init, 129 }; 130 131 static struct snd_soc_card snd_soc_card_rk = { 132 .name = "ROCKCHIP-I2S", 133 .owner = THIS_MODULE, 134 .dai_link = &rk_dailink, 135 .num_links = 1, 136 .aux_dev = &rk_98090_headset_dev, 137 .num_aux_devs = 1, 138 .dapm_widgets = rk_dapm_widgets, 139 .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets), 140 .dapm_routes = rk_audio_map, 141 .num_dapm_routes = ARRAY_SIZE(rk_audio_map), 142 .controls = rk_mc_controls, 143 .num_controls = ARRAY_SIZE(rk_mc_controls), 144 }; 145 146 static int rk_98090_headset_init(struct snd_soc_component *component) 147 { 148 int ret; 149 150 /* Enable Headset and 4 Buttons Jack detection */ 151 ret = snd_soc_card_jack_new(&snd_soc_card_rk, "Headset Jack", 152 SND_JACK_HEADSET | 153 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 154 SND_JACK_BTN_2 | SND_JACK_BTN_3, 155 &headset_jack, 156 headset_jack_pins, 157 ARRAY_SIZE(headset_jack_pins)); 158 if (ret) 159 return ret; 160 161 ret = ts3a227e_enable_jack_detect(component, &headset_jack); 162 163 return ret; 164 } 165 166 static int snd_rk_mc_probe(struct platform_device *pdev) 167 { 168 int ret = 0; 169 struct snd_soc_card *card = &snd_soc_card_rk; 170 struct device_node *np = pdev->dev.of_node; 171 172 /* register the soc card */ 173 card->dev = &pdev->dev; 174 175 rk_dailink.codec_of_node = of_parse_phandle(np, 176 "rockchip,audio-codec", 0); 177 if (!rk_dailink.codec_of_node) { 178 dev_err(&pdev->dev, 179 "Property 'rockchip,audio-codec' missing or invalid\n"); 180 return -EINVAL; 181 } 182 183 rk_dailink.cpu_of_node = of_parse_phandle(np, 184 "rockchip,i2s-controller", 0); 185 if (!rk_dailink.cpu_of_node) { 186 dev_err(&pdev->dev, 187 "Property 'rockchip,i2s-controller' missing or invalid\n"); 188 return -EINVAL; 189 } 190 191 rk_dailink.platform_of_node = rk_dailink.cpu_of_node; 192 193 rk_98090_headset_dev.codec_of_node = of_parse_phandle(np, 194 "rockchip,headset-codec", 0); 195 if (!rk_98090_headset_dev.codec_of_node) { 196 dev_err(&pdev->dev, 197 "Property 'rockchip,headset-codec' missing/invalid\n"); 198 return -EINVAL; 199 } 200 201 ret = snd_soc_of_parse_card_name(card, "rockchip,model"); 202 if (ret) { 203 dev_err(&pdev->dev, 204 "Soc parse card name failed %d\n", ret); 205 return ret; 206 } 207 208 ret = devm_snd_soc_register_card(&pdev->dev, card); 209 if (ret) { 210 dev_err(&pdev->dev, 211 "Soc register card failed %d\n", ret); 212 return ret; 213 } 214 215 return ret; 216 } 217 218 static const struct of_device_id rockchip_max98090_of_match[] = { 219 { .compatible = "rockchip,rockchip-audio-max98090", }, 220 {}, 221 }; 222 223 MODULE_DEVICE_TABLE(of, rockchip_max98090_of_match); 224 225 static struct platform_driver snd_rk_mc_driver = { 226 .probe = snd_rk_mc_probe, 227 .driver = { 228 .name = DRV_NAME, 229 .pm = &snd_soc_pm_ops, 230 .of_match_table = rockchip_max98090_of_match, 231 }, 232 }; 233 234 module_platform_driver(snd_rk_mc_driver); 235 236 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>"); 237 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver"); 238 MODULE_LICENSE("GPL v2"); 239 MODULE_ALIAS("platform:" DRV_NAME); 240