Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 The Linux Foundation. All rights reserved. |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/device.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/of.h> |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <sound/pcm.h> |
| 14 | #include <sound/pcm_params.h> |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 15 | #include <sound/jack.h> |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 16 | #include <sound/soc.h> |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 17 | #include <uapi/linux/input-event-codes.h> |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 18 | #include <dt-bindings/sound/apq8016-lpass.h> |
| 19 | |
| 20 | struct apq8016_sbc_data { |
| 21 | void __iomem *mic_iomux; |
| 22 | void __iomem *spkr_iomux; |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 23 | struct snd_soc_jack jack; |
| 24 | bool jack_setup; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 25 | struct snd_soc_dai_link dai_link[]; /* dynamically allocated */ |
| 26 | }; |
| 27 | |
Srinivas Kandagatla | bbedefb | 2016-02-11 12:18:45 +0000 | [diff] [blame] | 28 | #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21) |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 29 | #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17) |
| 30 | #define MIC_CTRL_TLMM_SCLK_EN BIT(1) |
| 31 | #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16)) |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 32 | #define DEFAULT_MCLK_RATE 9600000 |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 33 | |
| 34 | static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 35 | { |
| 36 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Kuninori Morimoto | 96e1b9e | 2017-12-05 04:24:13 +0000 | [diff] [blame] | 37 | struct snd_soc_component *component; |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 38 | struct snd_soc_dai_link *dai_link = rtd->dai_link; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 39 | struct snd_soc_card *card = rtd->card; |
| 40 | struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card); |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 41 | int i, rval; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 42 | |
| 43 | switch (cpu_dai->id) { |
| 44 | case MI2S_PRIMARY: |
| 45 | writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11, |
| 46 | pdata->spkr_iomux); |
| 47 | break; |
| 48 | |
| 49 | case MI2S_QUATERNARY: |
| 50 | /* Configure the Quat MI2S to TLMM */ |
| 51 | writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 | |
| 52 | MIC_CTRL_TLMM_SCLK_EN, |
| 53 | pdata->mic_iomux); |
| 54 | break; |
Srinivas Kandagatla | bbedefb | 2016-02-11 12:18:45 +0000 | [diff] [blame] | 55 | case MI2S_TERTIARY: |
| 56 | writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL | |
| 57 | MIC_CTRL_TLMM_SCLK_EN, |
| 58 | pdata->mic_iomux); |
| 59 | |
| 60 | break; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 61 | |
| 62 | default: |
| 63 | dev_err(card->dev, "unsupported cpu dai configuration\n"); |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 64 | return -EINVAL; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 65 | |
| 66 | } |
| 67 | |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 68 | if (!pdata->jack_setup) { |
| 69 | struct snd_jack *jack; |
| 70 | |
| 71 | rval = snd_soc_card_jack_new(card, "Headset Jack", |
| 72 | SND_JACK_HEADSET | |
| 73 | SND_JACK_HEADPHONE | |
| 74 | SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 75 | SND_JACK_BTN_2 | SND_JACK_BTN_3 | |
| 76 | SND_JACK_BTN_4, |
| 77 | &pdata->jack, NULL, 0); |
| 78 | |
| 79 | if (rval < 0) { |
| 80 | dev_err(card->dev, "Unable to add Headphone Jack\n"); |
| 81 | return rval; |
| 82 | } |
| 83 | |
| 84 | jack = pdata->jack.jack; |
| 85 | |
Benson Leung | 5f6d1df | 2017-11-22 12:56:43 -0800 | [diff] [blame] | 86 | snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 87 | snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); |
| 88 | snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP); |
| 89 | snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); |
| 90 | pdata->jack_setup = true; |
| 91 | } |
| 92 | |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 93 | for (i = 0 ; i < dai_link->num_codecs; i++) { |
| 94 | struct snd_soc_dai *dai = rtd->codec_dais[i]; |
| 95 | |
Kuninori Morimoto | 96e1b9e | 2017-12-05 04:24:13 +0000 | [diff] [blame] | 96 | component = dai->component; |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 97 | /* Set default mclk for internal codec */ |
Kuninori Morimoto | 96e1b9e | 2017-12-05 04:24:13 +0000 | [diff] [blame] | 98 | rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE, |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 99 | SND_SOC_CLOCK_IN); |
| 100 | if (rval != 0 && rval != -ENOTSUPP) { |
| 101 | dev_warn(card->dev, "Failed to set mclk: %d\n", rval); |
| 102 | return rval; |
| 103 | } |
Kuninori Morimoto | 96e1b9e | 2017-12-05 04:24:13 +0000 | [diff] [blame] | 104 | rval = snd_soc_component_set_jack(component, &pdata->jack, NULL); |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 105 | if (rval != 0 && rval != -ENOTSUPP) { |
| 106 | dev_warn(card->dev, "Failed to set jack: %d\n", rval); |
| 107 | return rval; |
| 108 | } |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return 0; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card) |
| 115 | { |
| 116 | struct device *dev = card->dev; |
| 117 | struct snd_soc_dai_link *link; |
| 118 | struct device_node *np, *codec, *cpu, *node = dev->of_node; |
| 119 | struct apq8016_sbc_data *data; |
Kuninori Morimoto | 98b232c | 2019-06-06 13:17:04 +0900 | [diff] [blame] | 120 | struct snd_soc_dai_link_component *dlc; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 121 | int ret, num_links; |
| 122 | |
| 123 | ret = snd_soc_of_parse_card_name(card, "qcom,model"); |
| 124 | if (ret) { |
| 125 | dev_err(dev, "Error parsing card name: %d\n", ret); |
| 126 | return ERR_PTR(ret); |
| 127 | } |
| 128 | |
Srinivas Kandagatla | 05f9033 | 2016-09-06 10:57:43 +0100 | [diff] [blame] | 129 | /* DAPM routes */ |
| 130 | if (of_property_read_bool(node, "qcom,audio-routing")) { |
| 131 | ret = snd_soc_of_parse_audio_routing(card, |
| 132 | "qcom,audio-routing"); |
| 133 | if (ret) |
| 134 | return ERR_PTR(ret); |
| 135 | } |
| 136 | |
| 137 | |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 138 | /* Populate links */ |
| 139 | num_links = of_get_child_count(node); |
| 140 | |
| 141 | /* Allocate the private data and the DAI link array */ |
Kees Cook | 0ed2dd0 | 2018-05-08 16:08:53 -0700 | [diff] [blame] | 142 | data = devm_kzalloc(dev, |
| 143 | struct_size(data, dai_link, num_links), |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 144 | GFP_KERNEL); |
| 145 | if (!data) |
| 146 | return ERR_PTR(-ENOMEM); |
| 147 | |
| 148 | card->dai_link = &data->dai_link[0]; |
| 149 | card->num_links = num_links; |
| 150 | |
| 151 | link = data->dai_link; |
| 152 | |
Kuninori Morimoto | 291728a | 2019-06-28 10:47:46 +0900 | [diff] [blame] | 153 | dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL); |
Kuninori Morimoto | 98b232c | 2019-06-06 13:17:04 +0900 | [diff] [blame] | 154 | if (!dlc) |
| 155 | return ERR_PTR(-ENOMEM); |
| 156 | |
Kuninori Morimoto | 291728a | 2019-06-28 10:47:46 +0900 | [diff] [blame] | 157 | link->cpus = &dlc[0]; |
| 158 | link->platforms = &dlc[1]; |
| 159 | |
| 160 | link->num_cpus = 1; |
| 161 | link->num_platforms = 1; |
Kuninori Morimoto | 98b232c | 2019-06-06 13:17:04 +0900 | [diff] [blame] | 162 | |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 163 | for_each_child_of_node(node, np) { |
| 164 | cpu = of_get_child_by_name(np, "cpu"); |
| 165 | codec = of_get_child_by_name(np, "codec"); |
| 166 | |
| 167 | if (!cpu || !codec) { |
| 168 | dev_err(dev, "Can't find cpu/codec DT node\n"); |
Takashi Iwai | 8d16672 | 2019-02-19 16:46:50 +0100 | [diff] [blame] | 169 | ret = -EINVAL; |
| 170 | goto error; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Kuninori Morimoto | 98b232c | 2019-06-06 13:17:04 +0900 | [diff] [blame] | 173 | link->cpus->of_node = of_parse_phandle(cpu, "sound-dai", 0); |
| 174 | if (!link->cpus->of_node) { |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 175 | dev_err(card->dev, "error getting cpu phandle\n"); |
Takashi Iwai | 8d16672 | 2019-02-19 16:46:50 +0100 | [diff] [blame] | 176 | ret = -EINVAL; |
| 177 | goto error; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Kuninori Morimoto | 98b232c | 2019-06-06 13:17:04 +0900 | [diff] [blame] | 180 | ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name); |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 181 | if (ret) { |
| 182 | dev_err(card->dev, "error getting cpu dai name\n"); |
Takashi Iwai | 8d16672 | 2019-02-19 16:46:50 +0100 | [diff] [blame] | 183 | goto error; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Srinivas Kandagatla | 09065f8 | 2016-10-20 15:20:48 +0100 | [diff] [blame] | 186 | ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); |
| 187 | |
| 188 | if (ret < 0) { |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 189 | dev_err(card->dev, "error getting codec dai name\n"); |
Takashi Iwai | 8d16672 | 2019-02-19 16:46:50 +0100 | [diff] [blame] | 190 | goto error; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Kuninori Morimoto | 291728a | 2019-06-28 10:47:46 +0900 | [diff] [blame] | 193 | link->platforms->of_node = link->cpus->of_node; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 194 | ret = of_property_read_string(np, "link-name", &link->name); |
| 195 | if (ret) { |
| 196 | dev_err(card->dev, "error getting codec dai_link name\n"); |
Takashi Iwai | 8d16672 | 2019-02-19 16:46:50 +0100 | [diff] [blame] | 197 | goto error; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | link->stream_name = link->name; |
| 201 | link->init = apq8016_sbc_dai_init; |
| 202 | link++; |
Takashi Iwai | 8d16672 | 2019-02-19 16:46:50 +0100 | [diff] [blame] | 203 | |
| 204 | of_node_put(cpu); |
| 205 | of_node_put(codec); |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return data; |
Takashi Iwai | 8d16672 | 2019-02-19 16:46:50 +0100 | [diff] [blame] | 209 | |
| 210 | error: |
| 211 | of_node_put(np); |
| 212 | of_node_put(cpu); |
| 213 | of_node_put(codec); |
| 214 | return ERR_PTR(ret); |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Srinivas Kandagatla | 1190300 | 2016-09-06 10:57:42 +0100 | [diff] [blame] | 217 | static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = { |
| 218 | |
| 219 | SND_SOC_DAPM_MIC("Handset Mic", NULL), |
| 220 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 221 | SND_SOC_DAPM_MIC("Secondary Mic", NULL), |
| 222 | SND_SOC_DAPM_MIC("Digital Mic1", NULL), |
| 223 | SND_SOC_DAPM_MIC("Digital Mic2", NULL), |
| 224 | }; |
| 225 | |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 226 | static int apq8016_sbc_platform_probe(struct platform_device *pdev) |
| 227 | { |
| 228 | struct device *dev = &pdev->dev; |
| 229 | struct snd_soc_card *card; |
| 230 | struct apq8016_sbc_data *data; |
| 231 | struct resource *res; |
| 232 | |
| 233 | card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); |
| 234 | if (!card) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | card->dev = dev; |
Srinivas Kandagatla | 1190300 | 2016-09-06 10:57:42 +0100 | [diff] [blame] | 238 | card->dapm_widgets = apq8016_sbc_dapm_widgets; |
| 239 | card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets); |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 240 | data = apq8016_sbc_parse_of(card); |
| 241 | if (IS_ERR(data)) { |
| 242 | dev_err(&pdev->dev, "Error resolving dai links: %ld\n", |
| 243 | PTR_ERR(data)); |
| 244 | return PTR_ERR(data); |
| 245 | } |
| 246 | |
| 247 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux"); |
| 248 | data->mic_iomux = devm_ioremap_resource(dev, res); |
| 249 | if (IS_ERR(data->mic_iomux)) |
| 250 | return PTR_ERR(data->mic_iomux); |
| 251 | |
| 252 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux"); |
| 253 | data->spkr_iomux = devm_ioremap_resource(dev, res); |
| 254 | if (IS_ERR(data->spkr_iomux)) |
| 255 | return PTR_ERR(data->spkr_iomux); |
| 256 | |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 257 | snd_soc_card_set_drvdata(card, data); |
| 258 | |
| 259 | return devm_snd_soc_register_card(&pdev->dev, card); |
| 260 | } |
| 261 | |
| 262 | static const struct of_device_id apq8016_sbc_device_id[] = { |
| 263 | { .compatible = "qcom,apq8016-sbc-sndcard" }, |
| 264 | {}, |
| 265 | }; |
| 266 | MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id); |
| 267 | |
| 268 | static struct platform_driver apq8016_sbc_platform_driver = { |
| 269 | .driver = { |
| 270 | .name = "qcom-apq8016-sbc", |
| 271 | .of_match_table = of_match_ptr(apq8016_sbc_device_id), |
| 272 | }, |
| 273 | .probe = apq8016_sbc_platform_probe, |
| 274 | }; |
| 275 | module_platform_driver(apq8016_sbc_platform_driver); |
| 276 | |
| 277 | MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); |
| 278 | MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver"); |
| 279 | MODULE_LICENSE("GPL v2"); |