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