1 From 34b6a12e70e8260cf273dab7e618602df0c1bebe Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 24 Apr 2010 12:37:47 +0200
4 Subject: [PATCH] Add N516 sound SoC board driver
7 sound/soc/jz4740/Kconfig | 8 ++
8 sound/soc/jz4740/Makefile | 2 +
9 sound/soc/jz4740/n516.c | 303 +++++++++++++++++++++++++++++++++++++++++++++
10 3 files changed, 313 insertions(+), 0 deletions(-)
11 create mode 100644 sound/soc/jz4740/n516.c
13 diff --git a/sound/soc/jz4740/Kconfig b/sound/soc/jz4740/Kconfig
14 index e546c30..fea440d 100644
15 --- a/sound/soc/jz4740/Kconfig
16 +++ b/sound/soc/jz4740/Kconfig
17 @@ -19,3 +19,11 @@ config SND_JZ4740_SOC_QI_LB60
18 select SND_SOC_JZCODEC
20 Say Y if you want to add support for SoC audio of internal codec on Ingenic Jz4740 QI_LB60 board.
22 +config SND_JZ4740_SOC_N516
23 + tristate "SoC Audio support for Hanvon N516 eBook reader"
24 + depends on SND_JZ4740_SOC && JZ4740_N516
25 + select SND_JZ4740_SOC_I2S
26 + select SND_SOC_JZCODEC
28 + Say Y if you want to enable support for SoC audio on the Hanvon N516.
29 diff --git a/sound/soc/jz4740/Makefile b/sound/soc/jz4740/Makefile
30 index be873c1..b64d912 100644
31 --- a/sound/soc/jz4740/Makefile
32 +++ b/sound/soc/jz4740/Makefile
33 @@ -9,5 +9,7 @@ obj-$(CONFIG_SND_JZ4740_SOC_I2S) += snd-soc-jz4740-i2s.o
35 # Jz4740 Machine Support
36 snd-soc-qi-lb60-objs := qi_lb60.o
37 +snd-soc-n516-objs := n516.o
39 obj-$(CONFIG_SND_JZ4740_SOC_QI_LB60) += snd-soc-qi-lb60.o
40 +obj-$(CONFIG_SND_JZ4740_SOC_N516) += snd-soc-n516.o
41 diff --git a/sound/soc/jz4740/n516.c b/sound/soc/jz4740/n516.c
43 index 0000000..9cb51c2
45 +++ b/sound/soc/jz4740/n516.c
48 + * Copyright (C) 2009, Yauhen Kharuzhy <jekhor@gmail.com>
49 + * OpenInkpot project
50 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
52 + * This program is free software; you can redistribute it and/or modify
53 + * it under the terms of the GNU General Public License version 2 as
54 + * published by the Free Software Foundation.
56 + * You should have received a copy of the GNU General Public License along
57 + * with this program; if not, write to the Free Software Foundation, Inc.,
58 + * 675 Mass Ave, Cambridge, MA 02139, USA.
62 +#include <linux/module.h>
63 +#include <linux/interrupt.h>
64 +#include <linux/platform_device.h>
65 +#include <sound/core.h>
66 +#include <sound/pcm.h>
67 +#include <sound/soc.h>
68 +#include <sound/soc-dapm.h>
69 +#include <sound/jack.h>
70 +#include <linux/gpio.h>
71 +#include <linux/workqueue.h>
73 +#include "../codecs/jzcodec.h"
74 +#include "jz4740-pcm.h"
75 +#include "jz4740-i2s.h"
77 +#include <asm/mach-jz4740/board-n516.h>
80 + N516_SPEAKER_AUTO = 0,
81 + N516_SPEAKER_OFF = 1,
82 + N516_SPEAKER_ON = 2,
85 +static int n516_speaker_mode;
86 +static struct snd_soc_codec *n516_codec;
87 +static struct work_struct n516_headphone_work;
89 +static void n516_ext_control(void)
94 + switch (n516_speaker_mode) {
95 + case N516_SPEAKER_ON:
96 + snd_soc_dapm_enable_pin(n516_codec, "Speaker");
98 + case N516_SPEAKER_OFF:
99 + snd_soc_dapm_disable_pin(n516_codec, "Speaker");
101 + case N516_SPEAKER_AUTO:
102 + if (snd_soc_dapm_get_pin_status(n516_codec, "Headphone"))
103 + snd_soc_dapm_disable_pin(n516_codec, "Speaker");
105 + snd_soc_dapm_enable_pin(n516_codec, "Speaker");
111 + /* signal a DAPM event */
112 + snd_soc_dapm_sync(n516_codec);
115 +static int n516_speaker_event(struct snd_soc_dapm_widget *widget,
116 + struct snd_kcontrol *ctrl, int event)
118 + int on = !SND_SOC_DAPM_EVENT_OFF(event);
120 + gpio_set_value(GPIO_SPEAKER_ENABLE, on);
125 +static void n516_headphone_event_work(struct work_struct *work)
127 + n516_ext_control();
130 +static int n516_headphone_event(struct snd_soc_dapm_widget *widget,
131 + struct snd_kcontrol *ctrl, int event)
133 + /* We can't call soc_dapm_sync from a event handler */
134 + if (event & (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD))
135 + schedule_work(&n516_headphone_work);
139 +static const struct snd_soc_dapm_widget n516_widgets[] = {
140 + SND_SOC_DAPM_SPK("Speaker", n516_speaker_event),
141 + SND_SOC_DAPM_HP("Headphone", n516_headphone_event),
142 + SND_SOC_DAPM_MIC("Mic", NULL),
145 +static const struct snd_soc_dapm_route n516_routes[] = {
146 + {"Mic", NULL, "MIC"},
147 + {"Speaker", NULL, "LOUT"},
148 + {"Speaker", NULL, "ROUT"},
149 + {"Headphone", NULL, "LOUT"},
150 + {"Headphone", NULL, "ROUT"},
153 +static const char *n516_speaker_modes[] = {"Auto", "Off", "On"};
154 +static const struct soc_enum n516_speaker_mode_enum =
155 + SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(n516_speaker_modes), n516_speaker_modes);
157 +static int n516_get_speaker_mode(struct snd_kcontrol *kcontrol,
158 + struct snd_ctl_elem_value *ucontrol)
160 + ucontrol->value.integer.value[0] = n516_speaker_mode;
164 +static int n516_set_speaker_mode(struct snd_kcontrol *kcontrol,
165 + struct snd_ctl_elem_value *ucontrol)
167 + if (n516_speaker_mode == ucontrol->value.integer.value[0])
170 + n516_speaker_mode = ucontrol->value.integer.value[0];
171 + n516_ext_control();
175 +static const struct snd_kcontrol_new n516_controls[] = {
176 + SOC_ENUM_EXT("Speaker Function", n516_speaker_mode_enum,
177 + n516_get_speaker_mode, n516_set_speaker_mode),
180 +#define N516_DAIFMT (SND_SOC_DAIFMT_I2S | \
181 + SND_SOC_DAIFMT_NB_NF | \
182 + SND_SOC_DAIFMT_CBM_CFM)
184 +static int n516_codec_init(struct snd_soc_codec *codec)
187 + struct snd_soc_dai *cpu_dai = codec->socdev->card->dai_link->cpu_dai;
188 + struct snd_soc_dai *codec_dai = codec->socdev->card->dai_link->codec_dai;
190 + n516_codec = codec;
192 + snd_soc_dapm_nc_pin(codec, "LIN");
193 + snd_soc_dapm_nc_pin(codec, "RIN");
195 + ret = snd_soc_dai_set_fmt(codec_dai, N516_DAIFMT);
197 + dev_err(codec->dev, "Failed to set codec dai format: %d\n", ret);
201 + ret = snd_soc_dai_set_fmt(cpu_dai, N516_DAIFMT);
203 + dev_err(codec->dev, "Failed to set cpu dai format: %d\n", ret);
207 + ret = snd_soc_dai_set_sysclk(codec_dai, JZCODEC_SYSCLK, 111,
210 + dev_err(codec->dev, "Failed to set codec dai sysclk: %d\n", ret);
214 + ret = snd_soc_add_controls(codec, n516_controls,
215 + ARRAY_SIZE(n516_controls));
217 + dev_err(codec->dev, "Failed to add controls: %d\n", ret);
222 + ret = snd_soc_dapm_new_controls(codec, n516_widgets,
223 + ARRAY_SIZE(n516_widgets));
225 + dev_err(codec->dev, "Failed to add dapm controls: %d\n", ret);
229 + ret = snd_soc_dapm_add_routes(codec, n516_routes, ARRAY_SIZE(n516_routes));
231 + dev_err(codec->dev, "Failed to add dapm routes: %d\n", ret);
235 + snd_soc_dapm_sync(codec);
240 +static struct snd_soc_dai_link n516_dai = {
241 + .name = "jz-codec",
242 + .stream_name = "JZCODEC",
243 + .cpu_dai = &jz4740_i2s_dai,
244 + .codec_dai = &jz_codec_dai,
245 + .init = n516_codec_init,
248 +static struct snd_soc_card n516_card = {
250 + .dai_link = &n516_dai,
252 + .platform = &jz4740_soc_platform,
255 +static struct snd_soc_device n516_snd_devdata = {
256 + .card = &n516_card,
257 + .codec_dev = &soc_codec_dev_jzcodec,
260 +static struct platform_device *n516_snd_device;
262 +static struct snd_soc_jack n516_hp_jack;
264 +static struct snd_soc_jack_pin n516_hp_pin = {
265 + .pin = "Headphone",
266 + .mask = SND_JACK_HEADPHONE,
269 +static struct snd_soc_jack_gpio n516_hp_gpio = {
270 + .gpio = GPIO_HPHONE_DETECT,
271 + .name = "Headphone detect",
272 + .report = SND_JACK_HEADPHONE,
273 + .debounce_time = 100,
276 +static int __init n516_add_headphone_jack(void)
280 + ret = snd_soc_jack_new(&n516_card, "Headphone jack",
281 + SND_JACK_HEADPHONE, &n516_hp_jack);
285 + ret = snd_soc_jack_add_pins(&n516_hp_jack, 1, &n516_hp_pin);
289 + ret = snd_soc_jack_add_gpios(&n516_hp_jack, 1, &n516_hp_gpio);
294 +static int __init n516_init(void)
298 + n516_snd_device = platform_device_alloc("soc-audio", -1);
300 + if (!n516_snd_device)
303 + ret = gpio_request(GPIO_SPEAKER_ENABLE, "Speaker enable");
305 + pr_err("n516 snd: Failed to request SPEAKER_ENABLE GPIO(%d): %d\n",
306 + GPIO_SPEAKER_ENABLE, ret);
307 + goto err_device_put;
310 + gpio_direction_output(GPIO_SPEAKER_ENABLE, 0);
311 + INIT_WORK(&n516_headphone_work, n516_headphone_event_work);
313 + platform_set_drvdata(n516_snd_device, &n516_snd_devdata);
314 + n516_snd_devdata.dev = &n516_snd_device->dev;
315 + ret = platform_device_add(n516_snd_device);
317 + pr_err("n516 snd: Failed to add snd soc device: %d\n", ret);
318 + goto err_unset_pdata;
321 + ret = n516_add_headphone_jack();
322 + /* We can live without it, so just print a warning */
324 + pr_warning("n516 snd: Failed to initalise headphone jack: %d\n", ret);
329 + platform_set_drvdata(n516_snd_device, NULL);
330 +/*err_gpio_free_speaker:*/
331 + gpio_free(GPIO_SPEAKER_ENABLE);
333 + platform_device_put(n516_snd_device);
337 +module_init(n516_init);
339 +static void __exit n516_exit(void)
341 + snd_soc_jack_free_gpios(&n516_hp_jack, 1, &n516_hp_gpio);
342 + gpio_free(GPIO_SPEAKER_ENABLE);
343 + platform_device_unregister(n516_snd_device);
345 +module_exit(n516_exit);
347 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
348 +MODULE_DESCRIPTION("ALSA SoC N516 Audio support");
349 +MODULE_LICENSE("GPL v2");