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