1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * poodle.c -- SoC audio for Poodle
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2005 Wolfson Microelectronics PLC.
6*4882a593Smuzhiyun * Copyright 2005 Openedhand Ltd.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
9*4882a593Smuzhiyun * Richard Purdie <richard@openedhand.com>
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/moduleparam.h>
14*4882a593Smuzhiyun #include <linux/timer.h>
15*4882a593Smuzhiyun #include <linux/i2c.h>
16*4882a593Smuzhiyun #include <linux/interrupt.h>
17*4882a593Smuzhiyun #include <linux/platform_device.h>
18*4882a593Smuzhiyun #include <sound/core.h>
19*4882a593Smuzhiyun #include <sound/pcm.h>
20*4882a593Smuzhiyun #include <sound/soc.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <asm/mach-types.h>
23*4882a593Smuzhiyun #include <asm/hardware/locomo.h>
24*4882a593Smuzhiyun #include <mach/poodle.h>
25*4882a593Smuzhiyun #include <mach/audio.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "../codecs/wm8731.h"
28*4882a593Smuzhiyun #include "pxa2xx-i2s.h"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define POODLE_HP 1
31*4882a593Smuzhiyun #define POODLE_HP_OFF 0
32*4882a593Smuzhiyun #define POODLE_SPK_ON 1
33*4882a593Smuzhiyun #define POODLE_SPK_OFF 0
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* audio clock in Hz - rounded from 12.235MHz */
36*4882a593Smuzhiyun #define POODLE_AUDIO_CLOCK 12288000
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun static int poodle_jack_func;
39*4882a593Smuzhiyun static int poodle_spk_func;
40*4882a593Smuzhiyun
poodle_ext_control(struct snd_soc_dapm_context * dapm)41*4882a593Smuzhiyun static void poodle_ext_control(struct snd_soc_dapm_context *dapm)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun /* set up jack connection */
44*4882a593Smuzhiyun if (poodle_jack_func == POODLE_HP) {
45*4882a593Smuzhiyun /* set = unmute headphone */
46*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
47*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_L, 1);
48*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
49*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_R, 1);
50*4882a593Smuzhiyun snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
51*4882a593Smuzhiyun } else {
52*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
53*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_L, 0);
54*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
55*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_R, 0);
56*4882a593Smuzhiyun snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* set the endpoints to their new connection states */
60*4882a593Smuzhiyun if (poodle_spk_func == POODLE_SPK_ON)
61*4882a593Smuzhiyun snd_soc_dapm_enable_pin(dapm, "Ext Spk");
62*4882a593Smuzhiyun else
63*4882a593Smuzhiyun snd_soc_dapm_disable_pin(dapm, "Ext Spk");
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* signal a DAPM event */
66*4882a593Smuzhiyun snd_soc_dapm_sync(dapm);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
poodle_startup(struct snd_pcm_substream * substream)69*4882a593Smuzhiyun static int poodle_startup(struct snd_pcm_substream *substream)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* check the jack status at stream startup */
74*4882a593Smuzhiyun poodle_ext_control(&rtd->card->dapm);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun return 0;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* we need to unmute the HP at shutdown as the mute burns power on poodle */
poodle_shutdown(struct snd_pcm_substream * substream)80*4882a593Smuzhiyun static void poodle_shutdown(struct snd_pcm_substream *substream)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun /* set = unmute headphone */
83*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
84*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_L, 1);
85*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
86*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_R, 1);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
poodle_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)89*4882a593Smuzhiyun static int poodle_hw_params(struct snd_pcm_substream *substream,
90*4882a593Smuzhiyun struct snd_pcm_hw_params *params)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
93*4882a593Smuzhiyun struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
94*4882a593Smuzhiyun struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
95*4882a593Smuzhiyun unsigned int clk = 0;
96*4882a593Smuzhiyun int ret = 0;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun switch (params_rate(params)) {
99*4882a593Smuzhiyun case 8000:
100*4882a593Smuzhiyun case 16000:
101*4882a593Smuzhiyun case 48000:
102*4882a593Smuzhiyun case 96000:
103*4882a593Smuzhiyun clk = 12288000;
104*4882a593Smuzhiyun break;
105*4882a593Smuzhiyun case 11025:
106*4882a593Smuzhiyun case 22050:
107*4882a593Smuzhiyun case 44100:
108*4882a593Smuzhiyun clk = 11289600;
109*4882a593Smuzhiyun break;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /* set the codec system clock for DAC and ADC */
113*4882a593Smuzhiyun ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
114*4882a593Smuzhiyun SND_SOC_CLOCK_IN);
115*4882a593Smuzhiyun if (ret < 0)
116*4882a593Smuzhiyun return ret;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /* set the I2S system clock as input (unused) */
119*4882a593Smuzhiyun ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
120*4882a593Smuzhiyun SND_SOC_CLOCK_IN);
121*4882a593Smuzhiyun if (ret < 0)
122*4882a593Smuzhiyun return ret;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun static const struct snd_soc_ops poodle_ops = {
128*4882a593Smuzhiyun .startup = poodle_startup,
129*4882a593Smuzhiyun .hw_params = poodle_hw_params,
130*4882a593Smuzhiyun .shutdown = poodle_shutdown,
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
poodle_get_jack(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)133*4882a593Smuzhiyun static int poodle_get_jack(struct snd_kcontrol *kcontrol,
134*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = poodle_jack_func;
137*4882a593Smuzhiyun return 0;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
poodle_set_jack(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)140*4882a593Smuzhiyun static int poodle_set_jack(struct snd_kcontrol *kcontrol,
141*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (poodle_jack_func == ucontrol->value.enumerated.item[0])
146*4882a593Smuzhiyun return 0;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun poodle_jack_func = ucontrol->value.enumerated.item[0];
149*4882a593Smuzhiyun poodle_ext_control(&card->dapm);
150*4882a593Smuzhiyun return 1;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
poodle_get_spk(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)153*4882a593Smuzhiyun static int poodle_get_spk(struct snd_kcontrol *kcontrol,
154*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = poodle_spk_func;
157*4882a593Smuzhiyun return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
poodle_set_spk(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)160*4882a593Smuzhiyun static int poodle_set_spk(struct snd_kcontrol *kcontrol,
161*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun if (poodle_spk_func == ucontrol->value.enumerated.item[0])
166*4882a593Smuzhiyun return 0;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun poodle_spk_func = ucontrol->value.enumerated.item[0];
169*4882a593Smuzhiyun poodle_ext_control(&card->dapm);
170*4882a593Smuzhiyun return 1;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
poodle_amp_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)173*4882a593Smuzhiyun static int poodle_amp_event(struct snd_soc_dapm_widget *w,
174*4882a593Smuzhiyun struct snd_kcontrol *k, int event)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun if (SND_SOC_DAPM_EVENT_ON(event))
177*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
178*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_AMP_ON, 0);
179*4882a593Smuzhiyun else
180*4882a593Smuzhiyun locomo_gpio_write(&poodle_locomo_device.dev,
181*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_AMP_ON, 1);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun return 0;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* poodle machine dapm widgets */
187*4882a593Smuzhiyun static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
188*4882a593Smuzhiyun SND_SOC_DAPM_HP("Headphone Jack", NULL),
189*4882a593Smuzhiyun SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
190*4882a593Smuzhiyun SND_SOC_DAPM_MIC("Microphone", NULL),
191*4882a593Smuzhiyun };
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /* Corgi machine connections to the codec pins */
194*4882a593Smuzhiyun static const struct snd_soc_dapm_route poodle_audio_map[] = {
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /* headphone connected to LHPOUT1, RHPOUT1 */
197*4882a593Smuzhiyun {"Headphone Jack", NULL, "LHPOUT"},
198*4882a593Smuzhiyun {"Headphone Jack", NULL, "RHPOUT"},
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* speaker connected to LOUT, ROUT */
201*4882a593Smuzhiyun {"Ext Spk", NULL, "ROUT"},
202*4882a593Smuzhiyun {"Ext Spk", NULL, "LOUT"},
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun {"MICIN", NULL, "Microphone"},
205*4882a593Smuzhiyun };
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun static const char * const jack_function[] = {"Off", "Headphone"};
208*4882a593Smuzhiyun static const char * const spk_function[] = {"Off", "On"};
209*4882a593Smuzhiyun static const struct soc_enum poodle_enum[] = {
210*4882a593Smuzhiyun SOC_ENUM_SINGLE_EXT(2, jack_function),
211*4882a593Smuzhiyun SOC_ENUM_SINGLE_EXT(2, spk_function),
212*4882a593Smuzhiyun };
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
215*4882a593Smuzhiyun SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
216*4882a593Smuzhiyun poodle_set_jack),
217*4882a593Smuzhiyun SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
218*4882a593Smuzhiyun poodle_set_spk),
219*4882a593Smuzhiyun };
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* poodle digital audio interface glue - connects codec <--> CPU */
222*4882a593Smuzhiyun SND_SOC_DAILINK_DEFS(wm8731,
223*4882a593Smuzhiyun DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-i2s")),
224*4882a593Smuzhiyun DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.0-001b", "wm8731-hifi")),
225*4882a593Smuzhiyun DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun static struct snd_soc_dai_link poodle_dai = {
228*4882a593Smuzhiyun .name = "WM8731",
229*4882a593Smuzhiyun .stream_name = "WM8731",
230*4882a593Smuzhiyun .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
231*4882a593Smuzhiyun SND_SOC_DAIFMT_CBS_CFS,
232*4882a593Smuzhiyun .ops = &poodle_ops,
233*4882a593Smuzhiyun SND_SOC_DAILINK_REG(wm8731),
234*4882a593Smuzhiyun };
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun /* poodle audio machine driver */
237*4882a593Smuzhiyun static struct snd_soc_card poodle = {
238*4882a593Smuzhiyun .name = "Poodle",
239*4882a593Smuzhiyun .dai_link = &poodle_dai,
240*4882a593Smuzhiyun .num_links = 1,
241*4882a593Smuzhiyun .owner = THIS_MODULE,
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun .controls = wm8731_poodle_controls,
244*4882a593Smuzhiyun .num_controls = ARRAY_SIZE(wm8731_poodle_controls),
245*4882a593Smuzhiyun .dapm_widgets = wm8731_dapm_widgets,
246*4882a593Smuzhiyun .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
247*4882a593Smuzhiyun .dapm_routes = poodle_audio_map,
248*4882a593Smuzhiyun .num_dapm_routes = ARRAY_SIZE(poodle_audio_map),
249*4882a593Smuzhiyun .fully_routed = true,
250*4882a593Smuzhiyun };
251*4882a593Smuzhiyun
poodle_probe(struct platform_device * pdev)252*4882a593Smuzhiyun static int poodle_probe(struct platform_device *pdev)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun struct snd_soc_card *card = &poodle;
255*4882a593Smuzhiyun int ret;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun locomo_gpio_set_dir(&poodle_locomo_device.dev,
258*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_AMP_ON, 0);
259*4882a593Smuzhiyun /* should we mute HP at startup - burning power ?*/
260*4882a593Smuzhiyun locomo_gpio_set_dir(&poodle_locomo_device.dev,
261*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_L, 0);
262*4882a593Smuzhiyun locomo_gpio_set_dir(&poodle_locomo_device.dev,
263*4882a593Smuzhiyun POODLE_LOCOMO_GPIO_MUTE_R, 0);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun card->dev = &pdev->dev;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun ret = devm_snd_soc_register_card(&pdev->dev, card);
268*4882a593Smuzhiyun if (ret)
269*4882a593Smuzhiyun dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
270*4882a593Smuzhiyun ret);
271*4882a593Smuzhiyun return ret;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun static struct platform_driver poodle_driver = {
275*4882a593Smuzhiyun .driver = {
276*4882a593Smuzhiyun .name = "poodle-audio",
277*4882a593Smuzhiyun .pm = &snd_soc_pm_ops,
278*4882a593Smuzhiyun },
279*4882a593Smuzhiyun .probe = poodle_probe,
280*4882a593Smuzhiyun };
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun module_platform_driver(poodle_driver);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /* Module information */
285*4882a593Smuzhiyun MODULE_AUTHOR("Richard Purdie");
286*4882a593Smuzhiyun MODULE_DESCRIPTION("ALSA SoC Poodle");
287*4882a593Smuzhiyun MODULE_LICENSE("GPL");
288*4882a593Smuzhiyun MODULE_ALIAS("platform:poodle-audio");
289