1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Copyright 2012 Freescale Semiconductor, Inc. 4 // Copyright 2012 Linaro Ltd. 5 6 #include <linux/module.h> 7 #include <linux/of.h> 8 #include <linux/of_platform.h> 9 #include <linux/i2c.h> 10 #include <linux/clk.h> 11 #include <sound/soc.h> 12 13 #include "../codecs/sgtl5000.h" 14 #include "imx-audmux.h" 15 16 #define DAI_NAME_SIZE 32 17 18 struct imx_sgtl5000_data { 19 struct snd_soc_dai_link dai; 20 struct snd_soc_card card; 21 char codec_dai_name[DAI_NAME_SIZE]; 22 char platform_name[DAI_NAME_SIZE]; 23 struct clk *codec_clk; 24 unsigned int clk_frequency; 25 }; 26 27 static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime *rtd) 28 { 29 struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(rtd->card); 30 struct device *dev = rtd->card->dev; 31 int ret; 32 33 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, SGTL5000_SYSCLK, 34 data->clk_frequency, SND_SOC_CLOCK_IN); 35 if (ret) { 36 dev_err(dev, "could not set codec driver clock params\n"); 37 return ret; 38 } 39 40 return 0; 41 } 42 43 static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets[] = { 44 SND_SOC_DAPM_MIC("Mic Jack", NULL), 45 SND_SOC_DAPM_LINE("Line In Jack", NULL), 46 SND_SOC_DAPM_HP("Headphone Jack", NULL), 47 SND_SOC_DAPM_SPK("Line Out Jack", NULL), 48 SND_SOC_DAPM_SPK("Ext Spk", NULL), 49 }; 50 51 static int imx_sgtl5000_probe(struct platform_device *pdev) 52 { 53 struct device_node *np = pdev->dev.of_node; 54 struct device_node *ssi_np, *codec_np; 55 struct platform_device *ssi_pdev; 56 struct i2c_client *codec_dev; 57 struct imx_sgtl5000_data *data = NULL; 58 int int_port, ext_port; 59 int ret; 60 61 ret = of_property_read_u32(np, "mux-int-port", &int_port); 62 if (ret) { 63 dev_err(&pdev->dev, "mux-int-port missing or invalid\n"); 64 return ret; 65 } 66 ret = of_property_read_u32(np, "mux-ext-port", &ext_port); 67 if (ret) { 68 dev_err(&pdev->dev, "mux-ext-port missing or invalid\n"); 69 return ret; 70 } 71 72 /* 73 * The port numbering in the hardware manual starts at 1, while 74 * the audmux API expects it starts at 0. 75 */ 76 int_port--; 77 ext_port--; 78 ret = imx_audmux_v2_configure_port(int_port, 79 IMX_AUDMUX_V2_PTCR_SYN | 80 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) | 81 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) | 82 IMX_AUDMUX_V2_PTCR_TFSDIR | 83 IMX_AUDMUX_V2_PTCR_TCLKDIR, 84 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)); 85 if (ret) { 86 dev_err(&pdev->dev, "audmux internal port setup failed\n"); 87 return ret; 88 } 89 ret = imx_audmux_v2_configure_port(ext_port, 90 IMX_AUDMUX_V2_PTCR_SYN, 91 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)); 92 if (ret) { 93 dev_err(&pdev->dev, "audmux external port setup failed\n"); 94 return ret; 95 } 96 97 ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0); 98 codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0); 99 if (!ssi_np || !codec_np) { 100 dev_err(&pdev->dev, "phandle missing or invalid\n"); 101 ret = -EINVAL; 102 goto fail; 103 } 104 105 ssi_pdev = of_find_device_by_node(ssi_np); 106 if (!ssi_pdev) { 107 dev_dbg(&pdev->dev, "failed to find SSI platform device\n"); 108 ret = -EPROBE_DEFER; 109 goto fail; 110 } 111 put_device(&ssi_pdev->dev); 112 codec_dev = of_find_i2c_device_by_node(codec_np); 113 if (!codec_dev) { 114 dev_dbg(&pdev->dev, "failed to find codec platform device\n"); 115 ret = -EPROBE_DEFER; 116 goto fail; 117 } 118 119 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 120 if (!data) { 121 ret = -ENOMEM; 122 goto fail; 123 } 124 125 data->codec_clk = clk_get(&codec_dev->dev, NULL); 126 if (IS_ERR(data->codec_clk)) { 127 ret = PTR_ERR(data->codec_clk); 128 goto fail; 129 } 130 131 data->clk_frequency = clk_get_rate(data->codec_clk); 132 133 data->dai.name = "HiFi"; 134 data->dai.stream_name = "HiFi"; 135 data->dai.codec_dai_name = "sgtl5000"; 136 data->dai.codec_of_node = codec_np; 137 data->dai.cpu_of_node = ssi_np; 138 data->dai.platform_of_node = ssi_np; 139 data->dai.init = &imx_sgtl5000_dai_init; 140 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 141 SND_SOC_DAIFMT_CBM_CFM; 142 143 data->card.dev = &pdev->dev; 144 ret = snd_soc_of_parse_card_name(&data->card, "model"); 145 if (ret) 146 goto fail; 147 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing"); 148 if (ret) 149 goto fail; 150 data->card.num_links = 1; 151 data->card.owner = THIS_MODULE; 152 data->card.dai_link = &data->dai; 153 data->card.dapm_widgets = imx_sgtl5000_dapm_widgets; 154 data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets); 155 156 platform_set_drvdata(pdev, &data->card); 157 snd_soc_card_set_drvdata(&data->card, data); 158 159 ret = devm_snd_soc_register_card(&pdev->dev, &data->card); 160 if (ret) { 161 if (ret != -EPROBE_DEFER) 162 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", 163 ret); 164 goto fail; 165 } 166 167 of_node_put(ssi_np); 168 of_node_put(codec_np); 169 170 return 0; 171 172 fail: 173 if (data && !IS_ERR(data->codec_clk)) 174 clk_put(data->codec_clk); 175 of_node_put(ssi_np); 176 of_node_put(codec_np); 177 178 return ret; 179 } 180 181 static int imx_sgtl5000_remove(struct platform_device *pdev) 182 { 183 struct snd_soc_card *card = platform_get_drvdata(pdev); 184 struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(card); 185 186 clk_put(data->codec_clk); 187 188 return 0; 189 } 190 191 static const struct of_device_id imx_sgtl5000_dt_ids[] = { 192 { .compatible = "fsl,imx-audio-sgtl5000", }, 193 { /* sentinel */ } 194 }; 195 MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids); 196 197 static struct platform_driver imx_sgtl5000_driver = { 198 .driver = { 199 .name = "imx-sgtl5000", 200 .pm = &snd_soc_pm_ops, 201 .of_match_table = imx_sgtl5000_dt_ids, 202 }, 203 .probe = imx_sgtl5000_probe, 204 .remove = imx_sgtl5000_remove, 205 }; 206 module_platform_driver(imx_sgtl5000_driver); 207 208 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>"); 209 MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver"); 210 MODULE_LICENSE("GPL v2"); 211 MODULE_ALIAS("platform:imx-sgtl5000"); 212