1 /* 2 * Copyright (c) 2015 The Linux Foundation. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 and 6 * only version 2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 */ 14 15 #include <linux/device.h> 16 #include <linux/module.h> 17 #include <linux/kernel.h> 18 #include <linux/io.h> 19 #include <linux/of.h> 20 #include <linux/clk.h> 21 #include <linux/platform_device.h> 22 #include <sound/pcm.h> 23 #include <sound/pcm_params.h> 24 #include <sound/soc.h> 25 #include <dt-bindings/sound/apq8016-lpass.h> 26 27 struct apq8016_sbc_data { 28 void __iomem *mic_iomux; 29 void __iomem *spkr_iomux; 30 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */ 31 }; 32 33 #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21) 34 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17) 35 #define MIC_CTRL_TLMM_SCLK_EN BIT(1) 36 #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16)) 37 38 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd) 39 { 40 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 41 struct snd_soc_card *card = rtd->card; 42 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card); 43 int rval = 0; 44 45 switch (cpu_dai->id) { 46 case MI2S_PRIMARY: 47 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11, 48 pdata->spkr_iomux); 49 break; 50 51 case MI2S_QUATERNARY: 52 /* Configure the Quat MI2S to TLMM */ 53 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 | 54 MIC_CTRL_TLMM_SCLK_EN, 55 pdata->mic_iomux); 56 break; 57 case MI2S_TERTIARY: 58 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL | 59 MIC_CTRL_TLMM_SCLK_EN, 60 pdata->mic_iomux); 61 62 break; 63 64 default: 65 dev_err(card->dev, "unsupported cpu dai configuration\n"); 66 rval = -EINVAL; 67 break; 68 69 } 70 71 return rval; 72 } 73 74 static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card) 75 { 76 struct device *dev = card->dev; 77 struct snd_soc_dai_link *link; 78 struct device_node *np, *codec, *cpu, *node = dev->of_node; 79 struct apq8016_sbc_data *data; 80 int ret, num_links; 81 82 ret = snd_soc_of_parse_card_name(card, "qcom,model"); 83 if (ret) { 84 dev_err(dev, "Error parsing card name: %d\n", ret); 85 return ERR_PTR(ret); 86 } 87 88 /* Populate links */ 89 num_links = of_get_child_count(node); 90 91 /* Allocate the private data and the DAI link array */ 92 data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links, 93 GFP_KERNEL); 94 if (!data) 95 return ERR_PTR(-ENOMEM); 96 97 card->dai_link = &data->dai_link[0]; 98 card->num_links = num_links; 99 100 link = data->dai_link; 101 102 for_each_child_of_node(node, np) { 103 cpu = of_get_child_by_name(np, "cpu"); 104 codec = of_get_child_by_name(np, "codec"); 105 106 if (!cpu || !codec) { 107 dev_err(dev, "Can't find cpu/codec DT node\n"); 108 return ERR_PTR(-EINVAL); 109 } 110 111 link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0); 112 if (!link->cpu_of_node) { 113 dev_err(card->dev, "error getting cpu phandle\n"); 114 return ERR_PTR(-EINVAL); 115 } 116 117 link->codec_of_node = of_parse_phandle(codec, "sound-dai", 0); 118 if (!link->codec_of_node) { 119 dev_err(card->dev, "error getting codec phandle\n"); 120 return ERR_PTR(-EINVAL); 121 } 122 123 ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name); 124 if (ret) { 125 dev_err(card->dev, "error getting cpu dai name\n"); 126 return ERR_PTR(ret); 127 } 128 129 ret = snd_soc_of_get_dai_name(codec, &link->codec_dai_name); 130 if (ret) { 131 dev_err(card->dev, "error getting codec dai name\n"); 132 return ERR_PTR(ret); 133 } 134 135 link->platform_of_node = link->cpu_of_node; 136 ret = of_property_read_string(np, "link-name", &link->name); 137 if (ret) { 138 dev_err(card->dev, "error getting codec dai_link name\n"); 139 return ERR_PTR(ret); 140 } 141 142 link->stream_name = link->name; 143 link->init = apq8016_sbc_dai_init; 144 link++; 145 } 146 147 return data; 148 } 149 150 static int apq8016_sbc_platform_probe(struct platform_device *pdev) 151 { 152 struct device *dev = &pdev->dev; 153 struct snd_soc_card *card; 154 struct apq8016_sbc_data *data; 155 struct resource *res; 156 157 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 158 if (!card) 159 return -ENOMEM; 160 161 card->dev = dev; 162 data = apq8016_sbc_parse_of(card); 163 if (IS_ERR(data)) { 164 dev_err(&pdev->dev, "Error resolving dai links: %ld\n", 165 PTR_ERR(data)); 166 return PTR_ERR(data); 167 } 168 169 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux"); 170 data->mic_iomux = devm_ioremap_resource(dev, res); 171 if (IS_ERR(data->mic_iomux)) 172 return PTR_ERR(data->mic_iomux); 173 174 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux"); 175 data->spkr_iomux = devm_ioremap_resource(dev, res); 176 if (IS_ERR(data->spkr_iomux)) 177 return PTR_ERR(data->spkr_iomux); 178 179 platform_set_drvdata(pdev, data); 180 snd_soc_card_set_drvdata(card, data); 181 182 return devm_snd_soc_register_card(&pdev->dev, card); 183 } 184 185 static const struct of_device_id apq8016_sbc_device_id[] = { 186 { .compatible = "qcom,apq8016-sbc-sndcard" }, 187 {}, 188 }; 189 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id); 190 191 static struct platform_driver apq8016_sbc_platform_driver = { 192 .driver = { 193 .name = "qcom-apq8016-sbc", 194 .of_match_table = of_match_ptr(apq8016_sbc_device_id), 195 }, 196 .probe = apq8016_sbc_platform_probe, 197 }; 198 module_platform_driver(apq8016_sbc_platform_driver); 199 200 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); 201 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver"); 202 MODULE_LICENSE("GPL v2"); 203