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