Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/device.h> |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 21 | #include <linux/of.h> |
| 22 | #include <linux/of_device.h> |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 23 | #include <sound/core.h> |
| 24 | #include <sound/pcm.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/jack.h> |
| 27 | #include <sound/soc-dapm.h> |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 28 | |
| 29 | #include "../codecs/sgtl5000.h" |
| 30 | #include "mxs-saif.h" |
| 31 | |
| 32 | static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream, |
| 33 | struct snd_pcm_hw_params *params) |
| 34 | { |
| 35 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 36 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 37 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 38 | unsigned int rate = params_rate(params); |
| 39 | u32 dai_format, mclk; |
| 40 | int ret; |
| 41 | |
| 42 | /* sgtl5000 does not support 512*rate when in 96000 fs */ |
| 43 | switch (rate) { |
| 44 | case 96000: |
| 45 | mclk = 256 * rate; |
| 46 | break; |
| 47 | default: |
| 48 | mclk = 512 * rate; |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | /* Sgtl5000 sysclk should be >= 8MHz and <= 27M */ |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 53 | if (mclk < 8000000 || mclk > 27000000) { |
| 54 | dev_err(codec_dai->dev, "Invalid mclk frequency: %u.%03uMHz\n", |
| 55 | mclk / 1000000, mclk / 1000 % 1000); |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 56 | return -EINVAL; |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 57 | } |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 58 | |
| 59 | /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */ |
| 60 | ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0); |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 61 | if (ret) { |
| 62 | dev_err(codec_dai->dev, "Failed to set sysclk to %u.%03uMHz\n", |
| 63 | mclk / 1000000, mclk / 1000 % 1000); |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 64 | return ret; |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 65 | } |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 66 | |
| 67 | /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */ |
| 68 | ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0); |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 69 | if (ret) { |
| 70 | dev_err(cpu_dai->dev, "Failed to set sysclk to %u.%03uMHz\n", |
| 71 | mclk / 1000000, mclk / 1000 % 1000); |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 72 | return ret; |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 73 | } |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 74 | |
| 75 | /* set codec to slave mode */ |
| 76 | dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 77 | SND_SOC_DAIFMT_CBS_CFS; |
| 78 | |
| 79 | /* set codec DAI configuration */ |
| 80 | ret = snd_soc_dai_set_fmt(codec_dai, dai_format); |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 81 | if (ret) { |
| 82 | dev_err(codec_dai->dev, "Failed to set dai format to %08x\n", |
| 83 | dai_format); |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 84 | return ret; |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 85 | } |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 86 | |
| 87 | /* set cpu DAI configuration */ |
| 88 | ret = snd_soc_dai_set_fmt(cpu_dai, dai_format); |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 89 | if (ret) { |
| 90 | dev_err(cpu_dai->dev, "Failed to set dai format to %08x\n", |
| 91 | dai_format); |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 92 | return ret; |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 93 | } |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static struct snd_soc_ops mxs_sgtl5000_hifi_ops = { |
| 99 | .hw_params = mxs_sgtl5000_hw_params, |
| 100 | }; |
| 101 | |
| 102 | static struct snd_soc_dai_link mxs_sgtl5000_dai[] = { |
| 103 | { |
Dong Aisheng | 78a262c | 2011-08-22 00:02:46 +0800 | [diff] [blame] | 104 | .name = "HiFi Tx", |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 105 | .stream_name = "HiFi Playback", |
| 106 | .codec_dai_name = "sgtl5000", |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 107 | .ops = &mxs_sgtl5000_hifi_ops, |
Dong Aisheng | 78a262c | 2011-08-22 00:02:46 +0800 | [diff] [blame] | 108 | }, { |
| 109 | .name = "HiFi Rx", |
| 110 | .stream_name = "HiFi Capture", |
| 111 | .codec_dai_name = "sgtl5000", |
Dong Aisheng | 78a262c | 2011-08-22 00:02:46 +0800 | [diff] [blame] | 112 | .ops = &mxs_sgtl5000_hifi_ops, |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 113 | }, |
| 114 | }; |
| 115 | |
| 116 | static struct snd_soc_card mxs_sgtl5000 = { |
| 117 | .name = "mxs_sgtl5000", |
Lothar Waßmann | a5b6834 | 2011-12-28 20:06:21 +0800 | [diff] [blame] | 118 | .owner = THIS_MODULE, |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 119 | .dai_link = mxs_sgtl5000_dai, |
| 120 | .num_links = ARRAY_SIZE(mxs_sgtl5000_dai), |
| 121 | }; |
| 122 | |
Bill Pemberton | fd58273 | 2012-12-07 09:26:27 -0500 | [diff] [blame] | 123 | static int mxs_sgtl5000_probe_dt(struct platform_device *pdev) |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 124 | { |
| 125 | struct device_node *np = pdev->dev.of_node; |
| 126 | struct device_node *saif_np[2], *codec_np; |
Fabio Estevam | f8b24fc | 2013-04-24 13:23:14 -0300 | [diff] [blame] | 127 | int i; |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 128 | |
| 129 | if (!np) |
| 130 | return 1; /* no device tree */ |
| 131 | |
| 132 | saif_np[0] = of_parse_phandle(np, "saif-controllers", 0); |
| 133 | saif_np[1] = of_parse_phandle(np, "saif-controllers", 1); |
| 134 | codec_np = of_parse_phandle(np, "audio-codec", 0); |
| 135 | if (!saif_np[0] || !saif_np[1] || !codec_np) { |
| 136 | dev_err(&pdev->dev, "phandle missing or invalid\n"); |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
| 140 | for (i = 0; i < 2; i++) { |
| 141 | mxs_sgtl5000_dai[i].codec_name = NULL; |
| 142 | mxs_sgtl5000_dai[i].codec_of_node = codec_np; |
| 143 | mxs_sgtl5000_dai[i].cpu_dai_name = NULL; |
Stephen Warren | bc92657 | 2012-05-25 18:22:11 -0600 | [diff] [blame] | 144 | mxs_sgtl5000_dai[i].cpu_of_node = saif_np[i]; |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 145 | mxs_sgtl5000_dai[i].platform_name = NULL; |
| 146 | mxs_sgtl5000_dai[i].platform_of_node = saif_np[i]; |
| 147 | } |
| 148 | |
| 149 | of_node_put(codec_np); |
| 150 | of_node_put(saif_np[0]); |
| 151 | of_node_put(saif_np[1]); |
| 152 | |
Fabio Estevam | f8b24fc | 2013-04-24 13:23:14 -0300 | [diff] [blame] | 153 | return 0; |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Bill Pemberton | fd58273 | 2012-12-07 09:26:27 -0500 | [diff] [blame] | 156 | static int mxs_sgtl5000_probe(struct platform_device *pdev) |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 157 | { |
| 158 | struct snd_soc_card *card = &mxs_sgtl5000; |
| 159 | int ret; |
| 160 | |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 161 | ret = mxs_sgtl5000_probe_dt(pdev); |
| 162 | if (ret < 0) |
| 163 | return ret; |
| 164 | |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 165 | /* |
| 166 | * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w). |
| 167 | * The Sgtl5000 sysclk is derived from saif0 mclk and it's range |
| 168 | * should be >= 8MHz and <= 27M. |
| 169 | */ |
| 170 | ret = mxs_saif_get_mclk(0, 44100 * 256, 44100); |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 171 | if (ret) { |
| 172 | dev_err(&pdev->dev, "failed to get mclk\n"); |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 173 | return ret; |
Lothar Waßmann | d66a5b9 | 2013-08-02 10:30:15 +0200 | [diff] [blame^] | 174 | } |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 175 | |
| 176 | card->dev = &pdev->dev; |
| 177 | platform_set_drvdata(pdev, card); |
| 178 | |
| 179 | ret = snd_soc_register_card(card); |
| 180 | if (ret) { |
| 181 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", |
| 182 | ret); |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Bill Pemberton | fd58273 | 2012-12-07 09:26:27 -0500 | [diff] [blame] | 189 | static int mxs_sgtl5000_remove(struct platform_device *pdev) |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 190 | { |
| 191 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 192 | |
| 193 | mxs_saif_put_mclk(0); |
| 194 | |
| 195 | snd_soc_unregister_card(card); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 200 | static const struct of_device_id mxs_sgtl5000_dt_ids[] = { |
| 201 | { .compatible = "fsl,mxs-audio-sgtl5000", }, |
| 202 | { /* sentinel */ } |
| 203 | }; |
| 204 | MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids); |
| 205 | |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 206 | static struct platform_driver mxs_sgtl5000_audio_driver = { |
| 207 | .driver = { |
| 208 | .name = "mxs-sgtl5000", |
| 209 | .owner = THIS_MODULE, |
Shawn Guo | e968194 | 2012-05-11 22:24:18 +0800 | [diff] [blame] | 210 | .of_match_table = mxs_sgtl5000_dt_ids, |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 211 | }, |
| 212 | .probe = mxs_sgtl5000_probe, |
Bill Pemberton | fd58273 | 2012-12-07 09:26:27 -0500 | [diff] [blame] | 213 | .remove = mxs_sgtl5000_remove, |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 214 | }; |
| 215 | |
Axel Lin | 85aa096 | 2011-11-24 14:21:29 +0800 | [diff] [blame] | 216 | module_platform_driver(mxs_sgtl5000_audio_driver); |
Dong Aisheng | fcb5e47 | 2011-07-21 12:36:57 +0800 | [diff] [blame] | 217 | |
| 218 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 219 | MODULE_DESCRIPTION("MXS ALSA SoC Machine driver"); |
| 220 | MODULE_LICENSE("GPL"); |
Lothar Waßmann | 9fd369b | 2011-12-09 14:38:12 +0100 | [diff] [blame] | 221 | MODULE_ALIAS("platform:mxs-sgtl5000"); |