Merge git://git.kernel.org/pub/scm/linux/kernel/git/nico/orion into fixes
[pandora-kernel.git] / drivers / staging / solo6x10 / g723.c
1 /*
2  * Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com
3  * Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/mempool.h>
22 #include <linux/poll.h>
23 #include <linux/kthread.h>
24 #include <linux/freezer.h>
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/control.h>
29 #include "solo6x10.h"
30 #include "tw28.h"
31
32 #define G723_INTR_ORDER         0
33 #define G723_FDMA_PAGES         32
34 #define G723_PERIOD_BYTES       48
35 #define G723_PERIOD_BLOCK       1024
36 #define G723_FRAMES_PER_PAGE    48
37
38 /* Sets up channels 16-19 for decoding and 0-15 for encoding */
39 #define OUTMODE_MASK            0x300
40
41 #define SAMPLERATE              8000
42 #define BITRATE                 25
43
44 /* The solo writes to 1k byte pages, 32 pages, in the dma. Each 1k page
45  * is broken down to 20 * 48 byte regions (one for each channel possible)
46  * with the rest of the page being dummy data. */
47 #define MAX_BUFFER              (G723_PERIOD_BYTES * PERIODS_MAX)
48 #define IRQ_PAGES               4 /* 0 - 4 */
49 #define PERIODS_MIN             (1 << IRQ_PAGES)
50 #define PERIODS_MAX             G723_FDMA_PAGES
51
52 struct solo_snd_pcm {
53         int             on;
54         spinlock_t      lock;
55         struct solo_dev *solo_dev;
56         unsigned char   g723_buf[G723_PERIOD_BYTES];
57 };
58
59 static void solo_g723_config(struct solo_dev *solo_dev)
60 {
61         int clk_div;
62
63         clk_div = SOLO_CLOCK_MHZ / (SAMPLERATE * (BITRATE * 2) * 2);
64
65         solo_reg_write(solo_dev, SOLO_AUDIO_SAMPLE,
66                        SOLO_AUDIO_BITRATE(BITRATE) |
67                        SOLO_AUDIO_CLK_DIV(clk_div));
68
69         solo_reg_write(solo_dev, SOLO_AUDIO_FDMA_INTR,
70                       SOLO_AUDIO_FDMA_INTERVAL(IRQ_PAGES) |
71                       SOLO_AUDIO_INTR_ORDER(G723_INTR_ORDER) |
72                       SOLO_AUDIO_FDMA_BASE(SOLO_G723_EXT_ADDR(solo_dev) >> 16));
73
74         solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL,
75                        SOLO_AUDIO_ENABLE | SOLO_AUDIO_I2S_MODE |
76                        SOLO_AUDIO_I2S_MULTI(3) | SOLO_AUDIO_MODE(OUTMODE_MASK));
77 }
78
79 void solo_g723_isr(struct solo_dev *solo_dev)
80 {
81         struct snd_pcm_str *pstr =
82                 &solo_dev->snd_pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
83         struct snd_pcm_substream *ss;
84         struct solo_snd_pcm *solo_pcm;
85
86         solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_G723);
87
88         for (ss = pstr->substream; ss != NULL; ss = ss->next) {
89                 if (snd_pcm_substream_chip(ss) == NULL)
90                         continue;
91
92                 /* This means open() hasn't been called on this one */
93                 if (snd_pcm_substream_chip(ss) == solo_dev)
94                         continue;
95
96                 /* Haven't triggered a start yet */
97                 solo_pcm = snd_pcm_substream_chip(ss);
98                 if (!solo_pcm->on)
99                         continue;
100
101                 snd_pcm_period_elapsed(ss);
102         }
103 }
104
105 static int snd_solo_hw_params(struct snd_pcm_substream *ss,
106                               struct snd_pcm_hw_params *hw_params)
107 {
108         return snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params));
109 }
110
111 static int snd_solo_hw_free(struct snd_pcm_substream *ss)
112 {
113         return snd_pcm_lib_free_pages(ss);
114 }
115
116 static struct snd_pcm_hardware snd_solo_pcm_hw = {
117         .info                   = (SNDRV_PCM_INFO_MMAP |
118                                    SNDRV_PCM_INFO_INTERLEAVED |
119                                    SNDRV_PCM_INFO_BLOCK_TRANSFER |
120                                    SNDRV_PCM_INFO_MMAP_VALID),
121         .formats                = SNDRV_PCM_FMTBIT_U8,
122         .rates                  = SNDRV_PCM_RATE_8000,
123         .rate_min               = 8000,
124         .rate_max               = 8000,
125         .channels_min           = 1,
126         .channels_max           = 1,
127         .buffer_bytes_max       = MAX_BUFFER,
128         .period_bytes_min       = G723_PERIOD_BYTES,
129         .period_bytes_max       = G723_PERIOD_BYTES,
130         .periods_min            = PERIODS_MIN,
131         .periods_max            = PERIODS_MAX,
132 };
133
134 static int snd_solo_pcm_open(struct snd_pcm_substream *ss)
135 {
136         struct solo_dev *solo_dev = snd_pcm_substream_chip(ss);
137         struct solo_snd_pcm *solo_pcm;
138
139         solo_pcm = kzalloc(sizeof(*solo_pcm), GFP_KERNEL);
140         if (solo_pcm == NULL)
141                 return -ENOMEM;
142
143         spin_lock_init(&solo_pcm->lock);
144         solo_pcm->solo_dev = solo_dev;
145         ss->runtime->hw = snd_solo_pcm_hw;
146
147         snd_pcm_substream_chip(ss) = solo_pcm;
148
149         return 0;
150 }
151
152 static int snd_solo_pcm_close(struct snd_pcm_substream *ss)
153 {
154         struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
155
156         snd_pcm_substream_chip(ss) = solo_pcm->solo_dev;
157         kfree(solo_pcm);
158
159         return 0;
160 }
161
162 static int snd_solo_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
163 {
164         struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
165         struct solo_dev *solo_dev = solo_pcm->solo_dev;
166         int ret = 0;
167
168         spin_lock(&solo_pcm->lock);
169
170         switch (cmd) {
171         case SNDRV_PCM_TRIGGER_START:
172                 if (solo_pcm->on == 0) {
173                         /* If this is the first user, switch on interrupts */
174                         if (atomic_inc_return(&solo_dev->snd_users) == 1)
175                                 solo_irq_on(solo_dev, SOLO_IRQ_G723);
176                         solo_pcm->on = 1;
177                 }
178                 break;
179         case SNDRV_PCM_TRIGGER_STOP:
180                 if (solo_pcm->on) {
181                         /* If this was our last user, switch them off */
182                         if (atomic_dec_return(&solo_dev->snd_users) == 0)
183                                 solo_irq_off(solo_dev, SOLO_IRQ_G723);
184                         solo_pcm->on = 0;
185                 }
186                 break;
187         default:
188                 ret = -EINVAL;
189         }
190
191         spin_unlock(&solo_pcm->lock);
192
193         return ret;
194 }
195
196 static int snd_solo_pcm_prepare(struct snd_pcm_substream *ss)
197 {
198         return 0;
199 }
200
201 static snd_pcm_uframes_t snd_solo_pcm_pointer(struct snd_pcm_substream *ss)
202 {
203         struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
204         struct solo_dev *solo_dev = solo_pcm->solo_dev;
205         snd_pcm_uframes_t idx = solo_reg_read(solo_dev, SOLO_AUDIO_STA) & 0x1f;
206
207         return idx * G723_FRAMES_PER_PAGE;
208 }
209
210 static int snd_solo_pcm_copy(struct snd_pcm_substream *ss, int channel,
211                              snd_pcm_uframes_t pos, void __user *dst,
212                              snd_pcm_uframes_t count)
213 {
214         struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
215         struct solo_dev *solo_dev = solo_pcm->solo_dev;
216         int err, i;
217
218         for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
219                 int page = (pos / G723_FRAMES_PER_PAGE) + i;
220
221                 err = solo_p2m_dma(solo_dev, SOLO_P2M_DMA_ID_G723E, 0,
222                                    solo_pcm->g723_buf,
223                                    SOLO_G723_EXT_ADDR(solo_dev) +
224                                    (page * G723_PERIOD_BLOCK) +
225                                    (ss->number * G723_PERIOD_BYTES),
226                                    G723_PERIOD_BYTES);
227                 if (err)
228                         return err;
229
230                 err = copy_to_user(dst + (i * G723_PERIOD_BYTES),
231                                    solo_pcm->g723_buf, G723_PERIOD_BYTES);
232
233                 if (err)
234                         return -EFAULT;
235         }
236
237         return 0;
238 }
239
240 static struct snd_pcm_ops snd_solo_pcm_ops = {
241         .open = snd_solo_pcm_open,
242         .close = snd_solo_pcm_close,
243         .ioctl = snd_pcm_lib_ioctl,
244         .hw_params = snd_solo_hw_params,
245         .hw_free = snd_solo_hw_free,
246         .prepare = snd_solo_pcm_prepare,
247         .trigger = snd_solo_pcm_trigger,
248         .pointer = snd_solo_pcm_pointer,
249         .copy = snd_solo_pcm_copy,
250 };
251
252 static int snd_solo_capture_volume_info(struct snd_kcontrol *kcontrol,
253                                         struct snd_ctl_elem_info *info)
254 {
255         info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
256         info->count = 1;
257         info->value.integer.min = 0;
258         info->value.integer.max = 15;
259         info->value.integer.step = 1;
260
261         return 0;
262 }
263
264 static int snd_solo_capture_volume_get(struct snd_kcontrol *kcontrol,
265                                        struct snd_ctl_elem_value *value)
266 {
267         struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
268         u8 ch = value->id.numid - 1;
269
270         value->value.integer.value[0] = tw28_get_audio_gain(solo_dev, ch);
271
272         return 0;
273 }
274
275 static int snd_solo_capture_volume_put(struct snd_kcontrol *kcontrol,
276                                        struct snd_ctl_elem_value *value)
277 {
278         struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
279         u8 ch = value->id.numid - 1;
280         u8 old_val;
281
282         old_val = tw28_get_audio_gain(solo_dev, ch);
283         if (old_val == value->value.integer.value[0])
284                 return 0;
285
286         tw28_set_audio_gain(solo_dev, ch, value->value.integer.value[0]);
287
288         return 1;
289 }
290
291 static struct snd_kcontrol_new snd_solo_capture_volume = {
292         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
293         .name = "Capture Volume",
294         .info = snd_solo_capture_volume_info,
295         .get = snd_solo_capture_volume_get,
296         .put = snd_solo_capture_volume_put,
297 };
298
299 static int solo_snd_pcm_init(struct solo_dev *solo_dev)
300 {
301         struct snd_card *card = solo_dev->snd_card;
302         struct snd_pcm *pcm;
303         struct snd_pcm_substream *ss;
304         int ret;
305         int i;
306
307         ret = snd_pcm_new(card, card->driver, 0, 0, solo_dev->nr_chans,
308                           &pcm);
309         if (ret < 0)
310                 return ret;
311
312         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
313                         &snd_solo_pcm_ops);
314
315         snd_pcm_chip(pcm) = solo_dev;
316         pcm->info_flags = 0;
317         strcpy(pcm->name, card->shortname);
318
319         for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
320              ss; ss = ss->next, i++)
321                 sprintf(ss->name, "Camera #%d Audio", i);
322
323         ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
324                                         SNDRV_DMA_TYPE_CONTINUOUS,
325                                         snd_dma_continuous_data(GFP_KERNEL),
326                                         MAX_BUFFER, MAX_BUFFER);
327         if (ret < 0)
328                 return ret;
329
330         solo_dev->snd_pcm = pcm;
331
332         return 0;
333 }
334
335 int solo_g723_init(struct solo_dev *solo_dev)
336 {
337         static struct snd_device_ops ops = { NULL };
338         struct snd_card *card;
339         struct snd_kcontrol_new kctl;
340         char name[32];
341         int ret;
342
343         atomic_set(&solo_dev->snd_users, 0);
344
345         /* Allows for easier mapping between video and audio */
346         sprintf(name, "Softlogic%d", solo_dev->vfd->num);
347
348         ret = snd_card_create(SNDRV_DEFAULT_IDX1, name, THIS_MODULE, 0,
349                               &solo_dev->snd_card);
350         if (ret < 0)
351                 return ret;
352
353         card = solo_dev->snd_card;
354
355         strcpy(card->driver, SOLO6X10_NAME);
356         strcpy(card->shortname, "SOLO-6x10 Audio");
357         sprintf(card->longname, "%s on %s IRQ %d", card->shortname,
358                 pci_name(solo_dev->pdev), solo_dev->pdev->irq);
359         snd_card_set_dev(card, &solo_dev->pdev->dev);
360
361         ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, solo_dev, &ops);
362         if (ret < 0)
363                 goto snd_error;
364
365         /* Mixer controls */
366         strcpy(card->mixername, "SOLO-6x10");
367         kctl = snd_solo_capture_volume;
368         kctl.count = solo_dev->nr_chans;
369         ret = snd_ctl_add(card, snd_ctl_new1(&kctl, solo_dev));
370         if (ret < 0)
371                 return ret;
372
373         ret = solo_snd_pcm_init(solo_dev);
374         if (ret < 0)
375                 goto snd_error;
376
377         ret = snd_card_register(card);
378         if (ret < 0)
379                 goto snd_error;
380
381         solo_g723_config(solo_dev);
382
383         dev_info(&solo_dev->pdev->dev, "Alsa sound card as %s\n", name);
384
385         return 0;
386
387 snd_error:
388         snd_card_free(card);
389         return ret;
390 }
391
392 void solo_g723_exit(struct solo_dev *solo_dev)
393 {
394         solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL, 0);
395         solo_irq_off(solo_dev, SOLO_IRQ_G723);
396
397         snd_card_free(solo_dev->snd_card);
398 }