1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * spitz.c -- SoC audio for Sharp SL-Cxx00 models Spitz, Borzoi and Akita
5 * Copyright 2005 Wolfson Microelectronics PLC.
6 * Copyright 2005 Openedhand Ltd.
8 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
9 * Richard Purdie <richard@openedhand.com>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/timer.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/gpio.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/soc.h>
22 #include <asm/mach-types.h>
23 #include <mach/spitz.h>
24 #include "../codecs/wm8750.h"
25 #include "pxa2xx-i2s.h"
30 #define SPITZ_HEADSET 3
31 #define SPITZ_HP_OFF 4
32 #define SPITZ_SPK_ON 0
33 #define SPITZ_SPK_OFF 1
35 /* audio clock in Hz - rounded from 12.235MHz */
36 #define SPITZ_AUDIO_CLOCK 12288000
38 static int spitz_jack_func;
39 static int spitz_spk_func;
40 static int spitz_mic_gpio;
42 static void spitz_ext_control(struct snd_soc_dapm_context *dapm)
44 snd_soc_dapm_mutex_lock(dapm);
46 if (spitz_spk_func == SPITZ_SPK_ON)
47 snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
49 snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
51 /* set up jack connection */
52 switch (spitz_jack_func) {
54 /* enable and unmute hp jack, disable mic bias */
55 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
56 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
57 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
58 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
59 gpio_set_value(SPITZ_GPIO_MUTE_L, 1);
60 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
63 /* enable mic jack and bias, mute hp */
64 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
65 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
66 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
67 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
68 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
69 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
72 /* enable line jack, disable mic bias and mute hp */
73 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
74 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
75 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
76 snd_soc_dapm_enable_pin_unlocked(dapm, "Line Jack");
77 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
78 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
81 /* enable and unmute headset jack enable mic bias, mute L hp */
82 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
83 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
84 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
85 snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
86 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
87 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
91 /* jack removed, everything off */
92 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
93 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
94 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
95 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
96 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
97 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
101 snd_soc_dapm_sync_unlocked(dapm);
103 snd_soc_dapm_mutex_unlock(dapm);
106 static int spitz_startup(struct snd_pcm_substream *substream)
108 struct snd_soc_pcm_runtime *rtd = substream->private_data;
110 /* check the jack status at stream startup */
111 spitz_ext_control(&rtd->card->dapm);
116 static int spitz_hw_params(struct snd_pcm_substream *substream,
117 struct snd_pcm_hw_params *params)
119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
120 struct snd_soc_dai *codec_dai = rtd->codec_dai;
121 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
122 unsigned int clk = 0;
125 switch (params_rate(params)) {
139 /* set the codec system clock for DAC and ADC */
140 ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
145 /* set the I2S system clock as input (unused) */
146 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
154 static const struct snd_soc_ops spitz_ops = {
155 .startup = spitz_startup,
156 .hw_params = spitz_hw_params,
159 static int spitz_get_jack(struct snd_kcontrol *kcontrol,
160 struct snd_ctl_elem_value *ucontrol)
162 ucontrol->value.enumerated.item[0] = spitz_jack_func;
166 static int spitz_set_jack(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
169 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
171 if (spitz_jack_func == ucontrol->value.enumerated.item[0])
174 spitz_jack_func = ucontrol->value.enumerated.item[0];
175 spitz_ext_control(&card->dapm);
179 static int spitz_get_spk(struct snd_kcontrol *kcontrol,
180 struct snd_ctl_elem_value *ucontrol)
182 ucontrol->value.enumerated.item[0] = spitz_spk_func;
186 static int spitz_set_spk(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
189 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
191 if (spitz_spk_func == ucontrol->value.enumerated.item[0])
194 spitz_spk_func = ucontrol->value.enumerated.item[0];
195 spitz_ext_control(&card->dapm);
199 static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
200 struct snd_kcontrol *k, int event)
202 gpio_set_value_cansleep(spitz_mic_gpio, SND_SOC_DAPM_EVENT_ON(event));
206 /* spitz machine dapm widgets */
207 static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
208 SND_SOC_DAPM_HP("Headphone Jack", NULL),
209 SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
210 SND_SOC_DAPM_SPK("Ext Spk", NULL),
211 SND_SOC_DAPM_LINE("Line Jack", NULL),
213 /* headset is a mic and mono headphone */
214 SND_SOC_DAPM_HP("Headset Jack", NULL),
217 /* Spitz machine audio_map */
218 static const struct snd_soc_dapm_route spitz_audio_map[] = {
220 /* headphone connected to LOUT1, ROUT1 */
221 {"Headphone Jack", NULL, "LOUT1"},
222 {"Headphone Jack", NULL, "ROUT1"},
224 /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
225 {"Headset Jack", NULL, "ROUT1"},
227 /* ext speaker connected to LOUT2, ROUT2 */
228 {"Ext Spk", NULL, "ROUT2"},
229 {"Ext Spk", NULL, "LOUT2"},
231 /* mic is connected to input 1 - with bias */
232 {"LINPUT1", NULL, "Mic Bias"},
233 {"Mic Bias", NULL, "Mic Jack"},
235 /* line is connected to input 1 - no bias */
236 {"LINPUT1", NULL, "Line Jack"},
239 static const char * const jack_function[] = {"Headphone", "Mic", "Line",
241 static const char * const spk_function[] = {"On", "Off"};
242 static const struct soc_enum spitz_enum[] = {
243 SOC_ENUM_SINGLE_EXT(5, jack_function),
244 SOC_ENUM_SINGLE_EXT(2, spk_function),
247 static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
248 SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
250 SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
254 /* spitz digital audio interface glue - connects codec <--> CPU */
255 static struct snd_soc_dai_link spitz_dai = {
257 .stream_name = "WM8750",
258 .cpu_dai_name = "pxa2xx-i2s",
259 .codec_dai_name = "wm8750-hifi",
260 .platform_name = "pxa-pcm-audio",
261 .codec_name = "wm8750.0-001b",
262 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
263 SND_SOC_DAIFMT_CBS_CFS,
267 /* spitz audio machine driver */
268 static struct snd_soc_card snd_soc_spitz = {
270 .owner = THIS_MODULE,
271 .dai_link = &spitz_dai,
274 .controls = wm8750_spitz_controls,
275 .num_controls = ARRAY_SIZE(wm8750_spitz_controls),
276 .dapm_widgets = wm8750_dapm_widgets,
277 .num_dapm_widgets = ARRAY_SIZE(wm8750_dapm_widgets),
278 .dapm_routes = spitz_audio_map,
279 .num_dapm_routes = ARRAY_SIZE(spitz_audio_map),
280 .fully_routed = true,
283 static int spitz_probe(struct platform_device *pdev)
285 struct snd_soc_card *card = &snd_soc_spitz;
288 if (machine_is_akita())
289 spitz_mic_gpio = AKITA_GPIO_MIC_BIAS;
291 spitz_mic_gpio = SPITZ_GPIO_MIC_BIAS;
293 ret = gpio_request(spitz_mic_gpio, "MIC GPIO");
297 ret = gpio_direction_output(spitz_mic_gpio, 0);
301 card->dev = &pdev->dev;
303 ret = devm_snd_soc_register_card(&pdev->dev, card);
305 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
313 gpio_free(spitz_mic_gpio);
318 static int spitz_remove(struct platform_device *pdev)
320 gpio_free(spitz_mic_gpio);
324 static struct platform_driver spitz_driver = {
326 .name = "spitz-audio",
327 .pm = &snd_soc_pm_ops,
329 .probe = spitz_probe,
330 .remove = spitz_remove,
333 module_platform_driver(spitz_driver);
335 MODULE_AUTHOR("Richard Purdie");
336 MODULE_DESCRIPTION("ALSA SoC Spitz");
337 MODULE_LICENSE("GPL");
338 MODULE_ALIAS("platform:spitz-audio");