blob: aa24d0d7c2e816f6af240d932ac5f119d2b94c93 [file] [log] [blame]
Thomas Gleixner16216332019-05-19 15:51:31 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Dong Aishengfcb5e472011-07-21 12:36:57 +08002/*
3 * Copyright 2011 Freescale Semiconductor, Inc.
Dong Aishengfcb5e472011-07-21 12:36:57 +08004 */
5
6#include <linux/module.h>
7#include <linux/device.h>
Shawn Guoe9681942012-05-11 22:24:18 +08008#include <linux/of.h>
9#include <linux/of_device.h>
Dong Aishengfcb5e472011-07-21 12:36:57 +080010#include <sound/core.h>
11#include <sound/pcm.h>
12#include <sound/soc.h>
13#include <sound/jack.h>
14#include <sound/soc-dapm.h>
Dong Aishengfcb5e472011-07-21 12:36:57 +080015
16#include "../codecs/sgtl5000.h"
17#include "mxs-saif.h"
18
19static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream,
20 struct snd_pcm_hw_params *params)
21{
22 struct snd_soc_pcm_runtime *rtd = substream->private_data;
23 struct snd_soc_dai *codec_dai = rtd->codec_dai;
24 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
25 unsigned int rate = params_rate(params);
Lars-Peter Clausenbc6a5d62015-01-01 17:16:15 +010026 u32 mclk;
Dong Aishengfcb5e472011-07-21 12:36:57 +080027 int ret;
28
29 /* sgtl5000 does not support 512*rate when in 96000 fs */
30 switch (rate) {
31 case 96000:
32 mclk = 256 * rate;
33 break;
34 default:
35 mclk = 512 * rate;
36 break;
37 }
38
Dong Aishengfcb5e472011-07-21 12:36:57 +080039 /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
40 ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
Lothar Waßmannd66a5b92013-08-02 10:30:15 +020041 if (ret) {
42 dev_err(codec_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
43 mclk / 1000000, mclk / 1000 % 1000);
Dong Aishengfcb5e472011-07-21 12:36:57 +080044 return ret;
Lothar Waßmannd66a5b92013-08-02 10:30:15 +020045 }
Dong Aishengfcb5e472011-07-21 12:36:57 +080046
47 /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
48 ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
Lothar Waßmannd66a5b92013-08-02 10:30:15 +020049 if (ret) {
50 dev_err(cpu_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
51 mclk / 1000000, mclk / 1000 % 1000);
Dong Aishengfcb5e472011-07-21 12:36:57 +080052 return ret;
Lothar Waßmannd66a5b92013-08-02 10:30:15 +020053 }
Dong Aishengfcb5e472011-07-21 12:36:57 +080054
Dong Aishengfcb5e472011-07-21 12:36:57 +080055 return 0;
56}
57
Julia Lawall9b6fdef2016-10-15 16:55:49 +020058static const struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
Dong Aishengfcb5e472011-07-21 12:36:57 +080059 .hw_params = mxs_sgtl5000_hw_params,
60};
61
Lars-Peter Clausenbc6a5d62015-01-01 17:16:15 +010062#define MXS_SGTL5000_DAI_FMT (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | \
63 SND_SOC_DAIFMT_CBS_CFS)
64
Kuninori Morimoto49cb4482019-06-06 13:16:37 +090065
66SND_SOC_DAILINK_DEFS(hifi_tx,
67 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto5f922292019-06-19 10:17:18 +090068 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")));
Kuninori Morimoto49cb4482019-06-06 13:16:37 +090069
70SND_SOC_DAILINK_DEFS(hifi_rx,
71 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto5f922292019-06-19 10:17:18 +090072 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")));
Kuninori Morimoto49cb4482019-06-06 13:16:37 +090073
Dong Aishengfcb5e472011-07-21 12:36:57 +080074static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
75 {
Dong Aisheng78a262c2011-08-22 00:02:46 +080076 .name = "HiFi Tx",
Dong Aishengfcb5e472011-07-21 12:36:57 +080077 .stream_name = "HiFi Playback",
Lars-Peter Clausenbc6a5d62015-01-01 17:16:15 +010078 .dai_fmt = MXS_SGTL5000_DAI_FMT,
Dong Aishengfcb5e472011-07-21 12:36:57 +080079 .ops = &mxs_sgtl5000_hifi_ops,
Fabio Estevama90e6052013-08-29 10:32:14 -030080 .playback_only = true,
Kuninori Morimoto49cb4482019-06-06 13:16:37 +090081 SND_SOC_DAILINK_REG(hifi_tx),
Dong Aisheng78a262c2011-08-22 00:02:46 +080082 }, {
83 .name = "HiFi Rx",
84 .stream_name = "HiFi Capture",
Lars-Peter Clausenbc6a5d62015-01-01 17:16:15 +010085 .dai_fmt = MXS_SGTL5000_DAI_FMT,
Dong Aisheng78a262c2011-08-22 00:02:46 +080086 .ops = &mxs_sgtl5000_hifi_ops,
Fabio Estevama90e6052013-08-29 10:32:14 -030087 .capture_only = true,
Kuninori Morimoto49cb4482019-06-06 13:16:37 +090088 SND_SOC_DAILINK_REG(hifi_rx),
Dong Aishengfcb5e472011-07-21 12:36:57 +080089 },
90};
91
Christian Fischer949293d2018-01-12 14:22:59 +010092static const struct snd_soc_dapm_widget mxs_sgtl5000_dapm_widgets[] = {
93 SND_SOC_DAPM_MIC("Mic Jack", NULL),
94 SND_SOC_DAPM_LINE("Line In Jack", NULL),
95 SND_SOC_DAPM_HP("Headphone Jack", NULL),
96 SND_SOC_DAPM_SPK("Line Out Jack", NULL),
97 SND_SOC_DAPM_SPK("Ext Spk", NULL),
98};
99
Dong Aishengfcb5e472011-07-21 12:36:57 +0800100static struct snd_soc_card mxs_sgtl5000 = {
101 .name = "mxs_sgtl5000",
Lothar Waßmanna5b68342011-12-28 20:06:21 +0800102 .owner = THIS_MODULE,
Dong Aishengfcb5e472011-07-21 12:36:57 +0800103 .dai_link = mxs_sgtl5000_dai,
104 .num_links = ARRAY_SIZE(mxs_sgtl5000_dai),
105};
106
Fabio Estevam38f2b8c2013-09-02 17:56:18 -0300107static int mxs_sgtl5000_probe(struct platform_device *pdev)
Shawn Guoe9681942012-05-11 22:24:18 +0800108{
Fabio Estevam38f2b8c2013-09-02 17:56:18 -0300109 struct snd_soc_card *card = &mxs_sgtl5000;
110 int ret, i;
Shawn Guoe9681942012-05-11 22:24:18 +0800111 struct device_node *np = pdev->dev.of_node;
112 struct device_node *saif_np[2], *codec_np;
Shawn Guoe9681942012-05-11 22:24:18 +0800113
114 saif_np[0] = of_parse_phandle(np, "saif-controllers", 0);
115 saif_np[1] = of_parse_phandle(np, "saif-controllers", 1);
116 codec_np = of_parse_phandle(np, "audio-codec", 0);
117 if (!saif_np[0] || !saif_np[1] || !codec_np) {
118 dev_err(&pdev->dev, "phandle missing or invalid\n");
119 return -EINVAL;
120 }
121
122 for (i = 0; i < 2; i++) {
Kuninori Morimoto49cb4482019-06-06 13:16:37 +0900123 mxs_sgtl5000_dai[i].codecs->name = NULL;
124 mxs_sgtl5000_dai[i].codecs->of_node = codec_np;
125 mxs_sgtl5000_dai[i].cpus->dai_name = NULL;
126 mxs_sgtl5000_dai[i].cpus->of_node = saif_np[i];
Shawn Guoe9681942012-05-11 22:24:18 +0800127 }
128
129 of_node_put(codec_np);
130 of_node_put(saif_np[0]);
131 of_node_put(saif_np[1]);
132
Dong Aishengfcb5e472011-07-21 12:36:57 +0800133 /*
134 * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
135 * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
136 * should be >= 8MHz and <= 27M.
137 */
138 ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
Lothar Waßmannd66a5b92013-08-02 10:30:15 +0200139 if (ret) {
140 dev_err(&pdev->dev, "failed to get mclk\n");
Dong Aishengfcb5e472011-07-21 12:36:57 +0800141 return ret;
Lothar Waßmannd66a5b92013-08-02 10:30:15 +0200142 }
Dong Aishengfcb5e472011-07-21 12:36:57 +0800143
144 card->dev = &pdev->dev;
Dong Aishengfcb5e472011-07-21 12:36:57 +0800145
Christian Fischer949293d2018-01-12 14:22:59 +0100146 if (of_find_property(np, "audio-routing", NULL)) {
147 card->dapm_widgets = mxs_sgtl5000_dapm_widgets;
148 card->num_dapm_widgets = ARRAY_SIZE(mxs_sgtl5000_dapm_widgets);
149
150 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
151 if (ret) {
152 dev_err(&pdev->dev, "failed to parse audio-routing (%d)\n",
153 ret);
154 return ret;
155 }
156 }
157
Axel Lin5b73de12015-08-30 17:20:01 +0800158 ret = devm_snd_soc_register_card(&pdev->dev, card);
Dong Aishengfcb5e472011-07-21 12:36:57 +0800159 if (ret) {
Fabio Estevamd04c4132018-01-17 13:48:55 -0200160 if (ret != -EPROBE_DEFER)
161 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
162 ret);
Dong Aishengfcb5e472011-07-21 12:36:57 +0800163 return ret;
164 }
165
166 return 0;
167}
168
Bill Pembertonfd582732012-12-07 09:26:27 -0500169static int mxs_sgtl5000_remove(struct platform_device *pdev)
Dong Aishengfcb5e472011-07-21 12:36:57 +0800170{
Dong Aishengfcb5e472011-07-21 12:36:57 +0800171 mxs_saif_put_mclk(0);
172
Dong Aishengfcb5e472011-07-21 12:36:57 +0800173 return 0;
174}
175
Shawn Guoe9681942012-05-11 22:24:18 +0800176static const struct of_device_id mxs_sgtl5000_dt_ids[] = {
177 { .compatible = "fsl,mxs-audio-sgtl5000", },
178 { /* sentinel */ }
179};
180MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids);
181
Dong Aishengfcb5e472011-07-21 12:36:57 +0800182static struct platform_driver mxs_sgtl5000_audio_driver = {
183 .driver = {
184 .name = "mxs-sgtl5000",
Shawn Guoe9681942012-05-11 22:24:18 +0800185 .of_match_table = mxs_sgtl5000_dt_ids,
Dong Aishengfcb5e472011-07-21 12:36:57 +0800186 },
187 .probe = mxs_sgtl5000_probe,
Bill Pembertonfd582732012-12-07 09:26:27 -0500188 .remove = mxs_sgtl5000_remove,
Dong Aishengfcb5e472011-07-21 12:36:57 +0800189};
190
Axel Lin85aa0962011-11-24 14:21:29 +0800191module_platform_driver(mxs_sgtl5000_audio_driver);
Dong Aishengfcb5e472011-07-21 12:36:57 +0800192
193MODULE_AUTHOR("Freescale Semiconductor, Inc.");
194MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
195MODULE_LICENSE("GPL");
Lothar Waßmann9fd369b2011-12-09 14:38:12 +0100196MODULE_ALIAS("platform:mxs-sgtl5000");