/arch/arm/mach-fsm/qdsp5v2/snddev_mi2s.c
C | 410 lines | 304 code | 82 blank | 24 comment | 45 complexity | c2d75d0dd4edc913dd1404a07dfa0fd7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
1/* Copyright (c) 2010, 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/io.h>
23#include <linux/uaccess.h>
24
25#include <mach/qdsp5v2/audio_dev_ctl.h>
26#include <mach/qdsp5v2/audio_interct.h>
27#include <mach/qdsp5v2/mi2s.h>
28#include <mach/qdsp5v2/afe.h>
29#include <mach/debug_mm.h>
30#include <mach/qdsp5v2/snddev_mi2s.h>
31
32/* Global state for the driver */
33struct snddev_mi2s_drv_state {
34 struct clk *mclk;
35 struct clk *sclk;
36 struct mutex lock;
37 u8 sd_lines_used;
38 u8 clocks_enabled;
39};
40
41static struct snddev_mi2s_drv_state snddev_mi2s_drv;
42
43static int snddev_mi2s_open_tx(struct msm_snddev_info *dev_info)
44{
45 u8 channels;
46 struct msm_afe_config afe_config;
47 int rc;
48 struct snddev_mi2s_data *snddev_mi2s_data = dev_info->private_data;
49
50 MM_DBG("%s: channel_mode = %u sd_line_mask = 0x%x "
51 "default_sample_rate = %u\n", __func__,
52 snddev_mi2s_data->channel_mode, snddev_mi2s_data->sd_lines,
53 snddev_mi2s_data->default_sample_rate);
54
55 if (snddev_mi2s_data->channel_mode == 2) {
56 channels = MI2S_CHAN_STEREO;
57 } else {
58 MM_ERR("%s: Invalid number of channels = %u\n", __func__,
59 snddev_mi2s_data->channel_mode);
60 return -EINVAL;
61 }
62
63 /* Set MI2S */
64 mi2s_set_hdmi_input_path(channels, WT_16_BIT,
65 snddev_mi2s_data->sd_lines);
66
67 afe_config.sample_rate = snddev_mi2s_data->default_sample_rate / 1000;
68 afe_config.channel_mode = snddev_mi2s_data->channel_mode;
69 afe_config.volume = AFE_VOLUME_UNITY;
70 rc = afe_enable(AFE_HW_PATH_MI2S_TX, &afe_config);
71
72 if (IS_ERR_VALUE(rc)) {
73 MM_ERR("%s: afe_enable failed for AFE_HW_PATH_MI2S_TX "
74 "rc = %d\n", __func__, rc);
75 return -ENODEV;
76 }
77
78 /* Enable audio path */
79 if (snddev_mi2s_data->route)
80 snddev_mi2s_data->route();
81
82 return 0;
83}
84
85static int snddev_mi2s_open_rx(struct msm_snddev_info *dev_info)
86{
87 int rc;
88 struct msm_afe_config afe_config;
89 u8 channels;
90 struct snddev_mi2s_data *snddev_mi2s_data = dev_info->private_data;
91
92 MM_DBG("%s: channel_mode = %u sd_line_mask = 0x%x "
93 "default_sample_rate = %u\n", __func__,
94 snddev_mi2s_data->channel_mode, snddev_mi2s_data->sd_lines,
95 snddev_mi2s_data->default_sample_rate);
96
97 if (snddev_mi2s_data->channel_mode == 2)
98 channels = MI2S_CHAN_STEREO;
99 else if (snddev_mi2s_data->channel_mode == 4)
100 channels = MI2S_CHAN_4CHANNELS;
101 else if (snddev_mi2s_data->channel_mode == 6)
102 channels = MI2S_CHAN_6CHANNELS;
103 else if (snddev_mi2s_data->channel_mode == 8)
104 channels = MI2S_CHAN_8CHANNELS;
105 else
106 channels = MI2S_CHAN_MONO_RAW;
107
108 /* Set MI2S */
109 mi2s_set_hdmi_output_path(channels, WT_16_BIT,
110 snddev_mi2s_data->sd_lines);
111
112 /* Start AFE */
113 afe_config.sample_rate = snddev_mi2s_data->default_sample_rate / 1000;
114 afe_config.channel_mode = snddev_mi2s_data->channel_mode;
115 afe_config.volume = AFE_VOLUME_UNITY;
116 rc = afe_enable(AFE_HW_PATH_MI2S_RX, &afe_config);
117
118 if (IS_ERR_VALUE(rc)) {
119 MM_ERR("%s: encounter error\n", __func__);
120 return -ENODEV;
121 }
122
123 /* Enable audio path */
124 if (snddev_mi2s_data->route)
125 snddev_mi2s_data->route();
126
127 MM_DBG("%s: enabled %s \n", __func__, snddev_mi2s_data->name);
128
129 return 0;
130}
131
132static int snddev_mi2s_open(struct msm_snddev_info *dev_info)
133{
134 int rc = 0;
135 struct snddev_mi2s_drv_state *drv = &snddev_mi2s_drv;
136 u32 dir;
137 struct snddev_mi2s_data *snddev_mi2s_data = dev_info->private_data;
138
139 if (!dev_info) {
140 MM_ERR("%s: msm_snddev_info is null \n", __func__);
141 return -EINVAL;
142 }
143
144 mutex_lock(&drv->lock);
145
146 if (drv->sd_lines_used & snddev_mi2s_data->sd_lines) {
147 MM_ERR("%s: conflict in SD data line. can not use the device\n",
148 __func__);
149 mutex_unlock(&drv->lock);
150 return -EBUSY;
151 }
152
153 if (!drv->clocks_enabled) {
154
155 rc = mi2s_config_clk_gpio();
156 if (rc) {
157 MM_ERR("%s: mi2s GPIO config failed for %s\n",
158 __func__, snddev_mi2s_data->name);
159 mutex_unlock(&drv->lock);
160 return -EIO;
161 }
162 clk_enable(drv->mclk);
163 clk_enable(drv->sclk);
164 drv->clocks_enabled = 1;
165 MM_DBG("%s: clks enabled \n", __func__);
166 } else
167 MM_DBG("%s: clks already enabled \n", __func__);
168
169 if (snddev_mi2s_data->capability & SNDDEV_CAP_RX) {
170
171 dir = DIR_RX;
172 rc = mi2s_config_data_gpio(dir, snddev_mi2s_data->sd_lines);
173
174 if (rc) {
175 rc = -EIO;
176 MM_ERR("%s: mi2s GPIO config failed for %s\n",
177 __func__, snddev_mi2s_data->name);
178 goto mi2s_data_gpio_failure;
179 }
180
181 MM_DBG("%s: done gpio config rx SD lines\n", __func__);
182
183 rc = snddev_mi2s_open_rx(dev_info);
184
185 if (IS_ERR_VALUE(rc)) {
186 MM_ERR(" snddev_mi2s_open_rx failed \n");
187 goto mi2s_cleanup_open;
188 }
189
190 drv->sd_lines_used |= snddev_mi2s_data->sd_lines;
191
192 MM_DBG("%s: sd_lines_used = 0x%x\n", __func__,
193 drv->sd_lines_used);
194 mutex_unlock(&drv->lock);
195
196 } else {
197 dir = DIR_TX;
198 rc = mi2s_config_data_gpio(dir, snddev_mi2s_data->sd_lines);
199
200 if (rc) {
201 rc = -EIO;
202 MM_ERR("%s: mi2s GPIO config failed for %s\n",
203 __func__, snddev_mi2s_data->name);
204 goto mi2s_data_gpio_failure;
205 }
206 MM_DBG("%s: done data line gpio config for %s\n",
207 __func__, snddev_mi2s_data->name);
208
209 rc = snddev_mi2s_open_tx(dev_info);
210
211 if (IS_ERR_VALUE(rc)) {
212 MM_ERR(" snddev_mi2s_open_tx failed \n");
213 goto mi2s_cleanup_open;
214 }
215
216 drv->sd_lines_used |= snddev_mi2s_data->sd_lines;
217 MM_DBG("%s: sd_lines_used = 0x%x\n", __func__,
218 drv->sd_lines_used);
219 mutex_unlock(&drv->lock);
220 }
221
222 return 0;
223
224mi2s_cleanup_open:
225 mi2s_unconfig_data_gpio(dir, snddev_mi2s_data->sd_lines);
226
227 /* Disable audio path */
228 if (snddev_mi2s_data->deroute)
229 snddev_mi2s_data->deroute();
230
231mi2s_data_gpio_failure:
232 if (!drv->sd_lines_used) {
233 clk_disable(drv->sclk);
234 clk_disable(drv->mclk);
235 drv->clocks_enabled = 0;
236 mi2s_unconfig_clk_gpio();
237 }
238 mutex_unlock(&drv->lock);
239 return rc;
240}
241
242static int snddev_mi2s_close(struct msm_snddev_info *dev_info)
243{
244 struct snddev_mi2s_drv_state *drv = &snddev_mi2s_drv;
245 int dir;
246 struct snddev_mi2s_data *snddev_mi2s_data = dev_info->private_data;
247
248 if (!dev_info) {
249 MM_ERR("%s: msm_snddev_info is null \n", __func__);
250 return -EINVAL;
251 }
252
253 if (!dev_info->opened) {
254 MM_ERR(" %s: calling close device with out opening the"
255 " device \n", __func__);
256 return -EIO;
257 }
258
259 mutex_lock(&drv->lock);
260
261 drv->sd_lines_used &= ~snddev_mi2s_data->sd_lines;
262
263 MM_DBG("%s: sd_lines in use = 0x%x\n", __func__, drv->sd_lines_used);
264
265 if (snddev_mi2s_data->capability & SNDDEV_CAP_RX) {
266 dir = DIR_RX;
267 afe_disable(AFE_HW_PATH_MI2S_RX);
268 } else {
269 dir = DIR_TX;
270 afe_disable(AFE_HW_PATH_MI2S_TX);
271 }
272
273 mi2s_unconfig_data_gpio(dir, snddev_mi2s_data->sd_lines);
274
275 if (!drv->sd_lines_used) {
276 clk_disable(drv->sclk);
277 clk_disable(drv->mclk);
278 drv->clocks_enabled = 0;
279 mi2s_unconfig_clk_gpio();
280 }
281
282 /* Disable audio path */
283 if (snddev_mi2s_data->deroute)
284 snddev_mi2s_data->deroute();
285
286 mutex_unlock(&drv->lock);
287
288 return 0;
289}
290
291static int snddev_mi2s_set_freq(struct msm_snddev_info *dev_info, u32 req_freq)
292{
293 if (req_freq != 48000) {
294 MM_DBG("%s: Unsupported Frequency:%d\n", __func__, req_freq);
295 return -EINVAL;
296 }
297 return 48000;
298}
299
300static int snddev_mi2s_probe(struct platform_device *pdev)
301{
302 int rc = 0;
303 struct snddev_mi2s_data *pdata;
304 struct msm_snddev_info *dev_info;
305
306 if (!pdev || !pdev->dev.platform_data) {
307 printk(KERN_ALERT "Invalid caller \n");
308 return -ENODEV;
309 }
310
311 pdata = pdev->dev.platform_data;
312 if ((pdata->capability & SNDDEV_CAP_RX) &&
313 (pdata->capability & SNDDEV_CAP_TX)) {
314 MM_ERR("%s: invalid device data either RX or TX\n", __func__);
315 return -ENODEV;
316 }
317
318 dev_info = kzalloc(sizeof(struct msm_snddev_info), GFP_KERNEL);
319 if (!dev_info) {
320 MM_ERR("%s: uneable to allocate memeory for msm_snddev_info \n",
321 __func__);
322
323 return -ENOMEM;
324 }
325
326 dev_info->name = pdata->name;
327 dev_info->copp_id = pdata->copp_id;
328 dev_info->acdb_id = pdata->acdb_id;
329 dev_info->private_data = (void *)pdata;
330 dev_info->dev_ops.open = snddev_mi2s_open;
331 dev_info->dev_ops.close = snddev_mi2s_close;
332 dev_info->dev_ops.set_freq = snddev_mi2s_set_freq;
333 dev_info->capability = pdata->capability;
334 dev_info->opened = 0;
335 msm_snddev_register(dev_info);
336 dev_info->sample_rate = pdata->default_sample_rate;
337
338 MM_DBG("%s: probe done for %s\n", __func__, pdata->name);
339 return rc;
340}
341
342static int snddev_mi2s_remove(struct platform_device *pdev)
343{
344 return 0;
345}
346
347static struct platform_driver snddev_mi2s_driver = {
348 .probe = snddev_mi2s_probe,
349 .remove = snddev_mi2s_remove,
350 .driver = {.name = "snddev_mi2s"}
351};
352
353static int __init snddev_mi2s_init(void)
354{
355 s32 rc;
356 struct snddev_mi2s_drv_state *drv = &snddev_mi2s_drv;
357
358 rc = platform_driver_register(&snddev_mi2s_driver);
359 if (IS_ERR_VALUE(rc)) {
360
361 MM_ERR("%s: platform_driver_register failed \n", __func__);
362 goto error_platform_driver;
363 }
364
365 drv->mclk = clk_get(NULL, "mi2s_m_clk");
366 if (IS_ERR(drv->mclk)) {
367 MM_ERR("%s: clk_get mi2s_mclk failed \n", __func__);
368 goto error_mclk;
369 }
370
371 drv->sclk = clk_get(NULL, "mi2s_s_clk");
372 if (IS_ERR(drv->sclk)) {
373 MM_ERR("%s: clk_get mi2s_sclk failed \n", __func__);
374
375 goto error_sclk;
376 }
377
378 mutex_init(&drv->lock);
379
380 MM_DBG("snddev_mi2s_init : done \n");
381
382 return 0;
383
384error_sclk:
385 clk_put(drv->mclk);
386error_mclk:
387 platform_driver_unregister(&snddev_mi2s_driver);
388error_platform_driver:
389
390 MM_ERR("%s: encounter error\n", __func__);
391 return -ENODEV;
392}
393
394static void __exit snddev_mi2s_exit(void)
395{
396 struct snddev_mi2s_drv_state *drv = &snddev_mi2s_drv;
397
398 platform_driver_unregister(&snddev_mi2s_driver);
399
400 clk_put(drv->sclk);
401 clk_put(drv->mclk);
402 return;
403}
404
405module_init(snddev_mi2s_init);
406module_exit(snddev_mi2s_exit);
407
408MODULE_DESCRIPTION("mi2s Sound Device driver");
409MODULE_VERSION("1.0");
410MODULE_LICENSE("GPL v2");