Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Rockchip machine ASoC driver for boards using a RT5645/RT5650 CODEC. |
| 4 | * |
| 5 | * Copyright (c) 2015, ROCKCHIP CORPORATION. All rights reserved. |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 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 <linux/delay.h> |
| 14 | #include <sound/core.h> |
| 15 | #include <sound/jack.h> |
| 16 | #include <sound/pcm.h> |
| 17 | #include <sound/pcm_params.h> |
| 18 | #include <sound/soc.h> |
| 19 | #include "rockchip_i2s.h" |
Kuninori Morimoto | 79223bf | 2018-01-29 03:49:31 +0000 | [diff] [blame] | 20 | #include "../codecs/rt5645.h" |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 21 | |
| 22 | #define DRV_NAME "rockchip-snd-rt5645" |
| 23 | |
| 24 | static struct snd_soc_jack headset_jack; |
| 25 | |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 26 | static const struct snd_soc_dapm_widget rk_dapm_widgets[] = { |
| 27 | SND_SOC_DAPM_HP("Headphones", NULL), |
| 28 | SND_SOC_DAPM_SPK("Speakers", NULL), |
| 29 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 30 | SND_SOC_DAPM_MIC("Int Mic", NULL), |
| 31 | }; |
| 32 | |
| 33 | static const struct snd_soc_dapm_route rk_audio_map[] = { |
| 34 | /* Input Lines */ |
| 35 | {"DMIC L2", NULL, "Int Mic"}, |
| 36 | {"DMIC R2", NULL, "Int Mic"}, |
| 37 | {"RECMIXL", NULL, "Headset Mic"}, |
| 38 | {"RECMIXR", NULL, "Headset Mic"}, |
| 39 | |
| 40 | /* Output Lines */ |
| 41 | {"Headphones", NULL, "HPOR"}, |
| 42 | {"Headphones", NULL, "HPOL"}, |
| 43 | {"Speakers", NULL, "SPOL"}, |
| 44 | {"Speakers", NULL, "SPOR"}, |
| 45 | }; |
| 46 | |
| 47 | static const struct snd_kcontrol_new rk_mc_controls[] = { |
| 48 | SOC_DAPM_PIN_SWITCH("Headphones"), |
| 49 | SOC_DAPM_PIN_SWITCH("Speakers"), |
| 50 | SOC_DAPM_PIN_SWITCH("Headset Mic"), |
| 51 | SOC_DAPM_PIN_SWITCH("Int Mic"), |
| 52 | }; |
| 53 | |
| 54 | static int rk_aif1_hw_params(struct snd_pcm_substream *substream, |
| 55 | struct snd_pcm_hw_params *params) |
| 56 | { |
| 57 | int ret = 0; |
Kuninori Morimoto | 5c5eb29 | 2020-07-20 10:18:28 +0900 | [diff] [blame] | 58 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Kuninori Morimoto | a7ff526 | 2020-03-23 14:20:09 +0900 | [diff] [blame] | 59 | struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); |
| 60 | struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 61 | int mclk; |
| 62 | |
| 63 | switch (params_rate(params)) { |
| 64 | case 8000: |
| 65 | case 16000: |
Caesar Wang | 3a3b070 | 2015-11-06 19:38:16 +0800 | [diff] [blame] | 66 | case 24000: |
| 67 | case 32000: |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 68 | case 48000: |
Caesar Wang | 3a3b070 | 2015-11-06 19:38:16 +0800 | [diff] [blame] | 69 | case 64000: |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 70 | case 96000: |
| 71 | mclk = 12288000; |
| 72 | break; |
Caesar Wang | 3a3b070 | 2015-11-06 19:38:16 +0800 | [diff] [blame] | 73 | case 11025: |
| 74 | case 22050: |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 75 | case 44100: |
Caesar Wang | 3a3b070 | 2015-11-06 19:38:16 +0800 | [diff] [blame] | 76 | case 88200: |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 77 | mclk = 11289600; |
| 78 | break; |
| 79 | default: |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, |
| 84 | SND_SOC_CLOCK_OUT); |
| 85 | if (ret < 0) { |
| 86 | dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 91 | SND_SOC_CLOCK_IN); |
| 92 | if (ret < 0) { |
| 93 | dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int rk_init(struct snd_soc_pcm_runtime *runtime) |
| 101 | { |
| 102 | struct snd_soc_card *card = runtime->card; |
| 103 | int ret; |
| 104 | |
| 105 | /* Enable Headset and 4 Buttons Jack detection */ |
| 106 | ret = snd_soc_card_jack_new(card, "Headset Jack", |
| 107 | SND_JACK_HEADPHONE | SND_JACK_MICROPHONE | |
| 108 | SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 109 | SND_JACK_BTN_2 | SND_JACK_BTN_3, |
| 110 | &headset_jack, NULL, 0); |
Xing Zheng | f8ce200 | 2015-08-25 15:52:42 +0800 | [diff] [blame] | 111 | if (ret) { |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 112 | dev_err(card->dev, "New Headset Jack failed! (%d)\n", ret); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Kuninori Morimoto | a7ff526 | 2020-03-23 14:20:09 +0900 | [diff] [blame] | 116 | return rt5645_set_jack_detect(asoc_rtd_to_codec(runtime, 0)->component, |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 117 | &headset_jack, |
| 118 | &headset_jack, |
| 119 | &headset_jack); |
| 120 | } |
| 121 | |
Julia Lawall | 705e999 | 2016-10-15 16:55:47 +0200 | [diff] [blame] | 122 | static const struct snd_soc_ops rk_aif1_ops = { |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 123 | .hw_params = rk_aif1_hw_params, |
| 124 | }; |
| 125 | |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 126 | SND_SOC_DAILINK_DEFS(pcm, |
| 127 | DAILINK_COMP_ARRAY(COMP_EMPTY()), |
Kuninori Morimoto | f94d7b6 | 2019-06-28 10:48:01 +0900 | [diff] [blame] | 128 | DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1")), |
| 129 | DAILINK_COMP_ARRAY(COMP_EMPTY())); |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 130 | |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 131 | static struct snd_soc_dai_link rk_dailink = { |
| 132 | .name = "rt5645", |
| 133 | .stream_name = "rt5645 PCM", |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 134 | .init = rk_init, |
| 135 | .ops = &rk_aif1_ops, |
| 136 | /* set rt5645 as slave */ |
| 137 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 138 | SND_SOC_DAIFMT_CBS_CFS, |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 139 | SND_SOC_DAILINK_REG(pcm), |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static struct snd_soc_card snd_soc_card_rk = { |
| 143 | .name = "I2S-RT5650", |
Axel Lin | 54d8697 | 2015-08-21 20:59:21 +0800 | [diff] [blame] | 144 | .owner = THIS_MODULE, |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 145 | .dai_link = &rk_dailink, |
| 146 | .num_links = 1, |
| 147 | .dapm_widgets = rk_dapm_widgets, |
| 148 | .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets), |
| 149 | .dapm_routes = rk_audio_map, |
| 150 | .num_dapm_routes = ARRAY_SIZE(rk_audio_map), |
| 151 | .controls = rk_mc_controls, |
| 152 | .num_controls = ARRAY_SIZE(rk_mc_controls), |
| 153 | }; |
| 154 | |
| 155 | static int snd_rk_mc_probe(struct platform_device *pdev) |
| 156 | { |
| 157 | int ret = 0; |
| 158 | struct snd_soc_card *card = &snd_soc_card_rk; |
| 159 | struct device_node *np = pdev->dev.of_node; |
| 160 | |
| 161 | /* register the soc card */ |
| 162 | card->dev = &pdev->dev; |
| 163 | |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 164 | rk_dailink.codecs->of_node = of_parse_phandle(np, |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 165 | "rockchip,audio-codec", 0); |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 166 | if (!rk_dailink.codecs->of_node) { |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 167 | dev_err(&pdev->dev, |
| 168 | "Property 'rockchip,audio-codec' missing or invalid\n"); |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 172 | rk_dailink.cpus->of_node = of_parse_phandle(np, |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 173 | "rockchip,i2s-controller", 0); |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 174 | if (!rk_dailink.cpus->of_node) { |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 175 | dev_err(&pdev->dev, |
| 176 | "Property 'rockchip,i2s-controller' missing or invalid\n"); |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 177 | ret = -EINVAL; |
| 178 | goto put_codec_of_node; |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 179 | } |
| 180 | |
Kuninori Morimoto | f94d7b6 | 2019-06-28 10:48:01 +0900 | [diff] [blame] | 181 | rk_dailink.platforms->of_node = rk_dailink.cpus->of_node; |
| 182 | |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 183 | ret = snd_soc_of_parse_card_name(card, "rockchip,model"); |
| 184 | if (ret) { |
| 185 | dev_err(&pdev->dev, |
| 186 | "Soc parse card name failed %d\n", ret); |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 187 | goto put_cpu_of_node; |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ret = devm_snd_soc_register_card(&pdev->dev, card); |
| 191 | if (ret) { |
| 192 | dev_err(&pdev->dev, |
| 193 | "Soc register card failed %d\n", ret); |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 194 | goto put_cpu_of_node; |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return ret; |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 198 | |
| 199 | put_cpu_of_node: |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 200 | of_node_put(rk_dailink.cpus->of_node); |
| 201 | rk_dailink.cpus->of_node = NULL; |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 202 | put_codec_of_node: |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 203 | of_node_put(rk_dailink.codecs->of_node); |
| 204 | rk_dailink.codecs->of_node = NULL; |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static int snd_rk_mc_remove(struct platform_device *pdev) |
| 210 | { |
Kuninori Morimoto | 0209bf2 | 2019-06-06 13:17:37 +0900 | [diff] [blame] | 211 | of_node_put(rk_dailink.cpus->of_node); |
| 212 | rk_dailink.cpus->of_node = NULL; |
| 213 | of_node_put(rk_dailink.codecs->of_node); |
| 214 | rk_dailink.codecs->of_node = NULL; |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 215 | |
| 216 | return 0; |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static const struct of_device_id rockchip_rt5645_of_match[] = { |
| 220 | { .compatible = "rockchip,rockchip-audio-rt5645", }, |
| 221 | {}, |
| 222 | }; |
| 223 | |
| 224 | MODULE_DEVICE_TABLE(of, rockchip_rt5645_of_match); |
| 225 | |
| 226 | static struct platform_driver snd_rk_mc_driver = { |
| 227 | .probe = snd_rk_mc_probe, |
Alexey Khoroshilov | a56df73 | 2018-06-10 01:20:54 +0300 | [diff] [blame] | 228 | .remove = snd_rk_mc_remove, |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 229 | .driver = { |
| 230 | .name = DRV_NAME, |
zhengxing | 8605965 | 2015-07-19 19:33:49 +0800 | [diff] [blame] | 231 | .pm = &snd_soc_pm_ops, |
| 232 | .of_match_table = rockchip_rt5645_of_match, |
| 233 | }, |
| 234 | }; |
| 235 | |
| 236 | module_platform_driver(snd_rk_mc_driver); |
| 237 | |
| 238 | MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>"); |
| 239 | MODULE_DESCRIPTION("Rockchip rt5645 machine ASoC driver"); |
| 240 | MODULE_LICENSE("GPL v2"); |
| 241 | MODULE_ALIAS("platform:" DRV_NAME); |