blob: e11187fb433fa7b889b6198d61cbc07e5fe23e65 [file] [log] [blame]
Ola Lilja3592b7f2012-05-08 15:57:18 +02001/*
2 * Copyright (C) ST-Ericsson SA 2012
3 *
4 * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
5 * Roger Nilsson <roger.xr.nilsson@stericsson.com>
6 * for ST-Ericsson.
7 *
8 * License terms:
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/bitops.h>
18#include <linux/platform_device.h>
19#include <linux/clk.h>
20#include <linux/regulator/consumer.h>
21#include <linux/mfd/dbx500-prcmu.h>
22
23#include <mach/hardware.h>
Lee Jonesaa50fe52012-07-26 11:28:38 +010024#include <mach/msp.h>
Ola Lilja3592b7f2012-05-08 15:57:18 +020025
26#include <sound/soc.h>
27#include <sound/soc-dai.h>
28
29#include "ux500_msp_i2s.h"
30#include "ux500_msp_dai.h"
31
32static int setup_pcm_multichan(struct snd_soc_dai *dai,
33 struct ux500_msp_config *msp_config)
34{
35 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
36 struct msp_multichannel_config *multi =
37 &msp_config->multichannel_config;
38
39 if (drvdata->slots > 1) {
40 msp_config->multichannel_configured = 1;
41
42 multi->tx_multichannel_enable = true;
43 multi->rx_multichannel_enable = true;
44 multi->rx_comparison_enable_mode = MSP_COMPARISON_DISABLED;
45
46 multi->tx_channel_0_enable = drvdata->tx_mask;
47 multi->tx_channel_1_enable = 0;
48 multi->tx_channel_2_enable = 0;
49 multi->tx_channel_3_enable = 0;
50
51 multi->rx_channel_0_enable = drvdata->rx_mask;
52 multi->rx_channel_1_enable = 0;
53 multi->rx_channel_2_enable = 0;
54 multi->rx_channel_3_enable = 0;
55
56 dev_dbg(dai->dev,
57 "%s: Multichannel enabled. Slots: %d, TX: %u, RX: %u\n",
58 __func__, drvdata->slots, multi->tx_channel_0_enable,
59 multi->rx_channel_0_enable);
60 }
61
62 return 0;
63}
64
65static int setup_frameper(struct snd_soc_dai *dai, unsigned int rate,
66 struct msp_protdesc *prot_desc)
67{
68 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
69
70 switch (drvdata->slots) {
71 case 1:
72 switch (rate) {
73 case 8000:
74 prot_desc->frame_period =
75 FRAME_PER_SINGLE_SLOT_8_KHZ;
76 break;
77
78 case 16000:
79 prot_desc->frame_period =
80 FRAME_PER_SINGLE_SLOT_16_KHZ;
81 break;
82
83 case 44100:
84 prot_desc->frame_period =
85 FRAME_PER_SINGLE_SLOT_44_1_KHZ;
86 break;
87
88 case 48000:
89 prot_desc->frame_period =
90 FRAME_PER_SINGLE_SLOT_48_KHZ;
91 break;
92
93 default:
94 dev_err(dai->dev,
95 "%s: Error: Unsupported sample-rate (freq = %d)!\n",
96 __func__, rate);
97 return -EINVAL;
98 }
99 break;
100
101 case 2:
102 prot_desc->frame_period = FRAME_PER_2_SLOTS;
103 break;
104
105 case 8:
106 prot_desc->frame_period = FRAME_PER_8_SLOTS;
107 break;
108
109 case 16:
110 prot_desc->frame_period = FRAME_PER_16_SLOTS;
111 break;
112 default:
113 dev_err(dai->dev,
114 "%s: Error: Unsupported slot-count (slots = %d)!\n",
115 __func__, drvdata->slots);
116 return -EINVAL;
117 }
118
119 prot_desc->clocks_per_frame =
120 prot_desc->frame_period+1;
121
122 dev_dbg(dai->dev, "%s: Clocks per frame: %u\n",
123 __func__,
124 prot_desc->clocks_per_frame);
125
126 return 0;
127}
128
129static int setup_pcm_framing(struct snd_soc_dai *dai, unsigned int rate,
130 struct msp_protdesc *prot_desc)
131{
132 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
133
134 u32 frame_length = MSP_FRAME_LEN_1;
135 prot_desc->frame_width = 0;
136
137 switch (drvdata->slots) {
138 case 1:
139 frame_length = MSP_FRAME_LEN_1;
140 break;
141
142 case 2:
143 frame_length = MSP_FRAME_LEN_2;
144 break;
145
146 case 8:
147 frame_length = MSP_FRAME_LEN_8;
148 break;
149
150 case 16:
151 frame_length = MSP_FRAME_LEN_16;
152 break;
153 default:
154 dev_err(dai->dev,
155 "%s: Error: Unsupported slot-count (slots = %d)!\n",
156 __func__, drvdata->slots);
157 return -EINVAL;
158 }
159
160 prot_desc->tx_frame_len_1 = frame_length;
161 prot_desc->rx_frame_len_1 = frame_length;
162 prot_desc->tx_frame_len_2 = frame_length;
163 prot_desc->rx_frame_len_2 = frame_length;
164
165 prot_desc->tx_elem_len_1 = MSP_ELEM_LEN_16;
166 prot_desc->rx_elem_len_1 = MSP_ELEM_LEN_16;
167 prot_desc->tx_elem_len_2 = MSP_ELEM_LEN_16;
168 prot_desc->rx_elem_len_2 = MSP_ELEM_LEN_16;
169
170 return setup_frameper(dai, rate, prot_desc);
171}
172
173static int setup_clocking(struct snd_soc_dai *dai,
174 unsigned int fmt,
175 struct ux500_msp_config *msp_config)
176{
177 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
178 case SND_SOC_DAIFMT_NB_NF:
179 break;
180
181 case SND_SOC_DAIFMT_NB_IF:
182 msp_config->tx_fsync_pol ^= 1 << TFSPOL_SHIFT;
183 msp_config->rx_fsync_pol ^= 1 << RFSPOL_SHIFT;
184
185 break;
186
187 default:
188 dev_err(dai->dev,
189 "%s: Error: Unsopported inversion (fmt = 0x%x)!\n",
190 __func__, fmt);
191
192 return -EINVAL;
193 }
194
195 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
196 case SND_SOC_DAIFMT_CBM_CFM:
197 dev_dbg(dai->dev, "%s: Codec is master.\n", __func__);
198
199 msp_config->iodelay = 0x20;
200 msp_config->rx_fsync_sel = 0;
201 msp_config->tx_fsync_sel = 1 << TFSSEL_SHIFT;
202 msp_config->tx_clk_sel = 0;
203 msp_config->rx_clk_sel = 0;
204 msp_config->srg_clk_sel = 0x2 << SCKSEL_SHIFT;
205
206 break;
207
208 case SND_SOC_DAIFMT_CBS_CFS:
209 dev_dbg(dai->dev, "%s: Codec is slave.\n", __func__);
210
211 msp_config->tx_clk_sel = TX_CLK_SEL_SRG;
212 msp_config->tx_fsync_sel = TX_SYNC_SRG_PROG;
213 msp_config->rx_clk_sel = RX_CLK_SEL_SRG;
214 msp_config->rx_fsync_sel = RX_SYNC_SRG;
215 msp_config->srg_clk_sel = 1 << SCKSEL_SHIFT;
216
217 break;
218
219 default:
220 dev_err(dai->dev, "%s: Error: Unsopported master (fmt = 0x%x)!\n",
221 __func__, fmt);
222
223 return -EINVAL;
224 }
225
226 return 0;
227}
228
229static int setup_pcm_protdesc(struct snd_soc_dai *dai,
230 unsigned int fmt,
231 struct msp_protdesc *prot_desc)
232{
233 prot_desc->rx_phase_mode = MSP_SINGLE_PHASE;
234 prot_desc->tx_phase_mode = MSP_SINGLE_PHASE;
235 prot_desc->rx_phase2_start_mode = MSP_PHASE2_START_MODE_IMEDIATE;
236 prot_desc->tx_phase2_start_mode = MSP_PHASE2_START_MODE_IMEDIATE;
237 prot_desc->rx_byte_order = MSP_BTF_MS_BIT_FIRST;
238 prot_desc->tx_byte_order = MSP_BTF_MS_BIT_FIRST;
239 prot_desc->tx_fsync_pol = MSP_FSYNC_POL(MSP_FSYNC_POL_ACT_HI);
240 prot_desc->rx_fsync_pol = MSP_FSYNC_POL_ACT_HI << RFSPOL_SHIFT;
241
242 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_DSP_A) {
243 dev_dbg(dai->dev, "%s: DSP_A.\n", __func__);
244 prot_desc->rx_clk_pol = MSP_RISING_EDGE;
245 prot_desc->tx_clk_pol = MSP_FALLING_EDGE;
246
247 prot_desc->rx_data_delay = MSP_DELAY_1;
248 prot_desc->tx_data_delay = MSP_DELAY_1;
249 } else {
250 dev_dbg(dai->dev, "%s: DSP_B.\n", __func__);
251 prot_desc->rx_clk_pol = MSP_FALLING_EDGE;
252 prot_desc->tx_clk_pol = MSP_RISING_EDGE;
253
254 prot_desc->rx_data_delay = MSP_DELAY_0;
255 prot_desc->tx_data_delay = MSP_DELAY_0;
256 }
257
258 prot_desc->rx_half_word_swap = MSP_SWAP_NONE;
259 prot_desc->tx_half_word_swap = MSP_SWAP_NONE;
260 prot_desc->compression_mode = MSP_COMPRESS_MODE_LINEAR;
261 prot_desc->expansion_mode = MSP_EXPAND_MODE_LINEAR;
262 prot_desc->frame_sync_ignore = MSP_FSYNC_IGNORE;
263
264 return 0;
265}
266
267static int setup_i2s_protdesc(struct msp_protdesc *prot_desc)
268{
269 prot_desc->rx_phase_mode = MSP_DUAL_PHASE;
270 prot_desc->tx_phase_mode = MSP_DUAL_PHASE;
271 prot_desc->rx_phase2_start_mode = MSP_PHASE2_START_MODE_FSYNC;
272 prot_desc->tx_phase2_start_mode = MSP_PHASE2_START_MODE_FSYNC;
273 prot_desc->rx_byte_order = MSP_BTF_MS_BIT_FIRST;
274 prot_desc->tx_byte_order = MSP_BTF_MS_BIT_FIRST;
275 prot_desc->tx_fsync_pol = MSP_FSYNC_POL(MSP_FSYNC_POL_ACT_LO);
276 prot_desc->rx_fsync_pol = MSP_FSYNC_POL_ACT_LO << RFSPOL_SHIFT;
277
278 prot_desc->rx_frame_len_1 = MSP_FRAME_LEN_1;
279 prot_desc->rx_frame_len_2 = MSP_FRAME_LEN_1;
280 prot_desc->tx_frame_len_1 = MSP_FRAME_LEN_1;
281 prot_desc->tx_frame_len_2 = MSP_FRAME_LEN_1;
282 prot_desc->rx_elem_len_1 = MSP_ELEM_LEN_16;
283 prot_desc->rx_elem_len_2 = MSP_ELEM_LEN_16;
284 prot_desc->tx_elem_len_1 = MSP_ELEM_LEN_16;
285 prot_desc->tx_elem_len_2 = MSP_ELEM_LEN_16;
286
287 prot_desc->rx_clk_pol = MSP_RISING_EDGE;
288 prot_desc->tx_clk_pol = MSP_FALLING_EDGE;
289
290 prot_desc->rx_data_delay = MSP_DELAY_0;
291 prot_desc->tx_data_delay = MSP_DELAY_0;
292
293 prot_desc->tx_half_word_swap = MSP_SWAP_NONE;
294 prot_desc->rx_half_word_swap = MSP_SWAP_NONE;
295 prot_desc->compression_mode = MSP_COMPRESS_MODE_LINEAR;
296 prot_desc->expansion_mode = MSP_EXPAND_MODE_LINEAR;
297 prot_desc->frame_sync_ignore = MSP_FSYNC_IGNORE;
298
299 return 0;
300}
301
302static int setup_msp_config(struct snd_pcm_substream *substream,
303 struct snd_soc_dai *dai,
304 struct ux500_msp_config *msp_config)
305{
306 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
307 struct msp_protdesc *prot_desc = &msp_config->protdesc;
308 struct snd_pcm_runtime *runtime = substream->runtime;
309 unsigned int fmt = drvdata->fmt;
310 int ret;
311
312 memset(msp_config, 0, sizeof(*msp_config));
313
314 msp_config->f_inputclk = drvdata->master_clk;
315
316 msp_config->tx_fifo_config = TX_FIFO_ENABLE;
317 msp_config->rx_fifo_config = RX_FIFO_ENABLE;
318 msp_config->def_elem_len = 1;
319 msp_config->direction = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
320 MSP_DIR_TX : MSP_DIR_RX;
321 msp_config->data_size = MSP_DATA_BITS_32;
322 msp_config->frame_freq = runtime->rate;
323
324 dev_dbg(dai->dev, "%s: f_inputclk = %u, frame_freq = %u.\n",
325 __func__, msp_config->f_inputclk, msp_config->frame_freq);
326 /* To avoid division by zero */
327 prot_desc->clocks_per_frame = 1;
328
329 dev_dbg(dai->dev, "%s: rate: %u, channels: %d.\n", __func__,
330 runtime->rate, runtime->channels);
331 switch (fmt &
332 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
333 case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
334 dev_dbg(dai->dev, "%s: SND_SOC_DAIFMT_I2S.\n", __func__);
335
336 msp_config->default_protdesc = 1;
337 msp_config->protocol = MSP_I2S_PROTOCOL;
338 break;
339
340 case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
341 dev_dbg(dai->dev, "%s: SND_SOC_DAIFMT_I2S.\n", __func__);
342
343 msp_config->data_size = MSP_DATA_BITS_16;
344 msp_config->protocol = MSP_I2S_PROTOCOL;
345
346 ret = setup_i2s_protdesc(prot_desc);
347 if (ret < 0)
348 return ret;
349
350 break;
351
352 case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
353 case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
354 case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBS_CFS:
355 case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM:
356 dev_dbg(dai->dev, "%s: PCM format.\n", __func__);
357
358 msp_config->data_size = MSP_DATA_BITS_16;
359 msp_config->protocol = MSP_PCM_PROTOCOL;
360
361 ret = setup_pcm_protdesc(dai, fmt, prot_desc);
362 if (ret < 0)
363 return ret;
364
365 ret = setup_pcm_multichan(dai, msp_config);
366 if (ret < 0)
367 return ret;
368
369 ret = setup_pcm_framing(dai, runtime->rate, prot_desc);
370 if (ret < 0)
371 return ret;
372
373 break;
374
375 default:
376 dev_err(dai->dev, "%s: Error: Unsopported format (%d)!\n",
377 __func__, fmt);
378 return -EINVAL;
379 }
380
381 return setup_clocking(dai, fmt, msp_config);
382}
383
384static int ux500_msp_dai_startup(struct snd_pcm_substream *substream,
385 struct snd_soc_dai *dai)
386{
387 int ret = 0;
388 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
389
390 dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
391 snd_pcm_stream_str(substream));
392
393 /* Enable regulator */
394 ret = regulator_enable(drvdata->reg_vape);
395 if (ret != 0) {
396 dev_err(drvdata->msp->dev,
397 "%s: Failed to enable regulator!\n", __func__);
398 return ret;
399 }
400
Ulf Hanssonfe36a0b2012-10-22 14:32:04 +0200401 /* Prepare and enable clock */
Ola Lilja3592b7f2012-05-08 15:57:18 +0200402 dev_dbg(dai->dev, "%s: Enabling MSP-clock.\n", __func__);
Ulf Hanssonfe36a0b2012-10-22 14:32:04 +0200403 ret = clk_prepare_enable(drvdata->clk);
404 if (ret)
405 regulator_disable(drvdata->reg_vape);
Ola Lilja3592b7f2012-05-08 15:57:18 +0200406
Ulf Hanssonfe36a0b2012-10-22 14:32:04 +0200407 return ret;
Ola Lilja3592b7f2012-05-08 15:57:18 +0200408}
409
410static void ux500_msp_dai_shutdown(struct snd_pcm_substream *substream,
411 struct snd_soc_dai *dai)
412{
413 int ret;
414 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
415 bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
416
417 dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
418 snd_pcm_stream_str(substream));
419
420 if (drvdata->vape_opp_constraint == 1) {
421 prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
422 "ux500_msp_i2s", 50);
423 drvdata->vape_opp_constraint = 0;
424 }
425
426 if (ux500_msp_i2s_close(drvdata->msp,
427 is_playback ? MSP_DIR_TX : MSP_DIR_RX)) {
428 dev_err(dai->dev,
429 "%s: Error: MSP %d (%s): Unable to close i2s.\n",
430 __func__, dai->id, snd_pcm_stream_str(substream));
431 }
432
Ulf Hanssonfe36a0b2012-10-22 14:32:04 +0200433 /* Disable and unprepare clock */
434 clk_disable_unprepare(drvdata->clk);
Ola Lilja3592b7f2012-05-08 15:57:18 +0200435
436 /* Disable regulator */
437 ret = regulator_disable(drvdata->reg_vape);
438 if (ret < 0)
439 dev_err(dai->dev,
440 "%s: ERROR: Failed to disable regulator (%d)!\n",
441 __func__, ret);
442}
443
444static int ux500_msp_dai_prepare(struct snd_pcm_substream *substream,
445 struct snd_soc_dai *dai)
446{
447 int ret = 0;
448 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
449 struct snd_pcm_runtime *runtime = substream->runtime;
450 struct ux500_msp_config msp_config;
451
452 dev_dbg(dai->dev, "%s: MSP %d (%s): Enter (rate = %d).\n", __func__,
453 dai->id, snd_pcm_stream_str(substream), runtime->rate);
454
455 setup_msp_config(substream, dai, &msp_config);
456
457 ret = ux500_msp_i2s_open(drvdata->msp, &msp_config);
458 if (ret < 0) {
459 dev_err(dai->dev, "%s: Error: msp_setup failed (ret = %d)!\n",
460 __func__, ret);
461 return ret;
462 }
463
464 /* Set OPP-level */
465 if ((drvdata->fmt & SND_SOC_DAIFMT_MASTER_MASK) &&
466 (drvdata->msp->f_bitclk > 19200000)) {
467 /* If the bit-clock is higher than 19.2MHz, Vape should be
468 * run in 100% OPP. Only when bit-clock is used (MSP master) */
469 prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
470 "ux500-msp-i2s", 100);
471 drvdata->vape_opp_constraint = 1;
472 } else {
473 prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
474 "ux500-msp-i2s", 50);
475 drvdata->vape_opp_constraint = 0;
476 }
477
478 return ret;
479}
480
481static int ux500_msp_dai_hw_params(struct snd_pcm_substream *substream,
482 struct snd_pcm_hw_params *params,
483 struct snd_soc_dai *dai)
484{
485 unsigned int mask, slots_active;
486 struct snd_pcm_runtime *runtime = substream->runtime;
487 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
488
489 dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n",
490 __func__, dai->id, snd_pcm_stream_str(substream));
491
492 switch (drvdata->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
493 case SND_SOC_DAIFMT_I2S:
494 snd_pcm_hw_constraint_minmax(runtime,
495 SNDRV_PCM_HW_PARAM_CHANNELS,
496 1, 2);
497 break;
498
499 case SND_SOC_DAIFMT_DSP_B:
500 case SND_SOC_DAIFMT_DSP_A:
501 mask = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
502 drvdata->tx_mask :
503 drvdata->rx_mask;
504
505 slots_active = hweight32(mask);
506 dev_dbg(dai->dev, "TDM-slots active: %d", slots_active);
507
508 snd_pcm_hw_constraint_minmax(runtime,
509 SNDRV_PCM_HW_PARAM_CHANNELS,
510 slots_active, slots_active);
511 break;
512
513 default:
514 dev_err(dai->dev,
515 "%s: Error: Unsupported protocol (fmt = 0x%x)!\n",
516 __func__, drvdata->fmt);
517 return -EINVAL;
518 }
519
520 return 0;
521}
522
523static int ux500_msp_dai_set_dai_fmt(struct snd_soc_dai *dai,
524 unsigned int fmt)
525{
526 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
527
528 dev_dbg(dai->dev, "%s: MSP %d: Enter.\n", __func__, dai->id);
529
530 switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
531 SND_SOC_DAIFMT_MASTER_MASK)) {
532 case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
533 case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
534 case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBS_CFS:
535 case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM:
536 case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
537 case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
538 break;
539
540 default:
541 dev_err(dai->dev,
542 "%s: Error: Unsupported protocol/master (fmt = 0x%x)!\n",
543 __func__, drvdata->fmt);
544 return -EINVAL;
545 }
546
547 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
548 case SND_SOC_DAIFMT_NB_NF:
549 case SND_SOC_DAIFMT_NB_IF:
550 case SND_SOC_DAIFMT_IB_IF:
551 break;
552
553 default:
554 dev_err(dai->dev,
555 "%s: Error: Unsupported inversion (fmt = 0x%x)!\n",
556 __func__, drvdata->fmt);
557 return -EINVAL;
558 }
559
560 drvdata->fmt = fmt;
561 return 0;
562}
563
564static int ux500_msp_dai_set_tdm_slot(struct snd_soc_dai *dai,
565 unsigned int tx_mask,
566 unsigned int rx_mask,
567 int slots, int slot_width)
568{
569 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
570 unsigned int cap;
571
572 switch (slots) {
573 case 1:
574 cap = 0x01;
575 break;
576 case 2:
577 cap = 0x03;
578 break;
579 case 8:
580 cap = 0xFF;
581 break;
582 case 16:
583 cap = 0xFFFF;
584 break;
585 default:
586 dev_err(dai->dev, "%s: Error: Unsupported slot-count (%d)!\n",
587 __func__, slots);
588 return -EINVAL;
589 }
590 drvdata->slots = slots;
591
592 if (!(slot_width == 16)) {
593 dev_err(dai->dev, "%s: Error: Unsupported slot-width (%d)!\n",
594 __func__, slot_width);
595 return -EINVAL;
596 }
597 drvdata->slot_width = slot_width;
598
599 drvdata->tx_mask = tx_mask & cap;
600 drvdata->rx_mask = rx_mask & cap;
601
602 return 0;
603}
604
605static int ux500_msp_dai_set_dai_sysclk(struct snd_soc_dai *dai,
606 int clk_id, unsigned int freq, int dir)
607{
608 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
609
610 dev_dbg(dai->dev, "%s: MSP %d: Enter. clk-id: %d, freq: %u.\n",
611 __func__, dai->id, clk_id, freq);
612
613 switch (clk_id) {
614 case UX500_MSP_MASTER_CLOCK:
615 drvdata->master_clk = freq;
616 break;
617
618 default:
619 dev_err(dai->dev, "%s: MSP %d: Invalid clk-id (%d)!\n",
620 __func__, dai->id, clk_id);
621 return -EINVAL;
622 }
623
624 return 0;
625}
626
627static int ux500_msp_dai_trigger(struct snd_pcm_substream *substream,
628 int cmd, struct snd_soc_dai *dai)
629{
630 int ret = 0;
631 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
632
633 dev_dbg(dai->dev, "%s: MSP %d (%s): Enter (msp->id = %d, cmd = %d).\n",
634 __func__, dai->id, snd_pcm_stream_str(substream),
635 (int)drvdata->msp->id, cmd);
636
637 ret = ux500_msp_i2s_trigger(drvdata->msp, cmd, substream->stream);
638
639 return ret;
640}
641
642static int ux500_msp_dai_probe(struct snd_soc_dai *dai)
643{
644 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
645
646 drvdata->playback_dma_data.dma_cfg = drvdata->msp->dma_cfg_tx;
647 drvdata->capture_dma_data.dma_cfg = drvdata->msp->dma_cfg_rx;
648
649 dai->playback_dma_data = &drvdata->playback_dma_data;
650 dai->capture_dma_data = &drvdata->capture_dma_data;
651
652 drvdata->playback_dma_data.data_size = drvdata->slot_width;
653 drvdata->capture_dma_data.data_size = drvdata->slot_width;
654
655 return 0;
656}
657
658static struct snd_soc_dai_ops ux500_msp_dai_ops[] = {
659 {
660 .set_sysclk = ux500_msp_dai_set_dai_sysclk,
661 .set_fmt = ux500_msp_dai_set_dai_fmt,
662 .set_tdm_slot = ux500_msp_dai_set_tdm_slot,
663 .startup = ux500_msp_dai_startup,
664 .shutdown = ux500_msp_dai_shutdown,
665 .prepare = ux500_msp_dai_prepare,
666 .trigger = ux500_msp_dai_trigger,
667 .hw_params = ux500_msp_dai_hw_params,
668 }
669};
670
671static struct snd_soc_dai_driver ux500_msp_dai_drv[UX500_NBR_OF_DAI] = {
672 {
673 .name = "ux500-msp-i2s.0",
674 .probe = ux500_msp_dai_probe,
675 .id = 0,
676 .suspend = NULL,
677 .resume = NULL,
678 .playback = {
679 .channels_min = UX500_MSP_MIN_CHANNELS,
680 .channels_max = UX500_MSP_MAX_CHANNELS,
681 .rates = UX500_I2S_RATES,
682 .formats = UX500_I2S_FORMATS,
683 },
684 .capture = {
685 .channels_min = UX500_MSP_MIN_CHANNELS,
686 .channels_max = UX500_MSP_MAX_CHANNELS,
687 .rates = UX500_I2S_RATES,
688 .formats = UX500_I2S_FORMATS,
689 },
690 .ops = ux500_msp_dai_ops,
691 },
692 {
693 .name = "ux500-msp-i2s.1",
694 .probe = ux500_msp_dai_probe,
695 .id = 1,
696 .suspend = NULL,
697 .resume = NULL,
698 .playback = {
699 .channels_min = UX500_MSP_MIN_CHANNELS,
700 .channels_max = UX500_MSP_MAX_CHANNELS,
701 .rates = UX500_I2S_RATES,
702 .formats = UX500_I2S_FORMATS,
703 },
704 .capture = {
705 .channels_min = UX500_MSP_MIN_CHANNELS,
706 .channels_max = UX500_MSP_MAX_CHANNELS,
707 .rates = UX500_I2S_RATES,
708 .formats = UX500_I2S_FORMATS,
709 },
710 .ops = ux500_msp_dai_ops,
711 },
712 {
713 .name = "ux500-msp-i2s.2",
714 .id = 2,
715 .probe = ux500_msp_dai_probe,
716 .suspend = NULL,
717 .resume = NULL,
718 .playback = {
719 .channels_min = UX500_MSP_MIN_CHANNELS,
720 .channels_max = UX500_MSP_MAX_CHANNELS,
721 .rates = UX500_I2S_RATES,
722 .formats = UX500_I2S_FORMATS,
723 },
724 .capture = {
725 .channels_min = UX500_MSP_MIN_CHANNELS,
726 .channels_max = UX500_MSP_MAX_CHANNELS,
727 .rates = UX500_I2S_RATES,
728 .formats = UX500_I2S_FORMATS,
729 },
730 .ops = ux500_msp_dai_ops,
731 },
732 {
733 .name = "ux500-msp-i2s.3",
734 .probe = ux500_msp_dai_probe,
735 .id = 3,
736 .suspend = NULL,
737 .resume = NULL,
738 .playback = {
739 .channels_min = UX500_MSP_MIN_CHANNELS,
740 .channels_max = UX500_MSP_MAX_CHANNELS,
741 .rates = UX500_I2S_RATES,
742 .formats = UX500_I2S_FORMATS,
743 },
744 .capture = {
745 .channels_min = UX500_MSP_MIN_CHANNELS,
746 .channels_max = UX500_MSP_MAX_CHANNELS,
747 .rates = UX500_I2S_RATES,
748 .formats = UX500_I2S_FORMATS,
749 },
750 .ops = ux500_msp_dai_ops,
751 },
752};
753
754static int __devinit ux500_msp_drv_probe(struct platform_device *pdev)
755{
756 struct ux500_msp_i2s_drvdata *drvdata;
757 int ret = 0;
758
759 dev_dbg(&pdev->dev, "%s: Enter (pdev->name = %s).\n", __func__,
760 pdev->name);
761
762 drvdata = devm_kzalloc(&pdev->dev,
763 sizeof(struct ux500_msp_i2s_drvdata),
764 GFP_KERNEL);
Lee Jones0dcd4742012-07-26 11:28:37 +0100765 if (!drvdata)
766 return -ENOMEM;
767
Ola Lilja3592b7f2012-05-08 15:57:18 +0200768 drvdata->fmt = 0;
769 drvdata->slots = 1;
770 drvdata->tx_mask = 0x01;
771 drvdata->rx_mask = 0x01;
772 drvdata->slot_width = 16;
773 drvdata->master_clk = MSP_INPUT_FREQ_APB;
774
775 drvdata->reg_vape = devm_regulator_get(&pdev->dev, "v-ape");
776 if (IS_ERR(drvdata->reg_vape)) {
777 ret = (int)PTR_ERR(drvdata->reg_vape);
778 dev_err(&pdev->dev,
779 "%s: ERROR: Failed to get Vape supply (%d)!\n",
780 __func__, ret);
781 return ret;
782 }
783 prcmu_qos_add_requirement(PRCMU_QOS_APE_OPP, (char *)pdev->name, 50);
784
785 drvdata->clk = clk_get(&pdev->dev, NULL);
786 if (IS_ERR(drvdata->clk)) {
787 ret = (int)PTR_ERR(drvdata->clk);
788 dev_err(&pdev->dev, "%s: ERROR: clk_get failed (%d)!\n",
789 __func__, ret);
790 goto err_clk;
791 }
792
793 ret = ux500_msp_i2s_init_msp(pdev, &drvdata->msp,
794 pdev->dev.platform_data);
795 if (!drvdata->msp) {
796 dev_err(&pdev->dev,
797 "%s: ERROR: Failed to init MSP-struct (%d)!",
798 __func__, ret);
799 goto err_init_msp;
800 }
801 dev_set_drvdata(&pdev->dev, drvdata);
802
803 ret = snd_soc_register_dai(&pdev->dev,
804 &ux500_msp_dai_drv[drvdata->msp->id]);
805 if (ret < 0) {
806 dev_err(&pdev->dev, "Error: %s: Failed to register MSP%d!\n",
807 __func__, drvdata->msp->id);
808 goto err_init_msp;
809 }
810
811 return 0;
812
813err_init_msp:
814 clk_put(drvdata->clk);
815
816err_clk:
817 devm_regulator_put(drvdata->reg_vape);
818
819 return ret;
820}
821
822static int __devexit ux500_msp_drv_remove(struct platform_device *pdev)
823{
824 struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
825
826 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(ux500_msp_dai_drv));
827
828 devm_regulator_put(drvdata->reg_vape);
829 prcmu_qos_remove_requirement(PRCMU_QOS_APE_OPP, "ux500_msp_i2s");
830
831 clk_put(drvdata->clk);
832
833 ux500_msp_i2s_cleanup_msp(pdev, drvdata->msp);
834
835 return 0;
836}
837
Lee Jones49731c22012-07-26 17:07:26 +0100838static const struct of_device_id ux500_msp_i2s_match[] = {
839 { .compatible = "stericsson,ux500-msp-i2s", },
840 {},
841};
842
Ola Lilja3592b7f2012-05-08 15:57:18 +0200843static struct platform_driver msp_i2s_driver = {
844 .driver = {
845 .name = "ux500-msp-i2s",
846 .owner = THIS_MODULE,
Lee Jones49731c22012-07-26 17:07:26 +0100847 .of_match_table = ux500_msp_i2s_match,
Ola Lilja3592b7f2012-05-08 15:57:18 +0200848 },
849 .probe = ux500_msp_drv_probe,
850 .remove = ux500_msp_drv_remove,
851};
852module_platform_driver(msp_i2s_driver);
853
Ola Lilja85f24392012-06-13 10:09:51 +0200854MODULE_LICENSE("GPL v2");