Fabio Estevam | 6b24e03 | 2018-07-24 09:48:33 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // Copyright 2012 Freescale Semiconductor, Inc. |
| 4 | // Copyright 2012 Linaro Ltd. |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 5 | |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/of.h> |
| 8 | #include <linux/of_platform.h> |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 9 | #include <linux/i2c.h> |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 10 | #include <linux/clk.h> |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 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]; |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 23 | struct clk *codec_clk; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 24 | unsigned int clk_frequency; |
| 25 | }; |
| 26 | |
| 27 | static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 28 | { |
Shawn Guo | 47cf84e | 2014-02-08 13:20:35 +0800 | [diff] [blame] | 29 | struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(rtd->card); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 30 | struct device *dev = rtd->card->dev; |
| 31 | int ret; |
| 32 | |
Kuninori Morimoto | 17198ae | 2020-03-23 14:18:30 +0900 | [diff] [blame] | 33 | ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0), SGTL5000_SYSCLK, |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 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 | |
Shawn Guo | 172b4c5c | 2012-03-30 00:13:03 +0800 | [diff] [blame] | 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 | |
Bill Pemberton | a0a3d51 | 2012-12-07 09:26:16 -0500 | [diff] [blame] | 51 | static int imx_sgtl5000_probe(struct platform_device *pdev) |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 52 | { |
| 53 | struct device_node *np = pdev->dev.of_node; |
| 54 | struct device_node *ssi_np, *codec_np; |
| 55 | struct platform_device *ssi_pdev; |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 56 | struct i2c_client *codec_dev; |
Philipp Zabel | 50d4a79 | 2013-09-25 15:22:01 +0200 | [diff] [blame] | 57 | struct imx_sgtl5000_data *data = NULL; |
Kuninori Morimoto | 3026ef6 | 2019-06-06 13:16:02 +0900 | [diff] [blame] | 58 | struct snd_soc_dai_link_component *comp; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 59 | int int_port, ext_port; |
| 60 | int ret; |
| 61 | |
| 62 | ret = of_property_read_u32(np, "mux-int-port", &int_port); |
| 63 | if (ret) { |
| 64 | dev_err(&pdev->dev, "mux-int-port missing or invalid\n"); |
| 65 | return ret; |
| 66 | } |
| 67 | ret = of_property_read_u32(np, "mux-ext-port", &ext_port); |
| 68 | if (ret) { |
| 69 | dev_err(&pdev->dev, "mux-ext-port missing or invalid\n"); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * The port numbering in the hardware manual starts at 1, while |
| 75 | * the audmux API expects it starts at 0. |
| 76 | */ |
| 77 | int_port--; |
| 78 | ext_port--; |
| 79 | ret = imx_audmux_v2_configure_port(int_port, |
| 80 | IMX_AUDMUX_V2_PTCR_SYN | |
| 81 | IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) | |
| 82 | IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) | |
| 83 | IMX_AUDMUX_V2_PTCR_TFSDIR | |
| 84 | IMX_AUDMUX_V2_PTCR_TCLKDIR, |
| 85 | IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)); |
| 86 | if (ret) { |
| 87 | dev_err(&pdev->dev, "audmux internal port setup failed\n"); |
| 88 | return ret; |
| 89 | } |
Julia Lawall | db8b624 | 2012-08-19 09:02:52 +0200 | [diff] [blame] | 90 | ret = imx_audmux_v2_configure_port(ext_port, |
Hui Wang | ef3207c | 2012-07-04 15:38:27 +0800 | [diff] [blame] | 91 | IMX_AUDMUX_V2_PTCR_SYN, |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 92 | IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)); |
| 93 | if (ret) { |
| 94 | dev_err(&pdev->dev, "audmux external port setup failed\n"); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0); |
| 99 | codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0); |
| 100 | if (!ssi_np || !codec_np) { |
| 101 | dev_err(&pdev->dev, "phandle missing or invalid\n"); |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 102 | ret = -EINVAL; |
| 103 | goto fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ssi_pdev = of_find_device_by_node(ssi_np); |
| 107 | if (!ssi_pdev) { |
Stefan Agner | 691beb0 | 2019-01-18 10:06:53 +0100 | [diff] [blame] | 108 | dev_dbg(&pdev->dev, "failed to find SSI platform device\n"); |
Arnaud Patard (Rtp) | 28e5ca7 | 2013-06-20 23:20:49 +0200 | [diff] [blame] | 109 | ret = -EPROBE_DEFER; |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 110 | goto fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 111 | } |
Wen Yang | 8fa857d | 2019-02-18 15:13:47 +0000 | [diff] [blame] | 112 | put_device(&ssi_pdev->dev); |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 113 | codec_dev = of_find_i2c_device_by_node(codec_np); |
| 114 | if (!codec_dev) { |
Stefan Agner | 691beb0 | 2019-01-18 10:06:53 +0100 | [diff] [blame] | 115 | dev_dbg(&pdev->dev, "failed to find codec platform device\n"); |
Stefan Agner | d986657 | 2019-01-18 10:06:52 +0100 | [diff] [blame] | 116 | ret = -EPROBE_DEFER; |
| 117 | goto fail; |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 118 | } |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 119 | |
| 120 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 121 | if (!data) { |
| 122 | ret = -ENOMEM; |
| 123 | goto fail; |
| 124 | } |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 125 | |
Kuninori Morimoto | 2b1407c | 2019-06-28 10:47:26 +0900 | [diff] [blame] | 126 | comp = devm_kzalloc(&pdev->dev, 3 * sizeof(*comp), GFP_KERNEL); |
Kuninori Morimoto | 3026ef6 | 2019-06-06 13:16:02 +0900 | [diff] [blame] | 127 | if (!comp) { |
| 128 | ret = -ENOMEM; |
| 129 | goto fail; |
| 130 | } |
| 131 | |
Philipp Zabel | a8b22c1 | 2013-09-25 15:21:08 +0200 | [diff] [blame] | 132 | data->codec_clk = clk_get(&codec_dev->dev, NULL); |
Wei Yongjun | f1269ae | 2013-07-16 20:05:07 +0800 | [diff] [blame] | 133 | if (IS_ERR(data->codec_clk)) { |
| 134 | ret = PTR_ERR(data->codec_clk); |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 135 | goto fail; |
Wei Yongjun | f1269ae | 2013-07-16 20:05:07 +0800 | [diff] [blame] | 136 | } |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 137 | |
| 138 | data->clk_frequency = clk_get_rate(data->codec_clk); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 139 | |
Kuninori Morimoto | 3026ef6 | 2019-06-06 13:16:02 +0900 | [diff] [blame] | 140 | data->dai.cpus = &comp[0]; |
| 141 | data->dai.codecs = &comp[1]; |
Kuninori Morimoto | 2b1407c | 2019-06-28 10:47:26 +0900 | [diff] [blame] | 142 | data->dai.platforms = &comp[2]; |
Kuninori Morimoto | 3026ef6 | 2019-06-06 13:16:02 +0900 | [diff] [blame] | 143 | |
| 144 | data->dai.num_cpus = 1; |
| 145 | data->dai.num_codecs = 1; |
Kuninori Morimoto | 2b1407c | 2019-06-28 10:47:26 +0900 | [diff] [blame] | 146 | data->dai.num_platforms = 1; |
Kuninori Morimoto | 3026ef6 | 2019-06-06 13:16:02 +0900 | [diff] [blame] | 147 | |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 148 | data->dai.name = "HiFi"; |
| 149 | data->dai.stream_name = "HiFi"; |
Kuninori Morimoto | 3026ef6 | 2019-06-06 13:16:02 +0900 | [diff] [blame] | 150 | data->dai.codecs->dai_name = "sgtl5000"; |
| 151 | data->dai.codecs->of_node = codec_np; |
| 152 | data->dai.cpus->of_node = ssi_np; |
Kuninori Morimoto | 2b1407c | 2019-06-28 10:47:26 +0900 | [diff] [blame] | 153 | data->dai.platforms->of_node = ssi_np; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 154 | data->dai.init = &imx_sgtl5000_dai_init; |
| 155 | data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
Mark Brown | 419099b | 2021-09-21 22:35:39 +0100 | [diff] [blame] | 156 | SND_SOC_DAIFMT_CBP_CFP; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 157 | |
| 158 | data->card.dev = &pdev->dev; |
| 159 | ret = snd_soc_of_parse_card_name(&data->card, "model"); |
| 160 | if (ret) |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 161 | goto fail; |
Shawn Guo | 172b4c5c | 2012-03-30 00:13:03 +0800 | [diff] [blame] | 162 | ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing"); |
| 163 | if (ret) |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 164 | goto fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 165 | data->card.num_links = 1; |
Lothar Waßmann | 29df4306 | 2012-11-22 13:31:08 +0100 | [diff] [blame] | 166 | data->card.owner = THIS_MODULE; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 167 | data->card.dai_link = &data->dai; |
Shawn Guo | 172b4c5c | 2012-03-30 00:13:03 +0800 | [diff] [blame] | 168 | data->card.dapm_widgets = imx_sgtl5000_dapm_widgets; |
| 169 | data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 170 | |
Shawn Guo | 47cf84e | 2014-02-08 13:20:35 +0800 | [diff] [blame] | 171 | platform_set_drvdata(pdev, &data->card); |
| 172 | snd_soc_card_set_drvdata(&data->card, data); |
| 173 | |
Sachin Kamat | 01984a4 | 2013-09-17 09:42:46 +0530 | [diff] [blame] | 174 | ret = devm_snd_soc_register_card(&pdev->dev, &data->card); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 175 | if (ret) { |
Kuninori Morimoto | 2e6f557 | 2021-12-14 11:08:34 +0900 | [diff] [blame] | 176 | dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n"); |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 177 | goto fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Fabio Estevam | 2fc059f | 2013-04-24 11:54:43 -0300 | [diff] [blame] | 180 | of_node_put(ssi_np); |
| 181 | of_node_put(codec_np); |
| 182 | |
| 183 | return 0; |
| 184 | |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 185 | fail: |
Philipp Zabel | 50d4a79 | 2013-09-25 15:22:01 +0200 | [diff] [blame] | 186 | if (data && !IS_ERR(data->codec_clk)) |
Philipp Zabel | a8b22c1 | 2013-09-25 15:21:08 +0200 | [diff] [blame] | 187 | clk_put(data->codec_clk); |
Fabio Estevam | 7e3bc3c | 2014-10-07 15:13:25 -0300 | [diff] [blame] | 188 | of_node_put(ssi_np); |
| 189 | of_node_put(codec_np); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 190 | |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 191 | return ret; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Bill Pemberton | a0a3d51 | 2012-12-07 09:26:16 -0500 | [diff] [blame] | 194 | static int imx_sgtl5000_remove(struct platform_device *pdev) |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 195 | { |
Shawn Guo | 47cf84e | 2014-02-08 13:20:35 +0800 | [diff] [blame] | 196 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 197 | struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(card); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 198 | |
Philipp Zabel | a8b22c1 | 2013-09-25 15:21:08 +0200 | [diff] [blame] | 199 | clk_put(data->codec_clk); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static const struct of_device_id imx_sgtl5000_dt_ids[] = { |
| 205 | { .compatible = "fsl,imx-audio-sgtl5000", }, |
| 206 | { /* sentinel */ } |
| 207 | }; |
| 208 | MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids); |
| 209 | |
| 210 | static struct platform_driver imx_sgtl5000_driver = { |
| 211 | .driver = { |
| 212 | .name = "imx-sgtl5000", |
Nicolin Chen | 1abe729 | 2013-10-24 18:15:29 +0800 | [diff] [blame] | 213 | .pm = &snd_soc_pm_ops, |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 214 | .of_match_table = imx_sgtl5000_dt_ids, |
| 215 | }, |
| 216 | .probe = imx_sgtl5000_probe, |
Fabio Estevam | 4fa8dbc | 2013-09-30 22:17:37 -0300 | [diff] [blame] | 217 | .remove = imx_sgtl5000_remove, |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 218 | }; |
| 219 | module_platform_driver(imx_sgtl5000_driver); |
| 220 | |
| 221 | MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>"); |
| 222 | MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver"); |
| 223 | MODULE_LICENSE("GPL v2"); |
| 224 | MODULE_ALIAS("platform:imx-sgtl5000"); |