1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2012
5 * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
6 * Kristoffer Karlsson <kristoffer.karlsson@stericsson.com>
10 #include <linux/module.h>
11 #include <linux/device.h>
13 #include <linux/clk.h>
14 #include <linux/mutex.h>
16 #include <sound/soc.h>
17 #include <sound/soc-dapm.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
21 #include "ux500_pcm.h"
22 #include "ux500_msp_dai.h"
23 #include "mop500_ab8500.h"
24 #include "../codecs/ab8500-codec.h"
26 #define TX_SLOT_MONO 0x0008
27 #define TX_SLOT_STEREO 0x000a
28 #define RX_SLOT_MONO 0x0001
29 #define RX_SLOT_STEREO 0x0003
30 #define TX_SLOT_8CH 0x00FF
31 #define RX_SLOT_8CH 0x00FF
33 #define DEF_TX_SLOTS TX_SLOT_STEREO
34 #define DEF_RX_SLOTS RX_SLOT_MONO
36 #define DRIVERMODE_NORMAL 0
37 #define DRIVERMODE_CODEC_ONLY 1
39 /* Slot configuration */
40 static unsigned int tx_slots = DEF_TX_SLOTS;
41 static unsigned int rx_slots = DEF_RX_SLOTS;
43 /* Configuration consistency parameters */
44 static DEFINE_MUTEX(mop500_ab8500_params_lock);
45 static unsigned long mop500_ab8500_usage;
46 static int mop500_ab8500_rate;
47 static int mop500_ab8500_channels;
50 static const char * const enum_mclk[] = {
59 static SOC_ENUM_SINGLE_EXT_DECL(soc_enum_mclk, enum_mclk);
61 /* Private data for machine-part MOP500<->AB8500 */
62 struct mop500_ab8500_drvdata {
65 struct clk *clk_ptr_intclk;
66 struct clk *clk_ptr_sysclk;
67 struct clk *clk_ptr_ulpclk;
70 static inline const char *get_mclk_str(enum mclk mclk_sel)
82 static int mop500_ab8500_set_mclk(struct device *dev,
83 struct mop500_ab8500_drvdata *drvdata)
88 if (IS_ERR(drvdata->clk_ptr_intclk)) {
90 "%s: ERROR: intclk not initialized!\n", __func__);
94 switch (drvdata->mclk_sel) {
96 clk_ptr = drvdata->clk_ptr_sysclk;
99 clk_ptr = drvdata->clk_ptr_ulpclk;
105 if (IS_ERR(clk_ptr)) {
106 dev_err(dev, "%s: ERROR: %s not initialized!\n", __func__,
107 get_mclk_str(drvdata->mclk_sel));
111 status = clk_set_parent(drvdata->clk_ptr_intclk, clk_ptr);
114 "%s: ERROR: Setting intclk parent to %s failed (ret = %d)!",
115 __func__, get_mclk_str(drvdata->mclk_sel), status);
118 "%s: intclk parent changed to %s.\n",
119 __func__, get_mclk_str(drvdata->mclk_sel));
128 static int mclk_input_control_get(struct snd_kcontrol *kcontrol,
129 struct snd_ctl_elem_value *ucontrol)
131 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
132 struct mop500_ab8500_drvdata *drvdata =
133 snd_soc_card_get_drvdata(card);
135 ucontrol->value.enumerated.item[0] = drvdata->mclk_sel;
140 static int mclk_input_control_put(struct snd_kcontrol *kcontrol,
141 struct snd_ctl_elem_value *ucontrol)
143 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
144 struct mop500_ab8500_drvdata *drvdata =
145 snd_soc_card_get_drvdata(card);
146 unsigned int val = ucontrol->value.enumerated.item[0];
148 if (val > (unsigned int)MCLK_ULPCLK)
150 if (drvdata->mclk_sel == val)
153 drvdata->mclk_sel = val;
162 static struct snd_kcontrol_new mop500_ab8500_ctrls[] = {
163 SOC_ENUM_EXT("Master Clock Select",
165 mclk_input_control_get, mclk_input_control_put),
166 SOC_DAPM_PIN_SWITCH("Headset Left"),
167 SOC_DAPM_PIN_SWITCH("Headset Right"),
168 SOC_DAPM_PIN_SWITCH("Earpiece"),
169 SOC_DAPM_PIN_SWITCH("Speaker Left"),
170 SOC_DAPM_PIN_SWITCH("Speaker Right"),
171 SOC_DAPM_PIN_SWITCH("LineOut Left"),
172 SOC_DAPM_PIN_SWITCH("LineOut Right"),
173 SOC_DAPM_PIN_SWITCH("Vibra 1"),
174 SOC_DAPM_PIN_SWITCH("Vibra 2"),
175 SOC_DAPM_PIN_SWITCH("Mic 1"),
176 SOC_DAPM_PIN_SWITCH("Mic 2"),
177 SOC_DAPM_PIN_SWITCH("LineIn Left"),
178 SOC_DAPM_PIN_SWITCH("LineIn Right"),
179 SOC_DAPM_PIN_SWITCH("DMic 1"),
180 SOC_DAPM_PIN_SWITCH("DMic 2"),
181 SOC_DAPM_PIN_SWITCH("DMic 3"),
182 SOC_DAPM_PIN_SWITCH("DMic 4"),
183 SOC_DAPM_PIN_SWITCH("DMic 5"),
184 SOC_DAPM_PIN_SWITCH("DMic 6"),
189 static int mop500_ab8500_startup(struct snd_pcm_substream *substream)
191 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
193 /* Set audio-clock source */
194 return mop500_ab8500_set_mclk(rtd->card->dev,
195 snd_soc_card_get_drvdata(rtd->card));
198 static void mop500_ab8500_shutdown(struct snd_pcm_substream *substream)
200 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
201 struct device *dev = rtd->card->dev;
203 dev_dbg(dev, "%s: Enter\n", __func__);
205 /* Reset slots configuration to default(s) */
206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
207 tx_slots = DEF_TX_SLOTS;
209 rx_slots = DEF_RX_SLOTS;
212 static int mop500_ab8500_hw_params(struct snd_pcm_substream *substream,
213 struct snd_pcm_hw_params *params)
215 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
216 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
217 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
218 struct device *dev = rtd->card->dev;
220 int channels, ret = 0, driver_mode, slots;
221 unsigned int sw_codec, sw_cpu;
224 dev_dbg(dev, "%s: Enter\n", __func__);
226 dev_dbg(dev, "%s: substream->pcm->name = %s\n"
227 "substream->pcm->id = %s.\n"
228 "substream->name = %s.\n"
229 "substream->number = %d.\n",
231 substream->pcm->name,
236 /* Ensure configuration consistency between DAIs */
237 mutex_lock(&mop500_ab8500_params_lock);
238 if (mop500_ab8500_usage) {
239 if (mop500_ab8500_rate != params_rate(params) ||
240 mop500_ab8500_channels != params_channels(params)) {
241 mutex_unlock(&mop500_ab8500_params_lock);
245 mop500_ab8500_rate = params_rate(params);
246 mop500_ab8500_channels = params_channels(params);
248 __set_bit(cpu_dai->id, &mop500_ab8500_usage);
249 mutex_unlock(&mop500_ab8500_params_lock);
251 channels = params_channels(params);
253 switch (params_format(params)) {
254 case SNDRV_PCM_FORMAT_S32_LE:
258 case SNDRV_PCM_FORMAT_S16_LE:
266 /* Setup codec depending on driver-mode */
268 driver_mode = DRIVERMODE_CODEC_ONLY;
270 driver_mode = DRIVERMODE_NORMAL;
271 dev_dbg(dev, "%s: Driver-mode: %s.\n", __func__,
272 (driver_mode == DRIVERMODE_NORMAL) ? "NORMAL" : "CODEC_ONLY");
276 if (driver_mode == DRIVERMODE_NORMAL) {
277 fmt = SND_SOC_DAIFMT_DSP_A |
278 SND_SOC_DAIFMT_CBM_CFM |
279 SND_SOC_DAIFMT_NB_NF |
282 fmt = SND_SOC_DAIFMT_DSP_A |
283 SND_SOC_DAIFMT_CBM_CFM |
284 SND_SOC_DAIFMT_NB_NF |
285 SND_SOC_DAIFMT_GATED;
288 ret = snd_soc_runtime_set_dai_fmt(rtd, fmt);
292 /* Setup TDM-slots */
294 is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
298 tx_slots = (is_playback) ? TX_SLOT_MONO : 0;
299 rx_slots = (is_playback) ? 0 : RX_SLOT_MONO;
303 tx_slots = (is_playback) ? TX_SLOT_STEREO : 0;
304 rx_slots = (is_playback) ? 0 : RX_SLOT_STEREO;
308 tx_slots = (is_playback) ? TX_SLOT_8CH : 0;
309 rx_slots = (is_playback) ? 0 : RX_SLOT_8CH;
315 if (driver_mode == DRIVERMODE_NORMAL)
320 dev_dbg(dev, "%s: CPU-DAI TDM: TX=0x%04X RX=0x%04x\n", __func__,
322 ret = snd_soc_dai_set_tdm_slot(cpu_dai, tx_slots, rx_slots, slots,
327 dev_dbg(dev, "%s: CODEC-DAI TDM: TX=0x%04X RX=0x%04x\n", __func__,
329 ret = snd_soc_dai_set_tdm_slot(codec_dai, tx_slots, rx_slots, slots,
337 static int mop500_ab8500_hw_free(struct snd_pcm_substream *substream)
339 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
340 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
342 mutex_lock(&mop500_ab8500_params_lock);
343 __clear_bit(cpu_dai->id, &mop500_ab8500_usage);
344 mutex_unlock(&mop500_ab8500_params_lock);
349 const struct snd_soc_ops mop500_ab8500_ops[] = {
351 .hw_params = mop500_ab8500_hw_params,
352 .hw_free = mop500_ab8500_hw_free,
353 .startup = mop500_ab8500_startup,
354 .shutdown = mop500_ab8500_shutdown,
358 int mop500_ab8500_machine_init(struct snd_soc_pcm_runtime *rtd)
360 struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
361 struct device *dev = rtd->card->dev;
362 struct mop500_ab8500_drvdata *drvdata;
365 dev_dbg(dev, "%s Enter.\n", __func__);
367 /* Create driver private-data struct */
368 drvdata = devm_kzalloc(dev, sizeof(struct mop500_ab8500_drvdata),
374 snd_soc_card_set_drvdata(rtd->card, drvdata);
378 drvdata->clk_ptr_sysclk = clk_get(dev, "sysclk");
379 if (IS_ERR(drvdata->clk_ptr_sysclk))
380 dev_warn(dev, "%s: WARNING: clk_get failed for 'sysclk'!\n",
382 drvdata->clk_ptr_ulpclk = clk_get(dev, "ulpclk");
383 if (IS_ERR(drvdata->clk_ptr_ulpclk))
384 dev_warn(dev, "%s: WARNING: clk_get failed for 'ulpclk'!\n",
386 drvdata->clk_ptr_intclk = clk_get(dev, "intclk");
387 if (IS_ERR(drvdata->clk_ptr_intclk))
388 dev_warn(dev, "%s: WARNING: clk_get failed for 'intclk'!\n",
391 /* Set intclk default parent to ulpclk */
392 drvdata->mclk_sel = MCLK_ULPCLK;
393 ret = mop500_ab8500_set_mclk(dev, drvdata);
395 dev_warn(dev, "%s: WARNING: mop500_ab8500_set_mclk!\n",
398 drvdata->mclk_sel = MCLK_ULPCLK;
401 ret = snd_soc_add_card_controls(rtd->card, mop500_ab8500_ctrls,
402 ARRAY_SIZE(mop500_ab8500_ctrls));
404 pr_err("%s: Failed to add machine-controls (%d)!\n",
409 ret = snd_soc_dapm_disable_pin(dapm, "Earpiece");
410 ret |= snd_soc_dapm_disable_pin(dapm, "Speaker Left");
411 ret |= snd_soc_dapm_disable_pin(dapm, "Speaker Right");
412 ret |= snd_soc_dapm_disable_pin(dapm, "LineOut Left");
413 ret |= snd_soc_dapm_disable_pin(dapm, "LineOut Right");
414 ret |= snd_soc_dapm_disable_pin(dapm, "Vibra 1");
415 ret |= snd_soc_dapm_disable_pin(dapm, "Vibra 2");
416 ret |= snd_soc_dapm_disable_pin(dapm, "Mic 1");
417 ret |= snd_soc_dapm_disable_pin(dapm, "Mic 2");
418 ret |= snd_soc_dapm_disable_pin(dapm, "LineIn Left");
419 ret |= snd_soc_dapm_disable_pin(dapm, "LineIn Right");
420 ret |= snd_soc_dapm_disable_pin(dapm, "DMic 1");
421 ret |= snd_soc_dapm_disable_pin(dapm, "DMic 2");
422 ret |= snd_soc_dapm_disable_pin(dapm, "DMic 3");
423 ret |= snd_soc_dapm_disable_pin(dapm, "DMic 4");
424 ret |= snd_soc_dapm_disable_pin(dapm, "DMic 5");
425 ret |= snd_soc_dapm_disable_pin(dapm, "DMic 6");
430 void mop500_ab8500_remove(struct snd_soc_card *card)
432 struct mop500_ab8500_drvdata *drvdata = snd_soc_card_get_drvdata(card);
434 clk_put(drvdata->clk_ptr_sysclk);
435 clk_put(drvdata->clk_ptr_ulpclk);
436 clk_put(drvdata->clk_ptr_intclk);
438 snd_soc_card_set_drvdata(card, drvdata);