blob: 05559a725bec22ed16664c8a08493108c9ed298b [file] [log] [blame]
Dmitry Artamonowc26f6422011-05-18 19:25:10 +04001/*
2 * SoC audio for HP iPAQ hx4700
3 *
4 * Copyright (c) 2009 Philipp Zabel
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/timer.h>
15#include <linux/interrupt.h>
16#include <linux/platform_device.h>
17#include <linux/delay.h>
18#include <linux/gpio.h>
19
20#include <sound/core.h>
21#include <sound/jack.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25
26#include <mach/hx4700.h>
27#include <asm/mach-types.h>
28#include "pxa2xx-i2s.h"
29
30#include "../codecs/ak4641.h"
31
32static struct snd_soc_jack hs_jack;
33
34/* Headphones jack detection DAPM pin */
35static struct snd_soc_jack_pin hs_jack_pin[] = {
36 {
37 .pin = "Headphone Jack",
38 .mask = SND_JACK_HEADPHONE,
39 },
40 {
41 .pin = "Speaker",
42 /* disable speaker when hp jack is inserted */
43 .mask = SND_JACK_HEADPHONE,
44 .invert = 1,
45 },
46};
47
48/* Headphones jack detection GPIO */
49static struct snd_soc_jack_gpio hs_jack_gpio = {
50 .gpio = GPIO75_HX4700_EARPHONE_nDET,
51 .invert = true,
52 .name = "hp-gpio",
53 .report = SND_JACK_HEADPHONE,
54 .debounce_time = 200,
55};
56
57/*
58 * iPAQ hx4700 uses I2S for capture and playback.
59 */
60static int hx4700_hw_params(struct snd_pcm_substream *substream,
61 struct snd_pcm_hw_params *params)
62{
63 struct snd_soc_pcm_runtime *rtd = substream->private_data;
64 struct snd_soc_dai *codec_dai = rtd->codec_dai;
65 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
66 int ret = 0;
67
Dmitry Artamonowc26f6422011-05-18 19:25:10 +040068 /* set the I2S system clock as output */
69 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
70 SND_SOC_CLOCK_OUT);
71 if (ret < 0)
72 return ret;
73
74 /* inform codec driver about clock freq *
75 * (PXA I2S always uses divider 256) */
76 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 256 * params_rate(params),
77 SND_SOC_CLOCK_IN);
78 if (ret < 0)
79 return ret;
80
81 return 0;
82}
83
84static struct snd_soc_ops hx4700_ops = {
85 .hw_params = hx4700_hw_params,
86};
87
88static int hx4700_spk_power(struct snd_soc_dapm_widget *w,
89 struct snd_kcontrol *k, int event)
90{
91 gpio_set_value(GPIO107_HX4700_SPK_nSD, !!SND_SOC_DAPM_EVENT_ON(event));
92 return 0;
93}
94
95static int hx4700_hp_power(struct snd_soc_dapm_widget *w,
96 struct snd_kcontrol *k, int event)
97{
98 gpio_set_value(GPIO92_HX4700_HP_DRIVER, !!SND_SOC_DAPM_EVENT_ON(event));
99 return 0;
100}
101
102/* hx4700 machine dapm widgets */
103static const struct snd_soc_dapm_widget hx4700_dapm_widgets[] = {
104 SND_SOC_DAPM_HP("Headphone Jack", hx4700_hp_power),
105 SND_SOC_DAPM_SPK("Speaker", hx4700_spk_power),
106 SND_SOC_DAPM_MIC("Built-in Microphone", NULL),
107};
108
109/* hx4700 machine audio_map */
110static const struct snd_soc_dapm_route hx4700_audio_map[] = {
111
112 /* Headphone connected to LOUT, ROUT */
113 {"Headphone Jack", NULL, "LOUT"},
114 {"Headphone Jack", NULL, "ROUT"},
115
116 /* Speaker connected to MOUT2 */
117 {"Speaker", NULL, "MOUT2"},
118
119 /* Microphone connected to MICIN */
120 {"MICIN", NULL, "Built-in Microphone"},
121 {"AIN", NULL, "MICOUT"},
122};
123
124/*
125 * Logic for a ak4641 as connected on a HP iPAQ hx4700
126 */
127static int hx4700_ak4641_init(struct snd_soc_pcm_runtime *rtd)
128{
129 struct snd_soc_codec *codec = rtd->codec;
130 struct snd_soc_dapm_context *dapm = &codec->dapm;
131 int err;
132
133 /* NC codec pins */
134 /* FIXME: is anything connected here? */
135 snd_soc_dapm_nc_pin(dapm, "MOUT1");
136 snd_soc_dapm_nc_pin(dapm, "MICEXT");
137 snd_soc_dapm_nc_pin(dapm, "AUX");
138
139 /* Jack detection API stuff */
140 err = snd_soc_jack_new(codec, "Headphone Jack",
141 SND_JACK_HEADPHONE, &hs_jack);
142 if (err)
143 return err;
144
145 err = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pin),
146 hs_jack_pin);
147 if (err)
148 return err;
149
150 err = snd_soc_jack_add_gpios(&hs_jack, 1, &hs_jack_gpio);
151
152 return err;
153}
154
Takashi Iwai16088cb2014-06-03 12:31:46 +0200155static int hx4700_card_remove(struct snd_soc_card *card)
Stephen Warrene1d4d3c2014-05-30 12:42:57 -0600156{
157 snd_soc_jack_free_gpios(&hs_jack, 1, &hs_jack_gpio);
158
159 return 0;
160}
161
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400162/* hx4700 digital audio interface glue - connects codec <--> CPU */
163static struct snd_soc_dai_link hx4700_dai = {
164 .name = "ak4641",
165 .stream_name = "AK4641",
166 .cpu_dai_name = "pxa2xx-i2s",
167 .codec_dai_name = "ak4641-hifi",
168 .platform_name = "pxa-pcm-audio",
169 .codec_name = "ak4641.0-0012",
170 .init = hx4700_ak4641_init,
Axel Lin673847c2011-12-20 16:13:26 +0800171 .dai_fmt = SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
172 SND_SOC_DAIFMT_CBS_CFS,
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400173 .ops = &hx4700_ops,
174};
175
176/* hx4700 audio machine driver */
177static struct snd_soc_card snd_soc_card_hx4700 = {
178 .name = "iPAQ hx4700",
Axel Lin561c6a12011-12-22 09:44:43 +0800179 .owner = THIS_MODULE,
Stephen Warrene1d4d3c2014-05-30 12:42:57 -0600180 .remove = hx4700_card_remove,
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400181 .dai_link = &hx4700_dai,
182 .num_links = 1,
183 .dapm_widgets = hx4700_dapm_widgets,
184 .num_dapm_widgets = ARRAY_SIZE(hx4700_dapm_widgets),
185 .dapm_routes = hx4700_audio_map,
186 .num_dapm_routes = ARRAY_SIZE(hx4700_audio_map),
187};
188
189static struct gpio hx4700_audio_gpios[] = {
190 { GPIO107_HX4700_SPK_nSD, GPIOF_OUT_INIT_HIGH, "SPK_POWER" },
191 { GPIO92_HX4700_HP_DRIVER, GPIOF_OUT_INIT_LOW, "EP_POWER" },
192};
193
Bill Pembertond7f1be82012-12-07 09:26:14 -0500194static int hx4700_audio_probe(struct platform_device *pdev)
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400195{
196 int ret;
197
198 if (!machine_is_h4700())
199 return -ENODEV;
200
201 ret = gpio_request_array(hx4700_audio_gpios,
202 ARRAY_SIZE(hx4700_audio_gpios));
203 if (ret)
204 return ret;
205
206 snd_soc_card_hx4700.dev = &pdev->dev;
207 ret = snd_soc_register_card(&snd_soc_card_hx4700);
208 if (ret)
Axel Lin497d4962011-12-12 11:26:00 +0800209 gpio_free_array(hx4700_audio_gpios,
210 ARRAY_SIZE(hx4700_audio_gpios));
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400211
Axel Lin497d4962011-12-12 11:26:00 +0800212 return ret;
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400213}
214
Bill Pembertond7f1be82012-12-07 09:26:14 -0500215static int hx4700_audio_remove(struct platform_device *pdev)
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400216{
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400217 snd_soc_unregister_card(&snd_soc_card_hx4700);
218
219 gpio_set_value(GPIO92_HX4700_HP_DRIVER, 0);
220 gpio_set_value(GPIO107_HX4700_SPK_nSD, 0);
221
222 gpio_free_array(hx4700_audio_gpios, ARRAY_SIZE(hx4700_audio_gpios));
223 return 0;
224}
225
226static struct platform_driver hx4700_audio_driver = {
227 .driver = {
228 .name = "hx4700-audio",
229 .owner = THIS_MODULE,
230 .pm = &snd_soc_pm_ops,
231 },
232 .probe = hx4700_audio_probe,
Bill Pembertond7f1be82012-12-07 09:26:14 -0500233 .remove = hx4700_audio_remove,
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400234};
235
Axel Lin2f702a12011-11-25 10:13:37 +0800236module_platform_driver(hx4700_audio_driver);
Dmitry Artamonowc26f6422011-05-18 19:25:10 +0400237
238MODULE_AUTHOR("Philipp Zabel");
239MODULE_DESCRIPTION("ALSA SoC iPAQ hx4700");
240MODULE_LICENSE("GPL");
241MODULE_ALIAS("platform:hx4700-audio");