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> |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 19 | #include "common.h" |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 20 | |
| 21 | struct apq8016_sbc_data { |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 22 | struct snd_soc_card card; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 23 | void __iomem *mic_iomux; |
| 24 | void __iomem *spkr_iomux; |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 25 | struct snd_soc_jack jack; |
| 26 | bool jack_setup; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
Srinivas Kandagatla | bbedefb | 2016-02-11 12:18:45 +0000 | [diff] [blame] | 29 | #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21) |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 30 | #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17) |
| 31 | #define MIC_CTRL_TLMM_SCLK_EN BIT(1) |
| 32 | #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16)) |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 33 | #define DEFAULT_MCLK_RATE 9600000 |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 34 | |
| 35 | static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 36 | { |
Kuninori Morimoto | 6e3a98b | 2020-03-23 14:20:01 +0900 | [diff] [blame] | 37 | struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); |
Kuninori Morimoto | cf4dae0 | 2020-02-19 15:56:09 +0900 | [diff] [blame] | 38 | struct snd_soc_dai *codec_dai; |
Kuninori Morimoto | 96e1b9e | 2017-12-05 04:24:13 +0000 | [diff] [blame] | 39 | struct snd_soc_component *component; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 40 | struct snd_soc_card *card = rtd->card; |
| 41 | struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card); |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 42 | int i, rval; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 43 | |
| 44 | switch (cpu_dai->id) { |
| 45 | case MI2S_PRIMARY: |
| 46 | writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11, |
| 47 | pdata->spkr_iomux); |
| 48 | break; |
| 49 | |
| 50 | case MI2S_QUATERNARY: |
| 51 | /* Configure the Quat MI2S to TLMM */ |
| 52 | writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 | |
| 53 | MIC_CTRL_TLMM_SCLK_EN, |
| 54 | pdata->mic_iomux); |
| 55 | break; |
Srinivas Kandagatla | bbedefb | 2016-02-11 12:18:45 +0000 | [diff] [blame] | 56 | case MI2S_TERTIARY: |
| 57 | writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL | |
| 58 | MIC_CTRL_TLMM_SCLK_EN, |
| 59 | pdata->mic_iomux); |
| 60 | |
| 61 | break; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 62 | |
| 63 | default: |
| 64 | dev_err(card->dev, "unsupported cpu dai configuration\n"); |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 65 | return -EINVAL; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 66 | |
| 67 | } |
| 68 | |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 69 | if (!pdata->jack_setup) { |
| 70 | struct snd_jack *jack; |
| 71 | |
| 72 | rval = snd_soc_card_jack_new(card, "Headset Jack", |
| 73 | SND_JACK_HEADSET | |
| 74 | SND_JACK_HEADPHONE | |
| 75 | SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 76 | SND_JACK_BTN_2 | SND_JACK_BTN_3 | |
| 77 | SND_JACK_BTN_4, |
| 78 | &pdata->jack, NULL, 0); |
| 79 | |
| 80 | if (rval < 0) { |
| 81 | dev_err(card->dev, "Unable to add Headphone Jack\n"); |
| 82 | return rval; |
| 83 | } |
| 84 | |
| 85 | jack = pdata->jack.jack; |
| 86 | |
Benson Leung | 5f6d1df | 2017-11-22 12:56:43 -0800 | [diff] [blame] | 87 | snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); |
Srinivas Kandagatla | b47b91c | 2017-08-17 10:02:11 +0200 | [diff] [blame] | 88 | snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); |
| 89 | snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP); |
| 90 | snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); |
| 91 | pdata->jack_setup = true; |
| 92 | } |
| 93 | |
Kuninori Morimoto | c998ee3 | 2020-03-09 13:07:57 +0900 | [diff] [blame] | 94 | for_each_rtd_codec_dais(rtd, i, codec_dai) { |
Srinivas Kandagatla | 334822a | 2017-08-02 15:17:47 +0200 | [diff] [blame] | 95 | |
Kuninori Morimoto | cf4dae0 | 2020-02-19 15:56:09 +0900 | [diff] [blame] | 96 | component = codec_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 | |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 114 | static void apq8016_sbc_add_ops(struct snd_soc_card *card) |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 115 | { |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 116 | struct snd_soc_dai_link *link; |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 117 | int i; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 118 | |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 119 | for_each_card_prelinks(card, i, link) |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 120 | link->init = apq8016_sbc_dai_init; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Srinivas Kandagatla | 1190300 | 2016-09-06 10:57:42 +0100 | [diff] [blame] | 123 | static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = { |
| 124 | |
| 125 | SND_SOC_DAPM_MIC("Handset Mic", NULL), |
| 126 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 127 | SND_SOC_DAPM_MIC("Secondary Mic", NULL), |
| 128 | SND_SOC_DAPM_MIC("Digital Mic1", NULL), |
| 129 | SND_SOC_DAPM_MIC("Digital Mic2", NULL), |
| 130 | }; |
| 131 | |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 132 | static int apq8016_sbc_platform_probe(struct platform_device *pdev) |
| 133 | { |
| 134 | struct device *dev = &pdev->dev; |
| 135 | struct snd_soc_card *card; |
| 136 | struct apq8016_sbc_data *data; |
| 137 | struct resource *res; |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 138 | int ret; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 139 | |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 140 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 141 | if (!data) |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 142 | return -ENOMEM; |
| 143 | |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 144 | card = &data->card; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 145 | card->dev = dev; |
Srinivas Kandagatla | 1190300 | 2016-09-06 10:57:42 +0100 | [diff] [blame] | 146 | card->dapm_widgets = apq8016_sbc_dapm_widgets; |
| 147 | card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets); |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 148 | |
| 149 | ret = qcom_snd_parse_of(card); |
| 150 | if (ret) |
| 151 | return ret; |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 152 | |
| 153 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux"); |
| 154 | data->mic_iomux = devm_ioremap_resource(dev, res); |
| 155 | if (IS_ERR(data->mic_iomux)) |
| 156 | return PTR_ERR(data->mic_iomux); |
| 157 | |
| 158 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux"); |
| 159 | data->spkr_iomux = devm_ioremap_resource(dev, res); |
| 160 | if (IS_ERR(data->spkr_iomux)) |
| 161 | return PTR_ERR(data->spkr_iomux); |
| 162 | |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 163 | snd_soc_card_set_drvdata(card, data); |
| 164 | |
Stephan Gerhold | 118205d2 | 2020-07-23 20:39:03 +0200 | [diff] [blame^] | 165 | apq8016_sbc_add_ops(card); |
Srinivas Kandagatla | bdb052e | 2015-06-10 13:15:54 +0100 | [diff] [blame] | 166 | return devm_snd_soc_register_card(&pdev->dev, card); |
| 167 | } |
| 168 | |
| 169 | static const struct of_device_id apq8016_sbc_device_id[] = { |
| 170 | { .compatible = "qcom,apq8016-sbc-sndcard" }, |
| 171 | {}, |
| 172 | }; |
| 173 | MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id); |
| 174 | |
| 175 | static struct platform_driver apq8016_sbc_platform_driver = { |
| 176 | .driver = { |
| 177 | .name = "qcom-apq8016-sbc", |
| 178 | .of_match_table = of_match_ptr(apq8016_sbc_device_id), |
| 179 | }, |
| 180 | .probe = apq8016_sbc_platform_probe, |
| 181 | }; |
| 182 | module_platform_driver(apq8016_sbc_platform_driver); |
| 183 | |
| 184 | MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); |
| 185 | MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver"); |
| 186 | MODULE_LICENSE("GPL v2"); |