summaryrefslogtreecommitdiff
path: root/sound/soc/codecs/c55audio.c
blob: de2798c099890326203974dbf28965b00be1c635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2014 Motorola, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */

#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <sound/soc.h>

/* codec private data */
struct soc_c55_private {
	int mic_bias1_en_gpio;
	int mic_bias3_en_gpio;
};

static const struct snd_soc_dapm_widget soc_c55_dapm_widgets[] = {
	SND_SOC_DAPM_ADC("ADC", "Voice Capture", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_INPUT("MIC"),
};

static const struct snd_soc_dapm_route soc_c55_audio_map[] = {
	{"ADC", NULL, "MIC"},
};

static const struct of_device_id soc_c55_of_match[] = {
	{.compatible = "mot,soc-c55", },
	{ },
};
MODULE_DEVICE_TABLE(of, soc_c55_of_match);

static int soc_codec_c55_set_bias_level(struct snd_soc_codec *codec,
				  enum snd_soc_bias_level level)
{
	struct soc_c55_private *priv = snd_soc_codec_get_drvdata(codec);

	switch (level) {
	case SND_SOC_BIAS_ON:
		break;
	case SND_SOC_BIAS_PREPARE:
		break;
	case SND_SOC_BIAS_STANDBY:
		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
			/* Enable mic biases */
			gpio_set_value(priv->mic_bias1_en_gpio, 1);
			gpio_set_value(priv->mic_bias3_en_gpio, 1);
		}
		break;
	case SND_SOC_BIAS_OFF:
		/* Disable mic biases */
		gpio_set_value(priv->mic_bias1_en_gpio, 0);
		gpio_set_value(priv->mic_bias3_en_gpio, 0);
		break;
	}
	codec->dapm.bias_level = level;

	return 0;
}

static struct snd_soc_dai_driver soc_codec_c55_dai = {
	.name = "c55-voice",
	.capture = {
		.stream_name = "Voice Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = SNDRV_PCM_RATE_16000,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
};

static int soc_codec_c55_probe(struct snd_soc_codec *codec)
{
	struct soc_c55_private *c55;
	int gpio;
	int ret;

	c55 = devm_kzalloc(codec->dev, sizeof(struct soc_c55_private),
			       GFP_KERNEL);
	if (c55 == NULL) {
		dev_err(codec->dev, "Can not allocate memory\n");
		return -ENOMEM;
	}
	snd_soc_codec_set_drvdata(codec, c55);

#ifdef CONFIG_OF
	if (of_match_device(soc_c55_of_match, codec->dev)) {
		gpio = of_get_named_gpio(codec->dev->of_node,
			"mot,mic_bias1_en", 0);
		ret = (gpio < 0) ? -ENODEV : gpio_request(gpio, "mic_bias1_en");
		if (ret) {
			dev_err(codec->dev,
				"Failed acquiring Mic Bias 1 En GPIO-%d (%d)\n",
				gpio, ret);
			return ret;
		}
		gpio_direction_output(gpio, 0);
		c55->mic_bias1_en_gpio = gpio;

		gpio = of_get_named_gpio(codec->dev->of_node,
			"mot,mic_bias3_en", 0);
		ret = (gpio < 0) ? -ENODEV : gpio_request(gpio, "mic_bias3_en");
		if (ret) {
			dev_err(codec->dev,
				"Failed acquiring Mic Bias 3 En GPIO-%d (%d)\n",
				gpio, ret);
			return ret;
		}
		gpio_direction_output(gpio, 0);
		c55->mic_bias3_en_gpio = gpio;
	}
#endif

	return 0;
}

static int soc_codec_c55_remove(struct snd_soc_codec *codec)
{
	struct soc_c55_private *priv = snd_soc_codec_get_drvdata(codec);

	soc_codec_c55_set_bias_level(codec, SND_SOC_BIAS_OFF);

	if (priv) {
		gpio_free(priv->mic_bias1_en_gpio);
		gpio_free(priv->mic_bias3_en_gpio);
	}

	return 0;
}

static struct snd_soc_codec_driver soc_codec_c55 = {
	.probe = soc_codec_c55_probe,
	.remove = soc_codec_c55_remove,
	.set_bias_level = soc_codec_c55_set_bias_level,
	.idle_bias_off = true,
	.dapm_widgets = soc_c55_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(soc_c55_dapm_widgets),
	.dapm_routes = soc_c55_audio_map,
	.num_dapm_routes = ARRAY_SIZE(soc_c55_audio_map),
};

static int soc_c55_probe(struct platform_device *pdev)
{
	return snd_soc_register_codec(&pdev->dev, &soc_codec_c55,
		&soc_codec_c55_dai, 1);
}

static int soc_c55_remove(struct platform_device *pdev)
{
	snd_soc_unregister_codec(&pdev->dev);
	return 0;
}

static struct platform_driver soc_c55_driver = {
	.probe		= soc_c55_probe,
	.remove		= soc_c55_remove,
	.driver		= {
		.name	= "soc-c55",
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(soc_c55_of_match),
	},
};

module_platform_driver(soc_c55_driver);

MODULE_AUTHOR("Motorola");
MODULE_DESCRIPTION("ALSA SoC c55 audio codec driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:soc-c55");