|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * rockchip_rt5651_rk628.c -- RK3399 machine driver with |
| 4 | + * RT5651/RK628 codecs |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Rockchip Electronics Co. Ltd. |
| 7 | + * Author: binyuan <kevan.lan@rock-chips.com> |
| 8 | + */ |
| 9 | + |
| 10 | +#include <linux/module.h> |
| 11 | +#include <sound/soc.h> |
| 12 | + |
| 13 | +#include "rockchip_i2s.h" |
| 14 | +#include "../codecs/rt5651.h" |
| 15 | + |
| 16 | +#define DRV_NAME "rk3399-rt5651-rk628" |
| 17 | + |
| 18 | +static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = { |
| 19 | + SND_SOC_DAPM_HP("Headphones", NULL), |
| 20 | + SND_SOC_DAPM_SPK("Lineout", NULL), |
| 21 | + SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 22 | + SND_SOC_DAPM_MIC("Int Mic", NULL), |
| 23 | + SND_SOC_DAPM_MIC("HDMIIN", NULL), |
| 24 | +}; |
| 25 | + |
| 26 | +static const struct snd_soc_dapm_route rockchip_dapm_routes[] = { |
| 27 | + {"Headphones", NULL, "HPOL"}, |
| 28 | + {"Headphones", NULL, "HPOR"}, |
| 29 | + {"Lineout", NULL, "LOUTL"}, |
| 30 | + {"Lineout", NULL, "LOUTR"}, |
| 31 | + {"AIF2 Playback", NULL, "HDMIIN"}, |
| 32 | +}; |
| 33 | + |
| 34 | +static const struct snd_kcontrol_new rockchip_controls[] = { |
| 35 | + SOC_DAPM_PIN_SWITCH("Headphones"), |
| 36 | + SOC_DAPM_PIN_SWITCH("Lineout"), |
| 37 | + SOC_DAPM_PIN_SWITCH("Headset Mic"), |
| 38 | + SOC_DAPM_PIN_SWITCH("Int Mic"), |
| 39 | +}; |
| 40 | + |
| 41 | +static int rockchip_rt5651_hw_params(struct snd_pcm_substream *substream, |
| 42 | + struct snd_pcm_hw_params *params) |
| 43 | +{ |
| 44 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 45 | + struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 46 | + struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 47 | + int mclk, ret; |
| 48 | + |
| 49 | + /* in bypass mode, the mclk has to be one of the frequencies below */ |
| 50 | + switch (params_rate(params)) { |
| 51 | + case 8000: |
| 52 | + case 16000: |
| 53 | + case 24000: |
| 54 | + case 32000: |
| 55 | + case 48000: |
| 56 | + case 64000: |
| 57 | + case 96000: |
| 58 | + mclk = 12288000; |
| 59 | + break; |
| 60 | + case 11025: |
| 61 | + case 22050: |
| 62 | + case 44100: |
| 63 | + case 88200: |
| 64 | + mclk = 11289600; |
| 65 | + break; |
| 66 | + default: |
| 67 | + return -EINVAL; |
| 68 | + } |
| 69 | + |
| 70 | + ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, SND_SOC_CLOCK_OUT); |
| 71 | + if (ret < 0) { |
| 72 | + dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret); |
| 73 | + return ret; |
| 74 | + } |
| 75 | + |
| 76 | + snd_soc_dai_set_pll(codec_dai, 0, RT5651_PLL1_S_MCLK, mclk, mclk * 2); |
| 77 | + |
| 78 | + ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1, mclk * 2, |
| 79 | + SND_SOC_CLOCK_IN); |
| 80 | + if (ret < 0) { |
| 81 | + dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret); |
| 82 | + return ret; |
| 83 | + } |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static int rockchip_rt5651_voice_hw_params(struct snd_pcm_substream *substream, |
| 89 | + struct snd_pcm_hw_params *params) |
| 90 | +{ |
| 91 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 92 | + struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 93 | + int mclk, ret; |
| 94 | + |
| 95 | + /* in bypass mode, the mclk has to be one of the frequencies below */ |
| 96 | + switch (params_rate(params)) { |
| 97 | + case 8000: |
| 98 | + case 16000: |
| 99 | + case 24000: |
| 100 | + case 32000: |
| 101 | + case 48000: |
| 102 | + case 64000: |
| 103 | + case 96000: |
| 104 | + mclk = 12288000; |
| 105 | + break; |
| 106 | + case 11025: |
| 107 | + case 22050: |
| 108 | + case 44100: |
| 109 | + case 88200: |
| 110 | + mclk = 11289600; |
| 111 | + break; |
| 112 | + default: |
| 113 | + return -EINVAL; |
| 114 | + } |
| 115 | + |
| 116 | + /*Set the system clk for codec*/ |
| 117 | + snd_soc_dai_set_pll(codec_dai, 0, RT5651_PLL1_S_MCLK, mclk, 24576000); |
| 118 | + |
| 119 | + ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1, 24576000, |
| 120 | + SND_SOC_CLOCK_IN); |
| 121 | + if (ret < 0) { |
| 122 | + dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret); |
| 123 | + return ret; |
| 124 | + } |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +static struct snd_soc_ops rockchip_sound_rt5651_hifi_ops = { |
| 129 | + .hw_params = rockchip_rt5651_hw_params, |
| 130 | +}; |
| 131 | + |
| 132 | +static struct snd_soc_ops rockchip_sound_rt5651_voice_ops = { |
| 133 | + .hw_params = rockchip_rt5651_voice_hw_params, |
| 134 | +}; |
| 135 | + |
| 136 | +enum { |
| 137 | + DAILINK_RT5651_HIFI, |
| 138 | + DAILINK_RT5651_VOICE, |
| 139 | + DAILINK_RK628_HDMIIN, |
| 140 | +}; |
| 141 | + |
| 142 | +#define DAILINK_ENTITIES (DAILINK_RK628_HDMIIN + 1) |
| 143 | + |
| 144 | +static struct snd_soc_dai_link rockchip_dailinks[] = { |
| 145 | + [DAILINK_RT5651_HIFI] = { |
| 146 | + .name = "RT5651 HIFI", |
| 147 | + .stream_name = "RT5651 PCM", |
| 148 | + .codec_dai_name = "rt5651-aif1", |
| 149 | + .ops = &rockchip_sound_rt5651_hifi_ops, |
| 150 | + /* set rt5651 as slave */ |
| 151 | + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 152 | + SND_SOC_DAIFMT_CBS_CFS, |
| 153 | + }, |
| 154 | + [DAILINK_RT5651_VOICE] = { |
| 155 | + .name = "RT5651 HDMIIN", |
| 156 | + .stream_name = "RT5651 PCM", |
| 157 | + .codec_dai_name = "rt5651-aif2", |
| 158 | + .ops = &rockchip_sound_rt5651_voice_ops, |
| 159 | + /* set rt5651 as slave */ |
| 160 | + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 161 | + SND_SOC_DAIFMT_CBS_CFS, |
| 162 | + }, |
| 163 | + [DAILINK_RK628_HDMIIN] = { |
| 164 | + .name = "RK628 HDMIIN", |
| 165 | + .stream_name = "RK628 PCM", |
| 166 | + .codec_dai_name = "rk628-audio", |
| 167 | + }, |
| 168 | +}; |
| 169 | + |
| 170 | +static struct snd_soc_card rockchip_sound_card = { |
| 171 | + .name = "realtekrt5651codec_hdmiin", |
| 172 | + .owner = THIS_MODULE, |
| 173 | + .dai_link = rockchip_dailinks, |
| 174 | + .num_links = ARRAY_SIZE(rockchip_dailinks), |
| 175 | + .dapm_widgets = rockchip_dapm_widgets, |
| 176 | + .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets), |
| 177 | + .dapm_routes = rockchip_dapm_routes, |
| 178 | + .num_dapm_routes = ARRAY_SIZE(rockchip_dapm_routes), |
| 179 | + .controls = rockchip_controls, |
| 180 | + .num_controls = ARRAY_SIZE(rockchip_controls), |
| 181 | +}; |
| 182 | + |
| 183 | +static int rockchip_sound_probe(struct platform_device *pdev) |
| 184 | +{ |
| 185 | + struct snd_soc_card *card = &rockchip_sound_card; |
| 186 | + struct device_node *cpu_node; |
| 187 | + int i, ret; |
| 188 | + |
| 189 | + dev_info(&pdev->dev, "%s\n", __func__); |
| 190 | + |
| 191 | + cpu_node = of_parse_phandle(pdev->dev.of_node, "rockchip,cpu", 0); |
| 192 | + if (!cpu_node) { |
| 193 | + dev_err(&pdev->dev, |
| 194 | + "Property 'rockchip,cpu' failed\n"); |
| 195 | + return -EINVAL; |
| 196 | + } |
| 197 | + |
| 198 | + for (i = 0; i < DAILINK_ENTITIES; i++) { |
| 199 | + rockchip_dailinks[i].platform_of_node = cpu_node; |
| 200 | + rockchip_dailinks[i].cpu_of_node = cpu_node; |
| 201 | + |
| 202 | + rockchip_dailinks[i].codec_of_node = |
| 203 | + of_parse_phandle(pdev->dev.of_node, |
| 204 | + "rockchip,codec", i); |
| 205 | + if (!rockchip_dailinks[i].codec_of_node) { |
| 206 | + card->num_links = i; |
| 207 | + break; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + card->dev = &pdev->dev; |
| 212 | + platform_set_drvdata(pdev, card); |
| 213 | + ret = devm_snd_soc_register_card(&pdev->dev, card); |
| 214 | + if (ret) |
| 215 | + dev_err(&pdev->dev, "%s register card failed %d\n", |
| 216 | + __func__, ret); |
| 217 | + |
| 218 | + dev_info(&pdev->dev, "snd_soc_register_card successful\n"); |
| 219 | + return ret; |
| 220 | +} |
| 221 | + |
| 222 | +static const struct of_device_id rockchip_sound_of_match[] = { |
| 223 | + { .compatible = "rockchip,rockchip-rt5651-rk628-sound", }, |
| 224 | + {}, |
| 225 | +}; |
| 226 | + |
| 227 | +static struct platform_driver rockchip_sound_driver = { |
| 228 | + .probe = rockchip_sound_probe, |
| 229 | + .driver = { |
| 230 | + .name = DRV_NAME, |
| 231 | + .of_match_table = rockchip_sound_of_match, |
| 232 | +#ifdef CONFIG_PM |
| 233 | + .pm = &snd_soc_pm_ops, |
| 234 | +#endif |
| 235 | + }, |
| 236 | +}; |
| 237 | + |
| 238 | +module_platform_driver(rockchip_sound_driver); |
| 239 | + |
| 240 | +MODULE_AUTHOR("binyuan <kevan.lan@rock-chips.com>"); |
| 241 | +MODULE_DESCRIPTION("Rockchip ASoC Machine Driver"); |
| 242 | +MODULE_LICENSE("GPL v2"); |
| 243 | +MODULE_ALIAS("platform:" DRV_NAME); |
| 244 | +MODULE_DEVICE_TABLE(of, rockchip_sound_of_match); |
0 commit comments