1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Rockchip machine ASoC driver for RK3288 boards that have an HDMI and analog 4 * audio output 5 * 6 * Copyright (c) 2016, Collabora Ltd. 7 * 8 * Authors: Sjoerd Simons <sjoerd.simons@collabora.com>, 9 * Romain Perier <romain.perier@collabora.com> 10 */ 11 12 #include <linux/module.h> 13 #include <linux/platform_device.h> 14 #include <linux/slab.h> 15 #include <linux/gpio.h> 16 #include <linux/of_gpio.h> 17 #include <sound/core.h> 18 #include <sound/jack.h> 19 #include <sound/pcm.h> 20 #include <sound/pcm_params.h> 21 #include <sound/soc.h> 22 #include <sound/soc-dapm.h> 23 24 #include "rockchip_i2s.h" 25 26 #define DRV_NAME "rk3288-snd-hdmi-analog" 27 28 struct rk_drvdata { 29 int gpio_hp_en; 30 int gpio_hp_det; 31 }; 32 33 static int rk_hp_power(struct snd_soc_dapm_widget *w, 34 struct snd_kcontrol *k, int event) 35 { 36 struct rk_drvdata *machine = snd_soc_card_get_drvdata(w->dapm->card); 37 38 if (!gpio_is_valid(machine->gpio_hp_en)) 39 return 0; 40 41 gpio_set_value_cansleep(machine->gpio_hp_en, 42 SND_SOC_DAPM_EVENT_ON(event)); 43 44 return 0; 45 } 46 47 static struct snd_soc_jack headphone_jack; 48 static struct snd_soc_jack_pin headphone_jack_pins[] = { 49 { 50 .pin = "Analog", 51 .mask = SND_JACK_HEADPHONE 52 }, 53 }; 54 55 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = { 56 SND_SOC_DAPM_HP("Analog", rk_hp_power), 57 SND_SOC_DAPM_LINE("HDMI", NULL), 58 }; 59 60 static const struct snd_kcontrol_new rk_mc_controls[] = { 61 SOC_DAPM_PIN_SWITCH("Analog"), 62 SOC_DAPM_PIN_SWITCH("HDMI"), 63 }; 64 65 static int rk_hw_params(struct snd_pcm_substream *substream, 66 struct snd_pcm_hw_params *params) 67 { 68 int ret = 0; 69 struct snd_soc_pcm_runtime *rtd = substream->private_data; 70 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 71 struct snd_soc_dai *codec_dai = rtd->codec_dai; 72 int mclk; 73 74 switch (params_rate(params)) { 75 case 8000: 76 case 16000: 77 case 24000: 78 case 32000: 79 case 48000: 80 case 64000: 81 case 96000: 82 mclk = 12288000; 83 break; 84 case 192000: 85 mclk = 24576000; 86 break; 87 case 11025: 88 case 22050: 89 case 44100: 90 case 88200: 91 mclk = 11289600; 92 break; 93 default: 94 return -EINVAL; 95 } 96 97 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, 98 SND_SOC_CLOCK_OUT); 99 100 if (ret && ret != -ENOTSUPP) { 101 dev_err(codec_dai->dev, "Can't set cpu clock %d\n", ret); 102 return ret; 103 } 104 105 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, 106 SND_SOC_CLOCK_IN); 107 if (ret && ret != -ENOTSUPP) { 108 dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret); 109 return ret; 110 } 111 112 return 0; 113 } 114 115 static struct snd_soc_jack_gpio rk_hp_jack_gpio = { 116 .name = "Headphone detection", 117 .report = SND_JACK_HEADPHONE, 118 .debounce_time = 150 119 }; 120 121 static int rk_init(struct snd_soc_pcm_runtime *runtime) 122 { 123 struct rk_drvdata *machine = snd_soc_card_get_drvdata(runtime->card); 124 125 /* Enable Headset Jack detection */ 126 if (gpio_is_valid(machine->gpio_hp_det)) { 127 snd_soc_card_jack_new(runtime->card, "Headphone Jack", 128 SND_JACK_HEADPHONE, &headphone_jack, 129 headphone_jack_pins, 130 ARRAY_SIZE(headphone_jack_pins)); 131 rk_hp_jack_gpio.gpio = machine->gpio_hp_det; 132 snd_soc_jack_add_gpios(&headphone_jack, 1, &rk_hp_jack_gpio); 133 } 134 135 return 0; 136 } 137 138 static const struct snd_soc_ops rk_ops = { 139 .hw_params = rk_hw_params, 140 }; 141 142 static struct snd_soc_dai_link_component rk_codecs[] = { 143 { }, 144 { 145 .name = "hdmi-audio-codec.2.auto", 146 .dai_name = "i2s-hifi", 147 }, 148 }; 149 150 static struct snd_soc_dai_link rk_dailink = { 151 .name = "Codecs", 152 .stream_name = "Audio", 153 .init = rk_init, 154 .ops = &rk_ops, 155 .codecs = rk_codecs, 156 .num_codecs = ARRAY_SIZE(rk_codecs), 157 /* Set codecs as slave */ 158 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 159 SND_SOC_DAIFMT_CBS_CFS, 160 }; 161 162 static struct snd_soc_card snd_soc_card_rk = { 163 .name = "ROCKCHIP-I2S", 164 .dai_link = &rk_dailink, 165 .num_links = 1, 166 .num_aux_devs = 0, 167 .dapm_widgets = rk_dapm_widgets, 168 .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets), 169 .controls = rk_mc_controls, 170 .num_controls = ARRAY_SIZE(rk_mc_controls), 171 }; 172 173 static int snd_rk_mc_probe(struct platform_device *pdev) 174 { 175 int ret = 0; 176 struct snd_soc_card *card = &snd_soc_card_rk; 177 struct device_node *np = pdev->dev.of_node; 178 struct rk_drvdata *machine; 179 struct of_phandle_args args; 180 181 machine = devm_kzalloc(&pdev->dev, sizeof(struct rk_drvdata), 182 GFP_KERNEL); 183 if (!machine) 184 return -ENOMEM; 185 186 card->dev = &pdev->dev; 187 188 machine->gpio_hp_det = of_get_named_gpio(np, 189 "rockchip,hp-det-gpios", 0); 190 if (!gpio_is_valid(machine->gpio_hp_det) && machine->gpio_hp_det != -ENODEV) 191 return machine->gpio_hp_det; 192 193 machine->gpio_hp_en = of_get_named_gpio(np, 194 "rockchip,hp-en-gpios", 0); 195 if (!gpio_is_valid(machine->gpio_hp_en) && machine->gpio_hp_en != -ENODEV) 196 return machine->gpio_hp_en; 197 198 if (gpio_is_valid(machine->gpio_hp_en)) { 199 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_en, 200 GPIOF_OUT_INIT_LOW, "hp_en"); 201 if (ret) { 202 dev_err(card->dev, "cannot get hp_en gpio\n"); 203 return ret; 204 } 205 } 206 207 ret = snd_soc_of_parse_card_name(card, "rockchip,model"); 208 if (ret) { 209 dev_err(card->dev, "SoC parse card name failed %d\n", ret); 210 return ret; 211 } 212 213 rk_dailink.codecs[0].of_node = of_parse_phandle(np, 214 "rockchip,audio-codec", 215 0); 216 if (!rk_dailink.codecs[0].of_node) { 217 dev_err(&pdev->dev, 218 "Property 'rockchip,audio-codec' missing or invalid\n"); 219 return -EINVAL; 220 } 221 ret = of_parse_phandle_with_fixed_args(np, "rockchip,audio-codec", 222 0, 0, &args); 223 if (ret) { 224 dev_err(&pdev->dev, 225 "Unable to parse property 'rockchip,audio-codec'\n"); 226 return ret; 227 } 228 229 ret = snd_soc_get_dai_name(&args, &rk_dailink.codecs[0].dai_name); 230 if (ret) { 231 dev_err(&pdev->dev, "Unable to get codec_dai_name\n"); 232 return ret; 233 } 234 235 rk_dailink.cpu_of_node = of_parse_phandle(np, "rockchip,i2s-controller", 236 0); 237 if (!rk_dailink.cpu_of_node) { 238 dev_err(&pdev->dev, 239 "Property 'rockchip,i2s-controller' missing or invalid\n"); 240 return -EINVAL; 241 } 242 243 rk_dailink.platform_of_node = rk_dailink.cpu_of_node; 244 245 ret = snd_soc_of_parse_audio_routing(card, "rockchip,routing"); 246 if (ret) { 247 dev_err(&pdev->dev, 248 "Unable to parse 'rockchip,routing' property\n"); 249 return ret; 250 } 251 252 snd_soc_card_set_drvdata(card, machine); 253 254 ret = devm_snd_soc_register_card(&pdev->dev, card); 255 if (ret == -EPROBE_DEFER) 256 return -EPROBE_DEFER; 257 if (ret) { 258 dev_err(&pdev->dev, 259 "Soc register card failed %d\n", ret); 260 return ret; 261 } 262 263 return ret; 264 } 265 266 static const struct of_device_id rockchip_sound_of_match[] = { 267 { .compatible = "rockchip,rk3288-hdmi-analog", }, 268 {}, 269 }; 270 271 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match); 272 273 static struct platform_driver rockchip_sound_driver = { 274 .probe = snd_rk_mc_probe, 275 .driver = { 276 .name = DRV_NAME, 277 .pm = &snd_soc_pm_ops, 278 .of_match_table = rockchip_sound_of_match, 279 }, 280 }; 281 282 module_platform_driver(rockchip_sound_driver); 283 284 MODULE_AUTHOR("Sjoerd Simons <sjoerd.simons@collabora.com>"); 285 MODULE_DESCRIPTION("Rockchip RK3288 machine ASoC driver"); 286 MODULE_LICENSE("GPL v2"); 287 MODULE_ALIAS("platform:" DRV_NAME); 288