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>
19 #include <linux/mutex.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dapm.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
26 #include "ux500_pcm.h"
27 #include "ux500_msp_dai.h"
28 #include "mop500_ab8500.h"
29 #include "../codecs/ab8500-codec.h"
31 #define TX_SLOT_MONO 0x0008
32 #define TX_SLOT_STEREO 0x000a
33 #define RX_SLOT_MONO 0x0001
34 #define RX_SLOT_STEREO 0x0003
35 #define TX_SLOT_8CH 0x00FF
36 #define RX_SLOT_8CH 0x00FF
38 #define DEF_TX_SLOTS TX_SLOT_STEREO
39 #define DEF_RX_SLOTS RX_SLOT_MONO
41 #define DRIVERMODE_NORMAL 0
42 #define DRIVERMODE_CODEC_ONLY 1
44 /* Slot configuration */
45 static unsigned int tx_slots
= DEF_TX_SLOTS
;
46 static unsigned int rx_slots
= DEF_RX_SLOTS
;
48 /* Configuration consistency parameters */
49 static DEFINE_MUTEX(mop500_ab8500_params_lock
);
50 static unsigned long mop500_ab8500_usage
;
51 static int mop500_ab8500_rate
;
52 static int mop500_ab8500_channels
;
55 static const char * const enum_mclk
[] = {
64 static SOC_ENUM_SINGLE_EXT_DECL(soc_enum_mclk
, enum_mclk
);
66 /* Private data for machine-part MOP500<->AB8500 */
67 struct mop500_ab8500_drvdata
{
70 struct clk
*clk_ptr_intclk
;
71 struct clk
*clk_ptr_sysclk
;
72 struct clk
*clk_ptr_ulpclk
;
75 static inline const char *get_mclk_str(enum mclk mclk_sel
)
87 static int mop500_ab8500_set_mclk(struct device
*dev
,
88 struct mop500_ab8500_drvdata
*drvdata
)
93 if (IS_ERR(drvdata
->clk_ptr_intclk
)) {
95 "%s: ERROR: intclk not initialized!\n", __func__
);
99 switch (drvdata
->mclk_sel
) {
101 clk_ptr
= drvdata
->clk_ptr_sysclk
;
104 clk_ptr
= drvdata
->clk_ptr_ulpclk
;
110 if (IS_ERR(clk_ptr
)) {
111 dev_err(dev
, "%s: ERROR: %s not initialized!\n", __func__
,
112 get_mclk_str(drvdata
->mclk_sel
));
116 status
= clk_set_parent(drvdata
->clk_ptr_intclk
, clk_ptr
);
119 "%s: ERROR: Setting intclk parent to %s failed (ret = %d)!",
120 __func__
, get_mclk_str(drvdata
->mclk_sel
), status
);
123 "%s: intclk parent changed to %s.\n",
124 __func__
, get_mclk_str(drvdata
->mclk_sel
));
133 static int mclk_input_control_get(struct snd_kcontrol
*kcontrol
,
134 struct snd_ctl_elem_value
*ucontrol
)
136 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
137 struct mop500_ab8500_drvdata
*drvdata
=
138 snd_soc_card_get_drvdata(card
);
140 ucontrol
->value
.enumerated
.item
[0] = drvdata
->mclk_sel
;
145 static int mclk_input_control_put(struct snd_kcontrol
*kcontrol
,
146 struct snd_ctl_elem_value
*ucontrol
)
148 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
149 struct mop500_ab8500_drvdata
*drvdata
=
150 snd_soc_card_get_drvdata(card
);
151 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
153 if (val
> (unsigned int)MCLK_ULPCLK
)
155 if (drvdata
->mclk_sel
== val
)
158 drvdata
->mclk_sel
= val
;
167 static struct snd_kcontrol_new mop500_ab8500_ctrls
[] = {
168 SOC_ENUM_EXT("Master Clock Select",
170 mclk_input_control_get
, mclk_input_control_put
),
171 SOC_DAPM_PIN_SWITCH("Headset Left"),
172 SOC_DAPM_PIN_SWITCH("Headset Right"),
173 SOC_DAPM_PIN_SWITCH("Earpiece"),
174 SOC_DAPM_PIN_SWITCH("Speaker Left"),
175 SOC_DAPM_PIN_SWITCH("Speaker Right"),
176 SOC_DAPM_PIN_SWITCH("LineOut Left"),
177 SOC_DAPM_PIN_SWITCH("LineOut Right"),
178 SOC_DAPM_PIN_SWITCH("Vibra 1"),
179 SOC_DAPM_PIN_SWITCH("Vibra 2"),
180 SOC_DAPM_PIN_SWITCH("Mic 1"),
181 SOC_DAPM_PIN_SWITCH("Mic 2"),
182 SOC_DAPM_PIN_SWITCH("LineIn Left"),
183 SOC_DAPM_PIN_SWITCH("LineIn Right"),
184 SOC_DAPM_PIN_SWITCH("DMic 1"),
185 SOC_DAPM_PIN_SWITCH("DMic 2"),
186 SOC_DAPM_PIN_SWITCH("DMic 3"),
187 SOC_DAPM_PIN_SWITCH("DMic 4"),
188 SOC_DAPM_PIN_SWITCH("DMic 5"),
189 SOC_DAPM_PIN_SWITCH("DMic 6"),
194 static int mop500_ab8500_startup(struct snd_pcm_substream
*substream
)
196 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
198 /* Set audio-clock source */
199 return mop500_ab8500_set_mclk(rtd
->card
->dev
,
200 snd_soc_card_get_drvdata(rtd
->card
));
203 static void mop500_ab8500_shutdown(struct snd_pcm_substream
*substream
)
205 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
206 struct device
*dev
= rtd
->card
->dev
;
208 dev_dbg(dev
, "%s: Enter\n", __func__
);
210 /* Reset slots configuration to default(s) */
211 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
212 tx_slots
= DEF_TX_SLOTS
;
214 rx_slots
= DEF_RX_SLOTS
;
217 static int mop500_ab8500_hw_params(struct snd_pcm_substream
*substream
,
218 struct snd_pcm_hw_params
*params
)
220 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
221 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
222 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
223 struct device
*dev
= rtd
->card
->dev
;
225 int channels
, ret
= 0, driver_mode
, slots
;
226 unsigned int sw_codec
, sw_cpu
;
229 dev_dbg(dev
, "%s: Enter\n", __func__
);
231 dev_dbg(dev
, "%s: substream->pcm->name = %s\n"
232 "substream->pcm->id = %s.\n"
233 "substream->name = %s.\n"
234 "substream->number = %d.\n",
236 substream
->pcm
->name
,
241 /* Ensure configuration consistency between DAIs */
242 mutex_lock(&mop500_ab8500_params_lock
);
243 if (mop500_ab8500_usage
) {
244 if (mop500_ab8500_rate
!= params_rate(params
) ||
245 mop500_ab8500_channels
!= params_channels(params
)) {
246 mutex_unlock(&mop500_ab8500_params_lock
);
250 mop500_ab8500_rate
= params_rate(params
);
251 mop500_ab8500_channels
= params_channels(params
);
253 __set_bit(cpu_dai
->id
, &mop500_ab8500_usage
);
254 mutex_unlock(&mop500_ab8500_params_lock
);
256 channels
= params_channels(params
);
258 switch (params_format(params
)) {
259 case SNDRV_PCM_FORMAT_S32_LE
:
263 case SNDRV_PCM_FORMAT_S16_LE
:
271 /* Setup codec depending on driver-mode */
273 driver_mode
= DRIVERMODE_CODEC_ONLY
;
275 driver_mode
= DRIVERMODE_NORMAL
;
276 dev_dbg(dev
, "%s: Driver-mode: %s.\n", __func__
,
277 (driver_mode
== DRIVERMODE_NORMAL
) ? "NORMAL" : "CODEC_ONLY");
281 if (driver_mode
== DRIVERMODE_NORMAL
) {
282 fmt
= SND_SOC_DAIFMT_DSP_A
|
283 SND_SOC_DAIFMT_CBM_CFM
|
284 SND_SOC_DAIFMT_NB_NF
|
287 fmt
= SND_SOC_DAIFMT_DSP_A
|
288 SND_SOC_DAIFMT_CBM_CFM
|
289 SND_SOC_DAIFMT_NB_NF
|
290 SND_SOC_DAIFMT_GATED
;
293 ret
= snd_soc_dai_set_fmt(codec_dai
, fmt
);
296 "%s: ERROR: snd_soc_dai_set_fmt failed for codec_dai (ret = %d)!\n",
301 ret
= snd_soc_dai_set_fmt(cpu_dai
, fmt
);
304 "%s: ERROR: snd_soc_dai_set_fmt failed for cpu_dai (ret = %d)!\n",
309 /* Setup TDM-slots */
311 is_playback
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
315 tx_slots
= (is_playback
) ? TX_SLOT_MONO
: 0;
316 rx_slots
= (is_playback
) ? 0 : RX_SLOT_MONO
;
320 tx_slots
= (is_playback
) ? TX_SLOT_STEREO
: 0;
321 rx_slots
= (is_playback
) ? 0 : RX_SLOT_STEREO
;
325 tx_slots
= (is_playback
) ? TX_SLOT_8CH
: 0;
326 rx_slots
= (is_playback
) ? 0 : RX_SLOT_8CH
;
332 if (driver_mode
== DRIVERMODE_NORMAL
)
337 dev_dbg(dev
, "%s: CPU-DAI TDM: TX=0x%04X RX=0x%04x\n", __func__
,
339 ret
= snd_soc_dai_set_tdm_slot(cpu_dai
, tx_slots
, rx_slots
, slots
,
344 dev_dbg(dev
, "%s: CODEC-DAI TDM: TX=0x%04X RX=0x%04x\n", __func__
,
346 ret
= snd_soc_dai_set_tdm_slot(codec_dai
, tx_slots
, rx_slots
, slots
,
354 static int mop500_ab8500_hw_free(struct snd_pcm_substream
*substream
)
356 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
357 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
359 mutex_lock(&mop500_ab8500_params_lock
);
360 __clear_bit(cpu_dai
->id
, &mop500_ab8500_usage
);
361 mutex_unlock(&mop500_ab8500_params_lock
);
366 struct snd_soc_ops mop500_ab8500_ops
[] = {
368 .hw_params
= mop500_ab8500_hw_params
,
369 .hw_free
= mop500_ab8500_hw_free
,
370 .startup
= mop500_ab8500_startup
,
371 .shutdown
= mop500_ab8500_shutdown
,
375 int mop500_ab8500_machine_init(struct snd_soc_pcm_runtime
*rtd
)
377 struct snd_soc_codec
*codec
= rtd
->codec
;
378 struct device
*dev
= rtd
->card
->dev
;
379 struct mop500_ab8500_drvdata
*drvdata
;
382 dev_dbg(dev
, "%s Enter.\n", __func__
);
384 /* Create driver private-data struct */
385 drvdata
= devm_kzalloc(dev
, sizeof(struct mop500_ab8500_drvdata
),
387 snd_soc_card_set_drvdata(rtd
->card
, drvdata
);
391 drvdata
->clk_ptr_sysclk
= clk_get(dev
, "sysclk");
392 if (IS_ERR(drvdata
->clk_ptr_sysclk
))
393 dev_warn(dev
, "%s: WARNING: clk_get failed for 'sysclk'!\n",
395 drvdata
->clk_ptr_ulpclk
= clk_get(dev
, "ulpclk");
396 if (IS_ERR(drvdata
->clk_ptr_ulpclk
))
397 dev_warn(dev
, "%s: WARNING: clk_get failed for 'ulpclk'!\n",
399 drvdata
->clk_ptr_intclk
= clk_get(dev
, "intclk");
400 if (IS_ERR(drvdata
->clk_ptr_intclk
))
401 dev_warn(dev
, "%s: WARNING: clk_get failed for 'intclk'!\n",
404 /* Set intclk default parent to ulpclk */
405 drvdata
->mclk_sel
= MCLK_ULPCLK
;
406 ret
= mop500_ab8500_set_mclk(dev
, drvdata
);
408 dev_warn(dev
, "%s: WARNING: mop500_ab8500_set_mclk!\n",
411 drvdata
->mclk_sel
= MCLK_ULPCLK
;
414 ret
= snd_soc_add_card_controls(codec
->card
, mop500_ab8500_ctrls
,
415 ARRAY_SIZE(mop500_ab8500_ctrls
));
417 pr_err("%s: Failed to add machine-controls (%d)!\n",
422 ret
= snd_soc_dapm_disable_pin(&codec
->dapm
, "Earpiece");
423 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "Speaker Left");
424 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "Speaker Right");
425 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "LineOut Left");
426 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "LineOut Right");
427 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "Vibra 1");
428 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "Vibra 2");
429 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "Mic 1");
430 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "Mic 2");
431 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "LineIn Left");
432 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "LineIn Right");
433 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "DMic 1");
434 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "DMic 2");
435 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "DMic 3");
436 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "DMic 4");
437 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "DMic 5");
438 ret
|= snd_soc_dapm_disable_pin(&codec
->dapm
, "DMic 6");
443 void mop500_ab8500_remove(struct snd_soc_card
*card
)
445 struct mop500_ab8500_drvdata
*drvdata
= snd_soc_card_get_drvdata(card
);
447 if (drvdata
->clk_ptr_sysclk
!= NULL
)
448 clk_put(drvdata
->clk_ptr_sysclk
);
449 if (drvdata
->clk_ptr_ulpclk
!= NULL
)
450 clk_put(drvdata
->clk_ptr_ulpclk
);
451 if (drvdata
->clk_ptr_intclk
!= NULL
)
452 clk_put(drvdata
->clk_ptr_intclk
);
454 snd_soc_card_set_drvdata(card
, drvdata
);