PageRenderTime 34ms CodeModel.GetById 15ms app.highlight 17ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/arm/mach-msm/qdsp6v2/snddev_ecodec.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase
C | 387 lines | 292 code | 75 blank | 20 comment | 27 complexity | 3280911ec1e952b9b49270adefe3cc85 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  2 *
  3 * This program is free software; you can redistribute it and/or modify
  4 * it under the terms of the GNU General Public License version 2 and
  5 * only version 2 as published by the Free Software Foundation.
  6 *
  7 * This program is distributed in the hope that it will be useful,
  8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10 * GNU General Public License for more details.
 11 *
 12 * You should have received a copy of the GNU General Public License
 13 * along with this program; if not, write to the Free Software
 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 15 * 02110-1301, USA.
 16 *
 17 */
 18#include <linux/module.h>
 19#include <linux/platform_device.h>
 20#include <linux/clk.h>
 21#include <linux/err.h>
 22#include <linux/slab.h>
 23#include <linux/gpio.h>
 24#include <linux/delay.h>
 25#include <linux/io.h>
 26#include <asm/uaccess.h>
 27#include <asm/io.h>
 28#include <mach/clk.h>
 29#include <mach/qdsp6v2/audio_dev_ctl.h>
 30#include <mach/qdsp6v2/apr_audio.h>
 31#include "snddev_ecodec.h"
 32#include <mach/qdsp6v2/q6afe.h>
 33
 34#define ECODEC_SAMPLE_RATE 8000
 35
 36/* Context for each external codec device */
 37struct snddev_ecodec_state {
 38	struct snddev_ecodec_data *data;
 39	u32 sample_rate;
 40};
 41
 42/* Global state for the driver */
 43struct snddev_ecodec_drv_state {
 44	struct mutex dev_lock;
 45	int ref_cnt;		/* ensure one rx device at a time */
 46	struct clk *ecodec_clk;
 47};
 48
 49static struct snddev_ecodec_drv_state snddev_ecodec_drv;
 50
 51struct aux_pcm_state {
 52	unsigned int dout;
 53	unsigned int din;
 54	unsigned int syncout;
 55	unsigned int clkin_a;
 56};
 57
 58static struct aux_pcm_state the_aux_pcm_state;
 59
 60static int aux_pcm_gpios_request(void)
 61{
 62	int rc = 0;
 63
 64	pr_debug("%s\n", __func__);
 65	rc = gpio_request(the_aux_pcm_state.dout, "AUX PCM DOUT");
 66	if (rc < 0) {
 67		pr_err("%s: GPIO request for AUX PCM DOUT failed\n", __func__);
 68		return rc;
 69	}
 70
 71	rc = gpio_request(the_aux_pcm_state.din, "AUX PCM DIN");
 72	if (rc < 0) {
 73		pr_err("%s: GPIO request for AUX PCM DIN failed\n", __func__);
 74		gpio_free(the_aux_pcm_state.dout);
 75		return rc;
 76	}
 77
 78	rc = gpio_request(the_aux_pcm_state.syncout, "AUX PCM SYNC OUT");
 79	if (rc < 0) {
 80		pr_err("%s: GPIO request for AUX PCM SYNC OUT failed\n",
 81				__func__);
 82		gpio_free(the_aux_pcm_state.dout);
 83		gpio_free(the_aux_pcm_state.din);
 84		return rc;
 85	}
 86
 87	rc = gpio_request(the_aux_pcm_state.clkin_a, "AUX PCM CLKIN A");
 88	if (rc < 0) {
 89		pr_err("%s: GPIO request for AUX PCM CLKIN A failed\n",
 90				__func__);
 91		gpio_free(the_aux_pcm_state.dout);
 92		gpio_free(the_aux_pcm_state.din);
 93		gpio_free(the_aux_pcm_state.syncout);
 94		return rc;
 95	}
 96
 97	return rc;
 98}
 99
100static void aux_pcm_gpios_free(void)
101{
102	pr_debug("%s\n", __func__);
103	gpio_free(the_aux_pcm_state.dout);
104	gpio_free(the_aux_pcm_state.din);
105	gpio_free(the_aux_pcm_state.syncout);
106	gpio_free(the_aux_pcm_state.clkin_a);
107}
108
109static int get_aux_pcm_gpios(struct platform_device *pdev)
110{
111	int rc = 0;
112	struct resource *res;
113
114	/* Claim all of the GPIOs. */
115	res = platform_get_resource_byname(pdev, IORESOURCE_IO, "aux_pcm_dout");
116	if (!res) {
117		pr_err("%s: failed to get gpio AUX PCM DOUT\n", __func__);
118		return -ENODEV;
119	}
120
121	the_aux_pcm_state.dout = res->start;
122
123	res = platform_get_resource_byname(pdev, IORESOURCE_IO, "aux_pcm_din");
124	if (!res) {
125		pr_err("%s: failed to get gpio AUX PCM DIN\n", __func__);
126		return -ENODEV;
127	}
128
129	the_aux_pcm_state.din = res->start;
130
131	res = platform_get_resource_byname(pdev, IORESOURCE_IO,
132					   "aux_pcm_syncout");
133	if (!res) {
134		pr_err("%s: failed to get gpio AUX PCM SYNC OUT\n", __func__);
135		return -ENODEV;
136	}
137
138	the_aux_pcm_state.syncout = res->start;
139
140	res = platform_get_resource_byname(pdev, IORESOURCE_IO,
141					   "aux_pcm_clkin_a");
142	if (!res) {
143		pr_err("%s: failed to get gpio AUX PCM CLKIN A\n", __func__);
144		return -ENODEV;
145	}
146
147	the_aux_pcm_state.clkin_a = res->start;
148
149	return rc;
150}
151
152static int aux_pcm_probe(struct platform_device *pdev)
153{
154	int rc = 0;
155
156	rc = get_aux_pcm_gpios(pdev);
157	if (rc < 0) {
158		pr_err("%s: GPIO configuration failed\n", __func__);
159		return -ENODEV;
160	}
161	return rc;
162}
163
164static struct platform_driver aux_pcm_driver = {
165	.probe = aux_pcm_probe,
166	.driver = { .name = "msm_aux_pcm"}
167};
168
169static int snddev_ecodec_open(struct msm_snddev_info *dev_info)
170{
171	int rc;
172	struct snddev_ecodec_drv_state *drv = &snddev_ecodec_drv;
173	union afe_port_config afe_config;
174
175	pr_debug("%s\n", __func__);
176
177	mutex_lock(&drv->dev_lock);
178
179	if (dev_info->opened) {
180		pr_err("%s: ERROR: %s already opened\n", __func__,
181				dev_info->name);
182		mutex_unlock(&drv->dev_lock);
183		return -EBUSY;
184	}
185
186	if (drv->ref_cnt != 0) {
187		pr_debug("%s: opened %s\n", __func__, dev_info->name);
188		drv->ref_cnt++;
189		mutex_unlock(&drv->dev_lock);
190		return 0;
191	}
192
193	pr_info("%s: opening %s\n", __func__, dev_info->name);
194
195	rc = aux_pcm_gpios_request();
196	if (rc < 0) {
197		pr_err("%s: GPIO request failed\n", __func__);
198		return rc;
199	}
200
201	clk_reset(drv->ecodec_clk, CLK_RESET_ASSERT);
202
203	afe_config.pcm.mode = AFE_PCM_CFG_MODE_PCM;
204	afe_config.pcm.sync = AFE_PCM_CFG_SYNC_INT;
205	afe_config.pcm.frame = AFE_PCM_CFG_FRM_256BPF;
206	afe_config.pcm.quant = AFE_PCM_CFG_QUANT_LINEAR_NOPAD;
207	afe_config.pcm.slot = 0;
208	afe_config.pcm.data = AFE_PCM_CFG_CDATAOE_MASTER;
209
210	rc = afe_open(PCM_RX, &afe_config, ECODEC_SAMPLE_RATE);
211	if (rc < 0) {
212		pr_err("%s: afe open failed for PCM_RX\n", __func__);
213		goto err_rx_afe;
214	}
215
216	rc = afe_open(PCM_TX, &afe_config, ECODEC_SAMPLE_RATE);
217	if (rc < 0) {
218		pr_err("%s: afe open failed for PCM_TX\n", __func__);
219		goto err_tx_afe;
220	}
221
222	rc = clk_set_rate(drv->ecodec_clk, 2048000);
223	if (rc < 0) {
224		pr_err("%s: clk_set_rate failed\n", __func__);
225		goto err_clk;
226	}
227
228	clk_enable(drv->ecodec_clk);
229
230	clk_reset(drv->ecodec_clk, CLK_RESET_DEASSERT);
231
232	drv->ref_cnt++;
233	mutex_unlock(&drv->dev_lock);
234
235	return 0;
236
237err_clk:
238	afe_close(PCM_TX);
239err_tx_afe:
240	afe_close(PCM_RX);
241err_rx_afe:
242	aux_pcm_gpios_free();
243	mutex_unlock(&drv->dev_lock);
244	return -ENODEV;
245}
246
247int snddev_ecodec_close(struct msm_snddev_info *dev_info)
248{
249	struct snddev_ecodec_drv_state *drv = &snddev_ecodec_drv;
250
251	pr_debug("%s: closing %s\n", __func__, dev_info->name);
252
253	mutex_lock(&drv->dev_lock);
254
255	if (!dev_info->opened) {
256		pr_err("%s: ERROR: %s is not opened\n", __func__,
257				dev_info->name);
258		mutex_unlock(&drv->dev_lock);
259		return -EPERM;
260	}
261
262	drv->ref_cnt--;
263
264	if (drv->ref_cnt == 0) {
265
266		pr_info("%s: closing all devices\n", __func__);
267
268		clk_disable(drv->ecodec_clk);
269		aux_pcm_gpios_free();
270
271		afe_close(PCM_RX);
272		afe_close(PCM_TX);
273	}
274
275	mutex_unlock(&drv->dev_lock);
276
277	return 0;
278}
279
280int snddev_ecodec_set_freq(struct msm_snddev_info *dev_info, u32 rate)
281{
282	int rc = 0;
283
284	if (!dev_info) {
285		rc = -EINVAL;
286		goto error;
287	}
288	return ECODEC_SAMPLE_RATE;
289
290error:
291	return rc;
292}
293
294static int snddev_ecodec_probe(struct platform_device *pdev)
295{
296	int rc = 0;
297	struct snddev_ecodec_data *pdata;
298	struct msm_snddev_info *dev_info;
299	struct snddev_ecodec_state *ecodec;
300
301	if (!pdev || !pdev->dev.platform_data) {
302		printk(KERN_ALERT "Invalid caller\n");
303		rc = -1;
304		goto error;
305	}
306	pdata = pdev->dev.platform_data;
307
308	ecodec = kzalloc(sizeof(struct snddev_ecodec_state), GFP_KERNEL);
309	if (!ecodec) {
310		rc = -ENOMEM;
311		goto error;
312	}
313
314	dev_info = kzalloc(sizeof(struct msm_snddev_info), GFP_KERNEL);
315	if (!dev_info) {
316		kfree(ecodec);
317		rc = -ENOMEM;
318		goto error;
319	}
320
321	dev_info->name = pdata->name;
322	dev_info->copp_id = pdata->copp_id;
323	dev_info->private_data = (void *)ecodec;
324	dev_info->dev_ops.open = snddev_ecodec_open;
325	dev_info->dev_ops.close = snddev_ecodec_close;
326	dev_info->dev_ops.set_freq = snddev_ecodec_set_freq;
327	dev_info->dev_ops.enable_sidetone = NULL;
328	dev_info->capability = pdata->capability;
329	dev_info->opened = 0;
330
331	msm_snddev_register(dev_info);
332
333	ecodec->data = pdata;
334	ecodec->sample_rate = ECODEC_SAMPLE_RATE;	/* Default to 8KHz */
335error:
336	return rc;
337}
338
339struct platform_driver snddev_ecodec_driver = {
340	.probe = snddev_ecodec_probe,
341	.driver = {.name = "msm_snddev_ecodec"}
342};
343
344int __init snddev_ecodec_init(void)
345{
346	int rc = 0;
347	struct snddev_ecodec_drv_state *drv = &snddev_ecodec_drv;
348
349	mutex_init(&drv->dev_lock);
350	drv->ref_cnt = 0;
351
352	drv->ecodec_clk = clk_get(NULL, "pcm_clk");
353	if (IS_ERR(drv->ecodec_clk)) {
354		pr_err("%s: could not get pcm_clk\n", __func__);
355		return PTR_ERR(drv->ecodec_clk);
356	}
357
358	rc = platform_driver_register(&aux_pcm_driver);
359	if (IS_ERR_VALUE(rc)) {
360		pr_err("%s: platform_driver_register for aux pcm failed\n",
361				__func__);
362		goto error_aux_pcm_platform_driver;
363	}
364
365	rc = platform_driver_register(&snddev_ecodec_driver);
366	if (IS_ERR_VALUE(rc)) {
367		pr_err("%s: platform_driver_register for ecodec failed\n",
368				__func__);
369		goto error_ecodec_platform_driver;
370	}
371
372	return 0;
373
374error_ecodec_platform_driver:
375	platform_driver_unregister(&aux_pcm_driver);
376error_aux_pcm_platform_driver:
377	clk_put(drv->ecodec_clk);
378
379	pr_err("%s: encounter error\n", __func__);
380	return -ENODEV;
381}
382
383device_initcall(snddev_ecodec_init);
384
385MODULE_DESCRIPTION("ECodec Sound Device driver");
386MODULE_VERSION("1.0");
387MODULE_LICENSE("GPL v2");