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> |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 16 | #include <linux/of_i2c.h> |
| 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 | { |
| 36 | struct imx_sgtl5000_data *data = container_of(rtd->card, |
| 37 | struct imx_sgtl5000_data, card); |
| 38 | struct device *dev = rtd->card->dev; |
| 39 | int ret; |
| 40 | |
| 41 | ret = snd_soc_dai_set_sysclk(rtd->codec_dai, SGTL5000_SYSCLK, |
| 42 | data->clk_frequency, SND_SOC_CLOCK_IN); |
| 43 | if (ret) { |
| 44 | dev_err(dev, "could not set codec driver clock params\n"); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
Shawn Guo | 172b4c5c | 2012-03-30 00:13:03 +0800 | [diff] [blame] | 51 | static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets[] = { |
| 52 | SND_SOC_DAPM_MIC("Mic Jack", NULL), |
| 53 | SND_SOC_DAPM_LINE("Line In Jack", NULL), |
| 54 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 55 | SND_SOC_DAPM_SPK("Line Out Jack", NULL), |
| 56 | SND_SOC_DAPM_SPK("Ext Spk", NULL), |
| 57 | }; |
| 58 | |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 59 | static int __devinit imx_sgtl5000_probe(struct platform_device *pdev) |
| 60 | { |
| 61 | struct device_node *np = pdev->dev.of_node; |
| 62 | struct device_node *ssi_np, *codec_np; |
| 63 | struct platform_device *ssi_pdev; |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 64 | struct i2c_client *codec_dev; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 65 | struct imx_sgtl5000_data *data; |
| 66 | int int_port, ext_port; |
| 67 | int ret; |
| 68 | |
| 69 | ret = of_property_read_u32(np, "mux-int-port", &int_port); |
| 70 | if (ret) { |
| 71 | dev_err(&pdev->dev, "mux-int-port missing or invalid\n"); |
| 72 | return ret; |
| 73 | } |
| 74 | ret = of_property_read_u32(np, "mux-ext-port", &ext_port); |
| 75 | if (ret) { |
| 76 | dev_err(&pdev->dev, "mux-ext-port missing or invalid\n"); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * The port numbering in the hardware manual starts at 1, while |
| 82 | * the audmux API expects it starts at 0. |
| 83 | */ |
| 84 | int_port--; |
| 85 | ext_port--; |
| 86 | ret = imx_audmux_v2_configure_port(int_port, |
| 87 | IMX_AUDMUX_V2_PTCR_SYN | |
| 88 | IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) | |
| 89 | IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) | |
| 90 | IMX_AUDMUX_V2_PTCR_TFSDIR | |
| 91 | IMX_AUDMUX_V2_PTCR_TCLKDIR, |
| 92 | IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)); |
| 93 | if (ret) { |
| 94 | dev_err(&pdev->dev, "audmux internal port setup failed\n"); |
| 95 | return ret; |
| 96 | } |
Julia Lawall | db8b624 | 2012-08-19 09:02:52 +0200 | [diff] [blame] | 97 | ret = imx_audmux_v2_configure_port(ext_port, |
Hui Wang | ef3207c | 2012-07-04 15:38:27 +0800 | [diff] [blame] | 98 | IMX_AUDMUX_V2_PTCR_SYN, |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 99 | IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)); |
| 100 | if (ret) { |
| 101 | dev_err(&pdev->dev, "audmux external port setup failed\n"); |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0); |
| 106 | codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0); |
| 107 | if (!ssi_np || !codec_np) { |
| 108 | dev_err(&pdev->dev, "phandle missing or invalid\n"); |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 109 | ret = -EINVAL; |
| 110 | goto fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | ssi_pdev = of_find_device_by_node(ssi_np); |
| 114 | if (!ssi_pdev) { |
| 115 | dev_err(&pdev->dev, "failed to find SSI platform device\n"); |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 116 | ret = -EINVAL; |
| 117 | goto fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 118 | } |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 119 | codec_dev = of_find_i2c_device_by_node(codec_np); |
| 120 | if (!codec_dev) { |
| 121 | dev_err(&pdev->dev, "failed to find codec platform device\n"); |
| 122 | return -EINVAL; |
| 123 | } |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 124 | |
| 125 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 126 | if (!data) { |
| 127 | ret = -ENOMEM; |
| 128 | goto fail; |
| 129 | } |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 130 | |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 131 | data->codec_clk = clk_get(&codec_dev->dev, NULL); |
| 132 | if (IS_ERR(data->codec_clk)) { |
| 133 | /* assuming clock enabled by default */ |
| 134 | data->codec_clk = NULL; |
| 135 | ret = of_property_read_u32(codec_np, "clock-frequency", |
| 136 | &data->clk_frequency); |
| 137 | if (ret) { |
| 138 | dev_err(&codec_dev->dev, |
| 139 | "clock-frequency missing or invalid\n"); |
| 140 | goto fail; |
| 141 | } |
| 142 | } else { |
| 143 | data->clk_frequency = clk_get_rate(data->codec_clk); |
| 144 | clk_prepare_enable(data->codec_clk); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | data->dai.name = "HiFi"; |
| 148 | data->dai.stream_name = "HiFi"; |
| 149 | data->dai.codec_dai_name = "sgtl5000"; |
| 150 | data->dai.codec_of_node = codec_np; |
| 151 | data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev); |
| 152 | data->dai.platform_name = "imx-pcm-audio"; |
| 153 | data->dai.init = &imx_sgtl5000_dai_init; |
| 154 | data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 155 | SND_SOC_DAIFMT_CBM_CFM; |
| 156 | |
| 157 | data->card.dev = &pdev->dev; |
| 158 | ret = snd_soc_of_parse_card_name(&data->card, "model"); |
| 159 | if (ret) |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 160 | goto clk_fail; |
Shawn Guo | 172b4c5c | 2012-03-30 00:13:03 +0800 | [diff] [blame] | 161 | ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing"); |
| 162 | if (ret) |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 163 | goto clk_fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 164 | data->card.num_links = 1; |
Lothar Waßmann | 29df4306 | 2012-11-22 13:31:08 +0100 | [diff] [blame] | 165 | data->card.owner = THIS_MODULE; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 166 | data->card.dai_link = &data->dai; |
Shawn Guo | 172b4c5c | 2012-03-30 00:13:03 +0800 | [diff] [blame] | 167 | data->card.dapm_widgets = imx_sgtl5000_dapm_widgets; |
| 168 | data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 169 | |
| 170 | ret = snd_soc_register_card(&data->card); |
| 171 | if (ret) { |
| 172 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret); |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 173 | goto clk_fail; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | platform_set_drvdata(pdev, data); |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 177 | clk_fail: |
| 178 | clk_put(data->codec_clk); |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 179 | fail: |
| 180 | if (ssi_np) |
| 181 | of_node_put(ssi_np); |
| 182 | if (codec_np) |
| 183 | of_node_put(codec_np); |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 184 | |
Richard Zhao | 717071d | 2012-04-27 15:02:56 +0800 | [diff] [blame] | 185 | return ret; |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static int __devexit imx_sgtl5000_remove(struct platform_device *pdev) |
| 189 | { |
| 190 | struct imx_sgtl5000_data *data = platform_get_drvdata(pdev); |
| 191 | |
Richard Zhao | 81e8e49 | 2012-04-27 15:02:57 +0800 | [diff] [blame] | 192 | if (data->codec_clk) { |
| 193 | clk_disable_unprepare(data->codec_clk); |
| 194 | clk_put(data->codec_clk); |
| 195 | } |
Shawn Guo | c448303 | 2012-03-16 16:56:44 +0800 | [diff] [blame] | 196 | snd_soc_unregister_card(&data->card); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static const struct of_device_id imx_sgtl5000_dt_ids[] = { |
| 202 | { .compatible = "fsl,imx-audio-sgtl5000", }, |
| 203 | { /* sentinel */ } |
| 204 | }; |
| 205 | MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids); |
| 206 | |
| 207 | static struct platform_driver imx_sgtl5000_driver = { |
| 208 | .driver = { |
| 209 | .name = "imx-sgtl5000", |
| 210 | .owner = THIS_MODULE, |
| 211 | .of_match_table = imx_sgtl5000_dt_ids, |
| 212 | }, |
| 213 | .probe = imx_sgtl5000_probe, |
| 214 | .remove = __devexit_p(imx_sgtl5000_remove), |
| 215 | }; |
| 216 | module_platform_driver(imx_sgtl5000_driver); |
| 217 | |
| 218 | MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>"); |
| 219 | MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver"); |
| 220 | MODULE_LICENSE("GPL v2"); |
| 221 | MODULE_ALIAS("platform:imx-sgtl5000"); |