2 * Copyright (C) ST-Ericsson SA 2012
4 * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
5 * Kristoffer Karlsson <kristoffer.karlsson@stericsson.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/device.h>
18 #include <linux/clk.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
25 #include "ux500_pcm.h"
26 #include "ux500_msp_dai.h"
27 #include "../codecs/ab8500-codec.h"
29 #define TX_SLOT_MONO 0x0008
30 #define TX_SLOT_STEREO 0x000a
31 #define RX_SLOT_MONO 0x0001
32 #define RX_SLOT_STEREO 0x0003
33 #define TX_SLOT_8CH 0x00FF
34 #define RX_SLOT_8CH 0x00FF
36 #define DEF_TX_SLOTS TX_SLOT_STEREO
37 #define DEF_RX_SLOTS RX_SLOT_MONO
39 #define DRIVERMODE_NORMAL 0
40 #define DRIVERMODE_CODEC_ONLY 1
42 /* Slot configuration */
43 static unsigned int tx_slots = DEF_TX_SLOTS;
44 static unsigned int rx_slots = DEF_RX_SLOTS;
47 static const char * const enum_mclk[] = {
56 static SOC_ENUM_SINGLE_EXT_DECL(soc_enum_mclk, enum_mclk);
58 /* Private data for machine-part MOP500<->AB8500 */
59 struct mop500_ab8500_drvdata {
62 struct clk *clk_ptr_intclk;
63 struct clk *clk_ptr_sysclk;
64 struct clk *clk_ptr_ulpclk;
67 static inline const char *get_mclk_str(enum mclk mclk_sel)
79 static int mop500_ab8500_set_mclk(struct device *dev,
80 struct mop500_ab8500_drvdata *drvdata)
85 if (IS_ERR(drvdata->clk_ptr_intclk)) {
87 "%s: ERROR: intclk not initialized!\n", __func__);
91 switch (drvdata->mclk_sel) {
93 clk_ptr = drvdata->clk_ptr_sysclk;
96 clk_ptr = drvdata->clk_ptr_ulpclk;
102 if (IS_ERR(clk_ptr)) {
103 dev_err(dev, "%s: ERROR: %s not initialized!\n", __func__,
104 get_mclk_str(drvdata->mclk_sel));
108 status = clk_set_parent(drvdata->clk_ptr_intclk, clk_ptr);
111 "%s: ERROR: Setting intclk parent to %s failed (ret = %d)!",
112 __func__, get_mclk_str(drvdata->mclk_sel), status);
115 "%s: intclk parent changed to %s.\n",
116 __func__, get_mclk_str(drvdata->mclk_sel));
125 static int mclk_input_control_get(struct snd_kcontrol *kcontrol,
126 struct snd_ctl_elem_value *ucontrol)
128 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
129 struct mop500_ab8500_drvdata *drvdata =
130 snd_soc_card_get_drvdata(card);
132 ucontrol->value.enumerated.item[0] = drvdata->mclk_sel;
137 static int mclk_input_control_put(struct snd_kcontrol *kcontrol,
138 struct snd_ctl_elem_value *ucontrol)
140 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
141 struct mop500_ab8500_drvdata *drvdata =
142 snd_soc_card_get_drvdata(card);
143 unsigned int val = ucontrol->value.enumerated.item[0];
145 if (val > (unsigned int)MCLK_ULPCLK)
147 if (drvdata->mclk_sel == val)
150 drvdata->mclk_sel = val;
159 static struct snd_kcontrol_new mop500_ab8500_ctrls[] = {
160 SOC_ENUM_EXT("Master Clock Select",
162 mclk_input_control_get, mclk_input_control_put),
163 SOC_DAPM_PIN_SWITCH("Headset Left"),
164 SOC_DAPM_PIN_SWITCH("Headset Right"),
165 SOC_DAPM_PIN_SWITCH("Earpiece"),
166 SOC_DAPM_PIN_SWITCH("Speaker Left"),
167 SOC_DAPM_PIN_SWITCH("Speaker Right"),
168 SOC_DAPM_PIN_SWITCH("LineOut Left"),
169 SOC_DAPM_PIN_SWITCH("LineOut Right"),
170 SOC_DAPM_PIN_SWITCH("Vibra 1"),
171 SOC_DAPM_PIN_SWITCH("Vibra 2"),
172 SOC_DAPM_PIN_SWITCH("Mic 1"),
173 SOC_DAPM_PIN_SWITCH("Mic 2"),
174 SOC_DAPM_PIN_SWITCH("LineIn Left"),
175 SOC_DAPM_PIN_SWITCH("LineIn Right"),
176 SOC_DAPM_PIN_SWITCH("DMic 1"),
177 SOC_DAPM_PIN_SWITCH("DMic 2"),
178 SOC_DAPM_PIN_SWITCH("DMic 3"),
179 SOC_DAPM_PIN_SWITCH("DMic 4"),
180 SOC_DAPM_PIN_SWITCH("DMic 5"),
181 SOC_DAPM_PIN_SWITCH("DMic 6"),
186 static int mop500_ab8500_startup(struct snd_pcm_substream *substream)
188 struct snd_soc_pcm_runtime *rtd = substream->private_data;
190 /* Set audio-clock source */
191 return mop500_ab8500_set_mclk(rtd->card->dev,
192 snd_soc_card_get_drvdata(rtd->card));
195 static void mop500_ab8500_shutdown(struct snd_pcm_substream *substream)
197 struct snd_soc_pcm_runtime *rtd = substream->private_data;
198 struct device *dev = rtd->card->dev;
200 dev_dbg(dev, "%s: Enter\n", __func__);
202 /* Reset slots configuration to default(s) */
203 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
204 tx_slots = DEF_TX_SLOTS;
206 rx_slots = DEF_RX_SLOTS;
209 static int mop500_ab8500_hw_params(struct snd_pcm_substream *substream,
210 struct snd_pcm_hw_params *params)
212 struct snd_soc_pcm_runtime *rtd = substream->private_data;
213 struct snd_soc_dai *codec_dai = rtd->codec_dai;
214 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
215 struct device *dev = rtd->card->dev;
217 int channels, ret = 0, driver_mode, slots;
218 unsigned int sw_codec, sw_cpu;
221 dev_dbg(dev, "%s: Enter\n", __func__);
223 dev_dbg(dev, "%s: substream->pcm->name = %s\n"
224 "substream->pcm->id = %s.\n"
225 "substream->name = %s.\n"
226 "substream->number = %d.\n",
228 substream->pcm->name,
233 channels = params_channels(params);
235 switch (params_format(params)) {
236 case SNDRV_PCM_FORMAT_S32_LE:
240 case SNDRV_PCM_FORMAT_S16_LE:
248 /* Setup codec depending on driver-mode */
250 driver_mode = DRIVERMODE_CODEC_ONLY;
252 driver_mode = DRIVERMODE_NORMAL;
253 dev_dbg(dev, "%s: Driver-mode: %s.\n", __func__,
254 (driver_mode == DRIVERMODE_NORMAL) ? "NORMAL" : "CODEC_ONLY");
258 if (driver_mode == DRIVERMODE_NORMAL) {
259 fmt = SND_SOC_DAIFMT_DSP_A |
260 SND_SOC_DAIFMT_CBM_CFM |
261 SND_SOC_DAIFMT_NB_NF |
264 fmt = SND_SOC_DAIFMT_DSP_A |
265 SND_SOC_DAIFMT_CBM_CFM |
266 SND_SOC_DAIFMT_NB_NF |
267 SND_SOC_DAIFMT_GATED;
270 ret = snd_soc_dai_set_fmt(codec_dai, fmt);
273 "%s: ERROR: snd_soc_dai_set_fmt failed for codec_dai (ret = %d)!\n",
278 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
281 "%s: ERROR: snd_soc_dai_set_fmt failed for cpu_dai (ret = %d)!\n",
286 /* Setup TDM-slots */
288 is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
292 tx_slots = (is_playback) ? TX_SLOT_MONO : 0;
293 rx_slots = (is_playback) ? 0 : RX_SLOT_MONO;
297 tx_slots = (is_playback) ? TX_SLOT_STEREO : 0;
298 rx_slots = (is_playback) ? 0 : RX_SLOT_STEREO;
302 tx_slots = (is_playback) ? TX_SLOT_8CH : 0;
303 rx_slots = (is_playback) ? 0 : RX_SLOT_8CH;
309 if (driver_mode == DRIVERMODE_NORMAL)
314 dev_dbg(dev, "%s: CPU-DAI TDM: TX=0x%04X RX=0x%04x\n", __func__,
316 ret = snd_soc_dai_set_tdm_slot(cpu_dai, tx_slots, rx_slots, slots,
321 dev_dbg(dev, "%s: CODEC-DAI TDM: TX=0x%04X RX=0x%04x\n", __func__,
323 ret = snd_soc_dai_set_tdm_slot(codec_dai, tx_slots, rx_slots, slots,
331 struct snd_soc_ops mop500_ab8500_ops[] = {
333 .hw_params = mop500_ab8500_hw_params,
334 .startup = mop500_ab8500_startup,
335 .shutdown = mop500_ab8500_shutdown,
339 int mop500_ab8500_machine_init(struct snd_soc_pcm_runtime *rtd)
341 struct snd_soc_codec *codec = rtd->codec;
342 struct device *dev = rtd->card->dev;
343 struct mop500_ab8500_drvdata *drvdata;
346 dev_dbg(dev, "%s Enter.\n", __func__);
348 /* Create driver private-data struct */
349 drvdata = devm_kzalloc(dev, sizeof(struct mop500_ab8500_drvdata),
351 snd_soc_card_set_drvdata(rtd->card, drvdata);
355 drvdata->clk_ptr_sysclk = clk_get(dev, "sysclk");
356 if (IS_ERR(drvdata->clk_ptr_sysclk))
357 dev_warn(dev, "%s: WARNING: clk_get failed for 'sysclk'!\n",
359 drvdata->clk_ptr_ulpclk = clk_get(dev, "ulpclk");
360 if (IS_ERR(drvdata->clk_ptr_ulpclk))
361 dev_warn(dev, "%s: WARNING: clk_get failed for 'ulpclk'!\n",
363 drvdata->clk_ptr_intclk = clk_get(dev, "intclk");
364 if (IS_ERR(drvdata->clk_ptr_intclk))
365 dev_warn(dev, "%s: WARNING: clk_get failed for 'intclk'!\n",
368 /* Set intclk default parent to ulpclk */
369 drvdata->mclk_sel = MCLK_ULPCLK;
370 ret = mop500_ab8500_set_mclk(dev, drvdata);
372 dev_warn(dev, "%s: WARNING: mop500_ab8500_set_mclk!\n",
375 drvdata->mclk_sel = MCLK_ULPCLK;
378 ret = snd_soc_add_card_controls(codec->card, mop500_ab8500_ctrls,
379 ARRAY_SIZE(mop500_ab8500_ctrls));
381 pr_err("%s: Failed to add machine-controls (%d)!\n",
386 ret = snd_soc_dapm_disable_pin(&codec->dapm, "Earpiece");
387 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "Speaker Left");
388 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "Speaker Right");
389 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "LineOut Left");
390 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "LineOut Right");
391 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "Vibra 1");
392 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "Vibra 2");
393 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "Mic 1");
394 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "Mic 2");
395 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "LineIn Left");
396 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "LineIn Right");
397 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "DMic 1");
398 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "DMic 2");
399 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "DMic 3");
400 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "DMic 4");
401 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "DMic 5");
402 ret |= snd_soc_dapm_disable_pin(&codec->dapm, "DMic 6");
407 void mop500_ab8500_remove(struct snd_soc_card *card)
409 struct mop500_ab8500_drvdata *drvdata = snd_soc_card_get_drvdata(card);
411 if (drvdata->clk_ptr_sysclk != NULL)
412 clk_put(drvdata->clk_ptr_sysclk);
413 if (drvdata->clk_ptr_ulpclk != NULL)
414 clk_put(drvdata->clk_ptr_ulpclk);
415 if (drvdata->clk_ptr_intclk != NULL)
416 clk_put(drvdata->clk_ptr_intclk);
418 snd_soc_card_set_drvdata(card, drvdata);