source: rtems/c/src/lib/libbsp/lm32/shared/milkymist_ac97/ac97.c @ c5ba8ba

4.11
Last change on this file since c5ba8ba was c5ba8ba, checked in by Xiangfu Liu <xiangfu@…>, on Jul 9, 2012 at 8:44:47 AM

bsps/lm32: PR2045: Add mic boost option

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*  ac97.c
2 *
3 *  Sound driver for Milkymist SoC
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 *
9 *  COPYRIGHT (c) 2010, 2011 Sebastien Bourdeauducq
10 */
11
12#define RTEMS_STATUS_CHECKS_USE_PRINTK
13
14#include <rtems.h>
15#include <bsp.h>
16#include <bsp/irq-generic.h>
17#include <rtems/libio.h>
18#include <rtems/status-checks.h>
19#include "../include/system_conf.h"
20#include "milkymist_ac97.h"
21
22#define SND_DEVICE_NAME "/dev/snd"
23#define MIXER_DEVICE_NAME "/dev/mixer"
24
25static rtems_id cr_write_sem;
26static rtems_id cr_read_sem;
27
28static rtems_isr crrequest_handler(rtems_vector_number n)
29{
30  rtems_semaphore_release(cr_write_sem);
31  lm32_interrupt_ack(1 << MM_IRQ_AC97CRREQUEST);
32}
33
34static rtems_isr crreply_handler(rtems_vector_number n)
35{
36  rtems_semaphore_release(cr_read_sem);
37  lm32_interrupt_ack(1 << MM_IRQ_AC97CRREPLY);
38}
39
40/* queued playback buffers */
41#define PLAY_Q_SIZE 8
42#define PLAY_Q_MASK (PLAY_Q_SIZE-1)
43
44static struct snd_buffer *play_q[PLAY_Q_SIZE];
45static int play_produce;
46static int play_consume;
47static int play_level;
48
49/* buffers played, for application to collect */
50static rtems_id play_q_done;
51
52static void play_start(struct snd_buffer *buf)
53{
54  if (buf->nsamples > (AC97_MAX_DMASIZE/4))
55    buf->nsamples = AC97_MAX_DMASIZE/4;
56
57  MM_WRITE(MM_AC97_DADDRESS, (unsigned int)buf->samples);
58  MM_WRITE(MM_AC97_DREMAINING, buf->nsamples*4);
59  MM_WRITE(MM_AC97_DCTL, AC97_SCTL_EN);
60}
61
62static rtems_isr pcmplay_handler(rtems_vector_number n)
63{
64  lm32_interrupt_ack(1 << MM_IRQ_AC97DMAR);
65
66  rtems_message_queue_send(play_q_done, &play_q[play_consume],
67    sizeof(void *));
68
69  play_consume = (play_consume + 1) & PLAY_Q_MASK;
70  play_level--;
71
72  if(play_level > 0)
73    play_start(play_q[play_consume]);
74  else
75    MM_WRITE(MM_AC97_DCTL, 0);
76}
77
78/* queued record buffers */
79#define RECORD_Q_SIZE 8
80#define RECORD_Q_MASK (RECORD_Q_SIZE-1)
81
82static struct snd_buffer *record_q[RECORD_Q_SIZE];
83static int record_produce;
84static int record_consume;
85static int record_level;
86
87/* buffers recorded, for application to collect */
88static rtems_id record_q_done;
89
90static void record_start(struct snd_buffer *buf)
91{
92  if (buf->nsamples > (AC97_MAX_DMASIZE/4))
93    buf->nsamples = AC97_MAX_DMASIZE/4;
94
95  MM_WRITE(MM_AC97_UADDRESS, (unsigned int)buf->samples);
96  MM_WRITE(MM_AC97_UREMAINING, buf->nsamples*4);
97  MM_WRITE(MM_AC97_UCTL, AC97_SCTL_EN);
98}
99
100static rtems_isr pcmrecord_handler(rtems_vector_number n)
101{
102  lm32_interrupt_ack(1 << MM_IRQ_AC97DMAW);
103
104  __asm__ volatile( /* Invalidate Level-1 data cache */
105      "wcsr DCC, r0\n"
106      "nop\n"
107    );
108
109  rtems_message_queue_send(record_q_done, &record_q[record_consume],
110    sizeof(void *));
111
112  record_consume = (record_consume + 1) & RECORD_Q_MASK;
113  record_level--;
114
115  if(record_level > 0)
116    record_start(record_q[record_consume]);
117  else
118    MM_WRITE(MM_AC97_UCTL, 0);
119}
120
121rtems_device_driver ac97_initialize(
122  rtems_device_major_number major,
123  rtems_device_minor_number minor,
124  void *arg
125)
126{
127  rtems_status_code sc;
128  rtems_isr_entry dummy;
129
130  sc = rtems_io_register_name(SND_DEVICE_NAME, major, 0);
131  RTEMS_CHECK_SC(sc, "create snd device");
132
133  sc = rtems_io_register_name(MIXER_DEVICE_NAME, major, 1);
134  RTEMS_CHECK_SC(sc, "create mixer device");
135
136  sc = rtems_semaphore_create(
137    rtems_build_name('C', 'R', 'W', 'S'),
138    0,
139    RTEMS_SIMPLE_BINARY_SEMAPHORE,
140    0,
141    &cr_write_sem
142  );
143  RTEMS_CHECK_SC(sc, "create AC97 register write semaphore");
144
145  sc = rtems_semaphore_create(
146    rtems_build_name('C', 'R', 'R', 'S'),
147    0,
148    RTEMS_SIMPLE_BINARY_SEMAPHORE,
149    0,
150    &cr_read_sem
151  );
152  RTEMS_CHECK_SC(sc, "create AC97 register read semaphore");
153
154  sc = rtems_message_queue_create(
155    rtems_build_name('P', 'L', 'Y', 'Q'),
156    PLAY_Q_SIZE*2,
157    sizeof(void *),
158    0,
159    &play_q_done
160  );
161  RTEMS_CHECK_SC(sc, "create playback done queue");
162
163  sc = rtems_message_queue_create(
164    rtems_build_name('R', 'E', 'C', 'Q'),
165    RECORD_Q_SIZE*2,
166    sizeof(void *),
167    0,
168    &record_q_done
169  );
170  RTEMS_CHECK_SC(sc, "create record done queue");
171
172  rtems_interrupt_catch(crrequest_handler, MM_IRQ_AC97CRREQUEST, &dummy);
173  rtems_interrupt_catch(crreply_handler, MM_IRQ_AC97CRREPLY, &dummy);
174  rtems_interrupt_catch(pcmplay_handler, MM_IRQ_AC97DMAR, &dummy);
175  rtems_interrupt_catch(pcmrecord_handler, MM_IRQ_AC97DMAW, &dummy);
176  bsp_interrupt_vector_enable(MM_IRQ_AC97CRREQUEST);
177  bsp_interrupt_vector_enable(MM_IRQ_AC97CRREPLY);
178  bsp_interrupt_vector_enable(MM_IRQ_AC97DMAR);
179  bsp_interrupt_vector_enable(MM_IRQ_AC97DMAW);
180
181  play_produce = 0;
182  play_consume = 0;
183  play_level = 0;
184
185  record_produce = 0;
186  record_consume = 0;
187  record_level = 0;
188
189  return RTEMS_SUCCESSFUL;
190}
191
192static rtems_status_code submit_play(struct snd_buffer *buf)
193{
194  bsp_interrupt_vector_disable(MM_IRQ_AC97DMAR);
195  if (play_level == PLAY_Q_SIZE) {
196    bsp_interrupt_vector_enable(MM_IRQ_AC97DMAR);
197    return RTEMS_UNSATISFIED;
198  }
199  play_q[play_produce] = buf;
200  play_produce = (play_produce + 1) & PLAY_Q_MASK;
201  play_level++;
202
203  if (play_level == 1)
204    play_start(buf);
205
206  bsp_interrupt_vector_enable(MM_IRQ_AC97DMAR);
207  return RTEMS_SUCCESSFUL;
208}
209
210static rtems_status_code collect_play(struct snd_buffer **buf)
211{
212  size_t s;
213
214  return rtems_message_queue_receive(
215    play_q_done,
216    buf,
217    &s,
218    RTEMS_WAIT,
219    RTEMS_NO_TIMEOUT
220  );
221}
222
223static rtems_status_code submit_record(struct snd_buffer *buf)
224{
225  bsp_interrupt_vector_disable(MM_IRQ_AC97DMAW);
226  if (record_level == RECORD_Q_SIZE) {
227    bsp_interrupt_vector_enable(MM_IRQ_AC97DMAW);
228    return RTEMS_UNSATISFIED;
229  }
230  record_q[record_produce] = buf;
231  record_produce = (record_produce + 1) & RECORD_Q_MASK;
232  record_level++;
233
234  if (record_level == 1)
235    record_start(buf);
236
237  bsp_interrupt_vector_enable(MM_IRQ_AC97DMAW);
238  return RTEMS_SUCCESSFUL;
239}
240
241static rtems_status_code collect_record(struct snd_buffer **buf)
242{
243  size_t s;
244
245  return rtems_message_queue_receive(
246    record_q_done,
247    buf,
248    &s,
249    RTEMS_WAIT,
250    RTEMS_NO_TIMEOUT
251  );
252}
253
254#define CR_TIMEOUT 10
255
256static int read_cr(unsigned int adr)
257{
258  rtems_status_code sc;
259
260  MM_WRITE(MM_AC97_CRADDR, adr);
261  MM_WRITE(MM_AC97_CRCTL, AC97_CRCTL_RQEN);
262  sc = rtems_semaphore_obtain(cr_write_sem, RTEMS_WAIT, CR_TIMEOUT);
263  if (sc != RTEMS_SUCCESSFUL)
264    return -1;
265  sc = rtems_semaphore_obtain(cr_read_sem, RTEMS_WAIT, CR_TIMEOUT);
266  if (sc != RTEMS_SUCCESSFUL)
267    return -1;
268  return MM_READ(MM_AC97_CRDATAIN);
269}
270
271static int write_cr(unsigned int adr, unsigned int val)
272{
273  rtems_status_code sc;
274
275  MM_WRITE(MM_AC97_CRADDR, adr);
276  MM_WRITE(MM_AC97_CRDATAOUT, val);
277  MM_WRITE(MM_AC97_CRCTL, AC97_CRCTL_RQEN|AC97_CRCTL_WRITE);
278  sc = rtems_semaphore_obtain(cr_write_sem, RTEMS_WAIT, CR_TIMEOUT);
279  if (sc != RTEMS_SUCCESSFUL)
280    return 0;
281  return 1;
282}
283
284rtems_device_driver ac97_open(
285   rtems_device_major_number major,
286   rtems_device_minor_number minor,
287   void *arg
288)
289{
290  int codec_id;
291 
292  if (minor == 0) {
293    /* snd */
294    return RTEMS_SUCCESSFUL;
295  } else {
296    /* mixer */
297    codec_id = read_cr(0x00);
298    if ((codec_id != 0x0d50) && (codec_id != 0x6150)) {
299      printk("AC97 codec detection failed\n");
300      return RTEMS_UNSATISFIED;
301    }
302    write_cr(0x02, 0x0000); /* master volume */
303    write_cr(0x04, 0x0f0f); /* headphones volume */
304    write_cr(0x18, 0x0000); /* PCM out volume */
305    write_cr(0x1c, 0x0f0f); /* record gain */
306
307    write_cr(0x1a, 0x0505); /* record select: stereo mix */
308
309    return RTEMS_SUCCESSFUL;
310  }
311}
312
313static rtems_status_code ioctl_read_channel(void *buf,
314  unsigned int chan, int mono)
315{
316  unsigned int *val = (unsigned int *)buf;
317  int mic_boost;
318  int codec;
319  int left, right;
320
321  codec = read_cr(chan);
322  if (codec < 0)
323    return RTEMS_UNSATISFIED;
324  if (codec & 0x8000) {
325    /* muted */
326    *val = 0;
327    return RTEMS_SUCCESSFUL;
328  }
329  if (mono) {
330    left = 100-(((codec & 0x1f) + 1)*100)/32;
331    mic_boost = (codec & (1 << 6)) >> 6;
332    *val = left | mic_boost << 8;
333  } else {
334    right = 100-(((codec & 0x1f) + 1)*100)/32;
335    left = 100-((((codec & 0x1f00) >> 8) + 1)*100)/32;
336    *val = left | (right << 8);
337  }
338  return RTEMS_SUCCESSFUL;
339}
340
341static rtems_status_code ioctl_write_channel(void *buf,
342  unsigned int chan, int mono)
343{
344  unsigned int *val = (unsigned int *)buf;
345  int mic_boost;
346  int left, right;
347  int codec;
348  rtems_status_code sc;
349
350  left = *val & 0xff;
351  left = (left*32)/100 - 1;
352  if (left < 0)
353    left = 0;
354
355  if (mono) {
356    mic_boost = *val >> 8;
357    right = 31;
358  } else {
359    right = (*val >> 8) & 0xff;
360    right = (right*32)/100 - 1;
361    if (right < 0)
362      right = 0;
363  }
364
365  if ((left == 0) && (right == 0))
366    /* mute */
367    codec = 0x8000;
368  else
369    codec = (31-left) | ((31-right) << 8);
370
371  if (mono) {
372    if (mic_boost)
373      codec |= (1 << 6);
374    else
375      codec &= ~(1 << 6);
376  }
377
378  if (!write_cr(chan, codec))
379    sc = RTEMS_UNSATISFIED;
380  else
381    sc = RTEMS_SUCCESSFUL;
382  return sc;
383}
384
385rtems_device_driver ac97_control(
386  rtems_device_major_number major,
387  rtems_device_minor_number minor,
388  void *arg
389)
390{
391  rtems_libio_ioctl_args_t *args = arg;
392  rtems_status_code sc;
393
394  args->ioctl_return = -1;
395  if(minor == 0) {
396    /* dsp */
397    switch (args->command) {
398      case SOUND_SND_SUBMIT_PLAY:
399        return submit_play((struct snd_buffer *)args->buffer);
400      case SOUND_SND_COLLECT_PLAY:
401        return collect_play((struct snd_buffer **)args->buffer);
402      case SOUND_SND_SUBMIT_RECORD:
403        return submit_record((struct snd_buffer *)args->buffer);
404      case SOUND_SND_COLLECT_RECORD:
405        return collect_record((struct snd_buffer **)args->buffer);
406      default:
407        return RTEMS_UNSATISFIED;
408    }
409  } else {
410    /* mixer */
411    switch (args->command) {
412      case SOUND_MIXER_READ(SOUND_MIXER_MIC):
413        sc = ioctl_read_channel(args->buffer, 0x0e, 1);
414        if(sc == RTEMS_SUCCESSFUL)
415          args->ioctl_return = 0;
416        return sc;
417      case SOUND_MIXER_READ(SOUND_MIXER_LINE):
418        sc = ioctl_read_channel(args->buffer, 0x10, 0);
419        if(sc == RTEMS_SUCCESSFUL)
420          args->ioctl_return = 0;
421        return sc;
422      case SOUND_MIXER_WRITE(SOUND_MIXER_MIC):
423        sc = ioctl_write_channel(args->buffer, 0x0e, 1);
424        if(sc == RTEMS_SUCCESSFUL)
425          args->ioctl_return = 0;
426        return sc;
427      case SOUND_MIXER_WRITE(SOUND_MIXER_LINE):
428        sc = ioctl_write_channel(args->buffer, 0x10, 0);
429        if(sc == RTEMS_SUCCESSFUL)
430          args->ioctl_return = 0;
431        return sc;
432      default:
433        return RTEMS_UNSATISFIED;
434    }
435  }
436}
Note: See TracBrowser for help on using the repository browser.