GNU Linux-libre 4.14.294-gnu1
[releases.git] / sound / sh / sh_dac_audio.c
1 /*
2  * sh_dac_audio.c - SuperH DAC audio driver for ALSA
3  *
4  * Copyright (c) 2009 by Rafael Ignacio Zurita <rizurita@yahoo.com>
5  *
6  *
7  * Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh)
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */
24
25 #include <linux/hrtimer.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/initval.h>
33 #include <sound/pcm.h>
34 #include <sound/sh_dac_audio.h>
35 #include <asm/clock.h>
36 #include <asm/hd64461.h>
37 #include <mach/hp6xx.h>
38 #include <cpu/dac.h>
39
40 MODULE_AUTHOR("Rafael Ignacio Zurita <rizurita@yahoo.com>");
41 MODULE_DESCRIPTION("SuperH DAC audio driver");
42 MODULE_LICENSE("GPL");
43 MODULE_SUPPORTED_DEVICE("{{SuperH DAC audio support}}");
44
45 /* Module Parameters */
46 static int index = SNDRV_DEFAULT_IDX1;
47 static char *id = SNDRV_DEFAULT_STR1;
48 module_param(index, int, 0444);
49 MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
50 module_param(id, charp, 0444);
51 MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
52
53 /* main struct */
54 struct snd_sh_dac {
55         struct snd_card *card;
56         struct snd_pcm_substream *substream;
57         struct hrtimer hrtimer;
58         ktime_t wakeups_per_second;
59
60         int rate;
61         int empty;
62         char *data_buffer, *buffer_begin, *buffer_end;
63         int processed; /* bytes proccesed, to compare with period_size */
64         int buffer_size;
65         struct dac_audio_pdata *pdata;
66 };
67
68
69 static void dac_audio_start_timer(struct snd_sh_dac *chip)
70 {
71         hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
72                       HRTIMER_MODE_REL);
73 }
74
75 static void dac_audio_stop_timer(struct snd_sh_dac *chip)
76 {
77         hrtimer_cancel(&chip->hrtimer);
78 }
79
80 static void dac_audio_reset(struct snd_sh_dac *chip)
81 {
82         dac_audio_stop_timer(chip);
83         chip->buffer_begin = chip->buffer_end = chip->data_buffer;
84         chip->processed = 0;
85         chip->empty = 1;
86 }
87
88 static void dac_audio_set_rate(struct snd_sh_dac *chip)
89 {
90         chip->wakeups_per_second = 1000000000 / chip->rate;
91 }
92
93
94 /* PCM INTERFACE */
95
96 static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
97         .info                   = (SNDRV_PCM_INFO_MMAP |
98                                         SNDRV_PCM_INFO_MMAP_VALID |
99                                         SNDRV_PCM_INFO_INTERLEAVED |
100                                         SNDRV_PCM_INFO_HALF_DUPLEX),
101         .formats                = SNDRV_PCM_FMTBIT_U8,
102         .rates                  = SNDRV_PCM_RATE_8000,
103         .rate_min               = 8000,
104         .rate_max               = 8000,
105         .channels_min           = 1,
106         .channels_max           = 1,
107         .buffer_bytes_max       = (48*1024),
108         .period_bytes_min       = 1,
109         .period_bytes_max       = (48*1024),
110         .periods_min            = 1,
111         .periods_max            = 1024,
112 };
113
114 static int snd_sh_dac_pcm_open(struct snd_pcm_substream *substream)
115 {
116         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
117         struct snd_pcm_runtime *runtime = substream->runtime;
118
119         runtime->hw = snd_sh_dac_pcm_hw;
120
121         chip->substream = substream;
122         chip->buffer_begin = chip->buffer_end = chip->data_buffer;
123         chip->processed = 0;
124         chip->empty = 1;
125
126         chip->pdata->start(chip->pdata);
127
128         return 0;
129 }
130
131 static int snd_sh_dac_pcm_close(struct snd_pcm_substream *substream)
132 {
133         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
134
135         chip->substream = NULL;
136
137         dac_audio_stop_timer(chip);
138         chip->pdata->stop(chip->pdata);
139
140         return 0;
141 }
142
143 static int snd_sh_dac_pcm_hw_params(struct snd_pcm_substream *substream,
144                                 struct snd_pcm_hw_params *hw_params)
145 {
146         return snd_pcm_lib_malloc_pages(substream,
147                         params_buffer_bytes(hw_params));
148 }
149
150 static int snd_sh_dac_pcm_hw_free(struct snd_pcm_substream *substream)
151 {
152         return snd_pcm_lib_free_pages(substream);
153 }
154
155 static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream *substream)
156 {
157         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
158         struct snd_pcm_runtime *runtime = chip->substream->runtime;
159
160         chip->buffer_size = runtime->buffer_size;
161         memset(chip->data_buffer, 0, chip->pdata->buffer_size);
162
163         return 0;
164 }
165
166 static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
167 {
168         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
169
170         switch (cmd) {
171         case SNDRV_PCM_TRIGGER_START:
172                 dac_audio_start_timer(chip);
173                 break;
174         case SNDRV_PCM_TRIGGER_STOP:
175                 chip->buffer_begin = chip->buffer_end = chip->data_buffer;
176                 chip->processed = 0;
177                 chip->empty = 1;
178                 dac_audio_stop_timer(chip);
179                 break;
180         default:
181                  return -EINVAL;
182         }
183
184         return 0;
185 }
186
187 static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
188                                int channel, unsigned long pos,
189                                void __user *src, unsigned long count)
190 {
191         /* channel is not used (interleaved data) */
192         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
193
194         if (copy_from_user_toio(chip->data_buffer + pos, src, count))
195                 return -EFAULT;
196         chip->buffer_end = chip->data_buffer + pos + count;
197
198         if (chip->empty) {
199                 chip->empty = 0;
200                 dac_audio_start_timer(chip);
201         }
202
203         return 0;
204 }
205
206 static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
207                                       int channel, unsigned long pos,
208                                       void *src, unsigned long count)
209 {
210         /* channel is not used (interleaved data) */
211         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
212
213         memcpy_toio(chip->data_buffer + pos, src, count);
214         chip->buffer_end = chip->data_buffer + pos + count;
215
216         if (chip->empty) {
217                 chip->empty = 0;
218                 dac_audio_start_timer(chip);
219         }
220
221         return 0;
222 }
223
224 static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
225                                   int channel, unsigned long pos,
226                                   unsigned long count)
227 {
228         /* channel is not used (interleaved data) */
229         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
230
231         memset_io(chip->data_buffer + pos, 0, count);
232         chip->buffer_end = chip->data_buffer + pos + count;
233
234         if (chip->empty) {
235                 chip->empty = 0;
236                 dac_audio_start_timer(chip);
237         }
238
239         return 0;
240 }
241
242 static
243 snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
244 {
245         struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
246         int pointer = chip->buffer_begin - chip->data_buffer;
247
248         return pointer;
249 }
250
251 /* pcm ops */
252 static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
253         .open           = snd_sh_dac_pcm_open,
254         .close          = snd_sh_dac_pcm_close,
255         .ioctl          = snd_pcm_lib_ioctl,
256         .hw_params      = snd_sh_dac_pcm_hw_params,
257         .hw_free        = snd_sh_dac_pcm_hw_free,
258         .prepare        = snd_sh_dac_pcm_prepare,
259         .trigger        = snd_sh_dac_pcm_trigger,
260         .pointer        = snd_sh_dac_pcm_pointer,
261         .copy_user      = snd_sh_dac_pcm_copy,
262         .copy_kernel    = snd_sh_dac_pcm_copy_kernel,
263         .fill_silence   = snd_sh_dac_pcm_silence,
264         .mmap           = snd_pcm_lib_mmap_iomem,
265 };
266
267 static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
268 {
269         int err;
270         struct snd_pcm *pcm;
271
272         /* device should be always 0 for us */
273         err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
274         if (err < 0)
275                 return err;
276
277         pcm->private_data = chip;
278         strcpy(pcm->name, "SH_DAC PCM");
279         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
280
281         /* buffer size=48K */
282         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
283                                           snd_dma_continuous_data(GFP_KERNEL),
284                                                         48 * 1024,
285                                                         48 * 1024);
286
287         return 0;
288 }
289 /* END OF PCM INTERFACE */
290
291
292 /* driver .remove  --  destructor */
293 static int snd_sh_dac_remove(struct platform_device *devptr)
294 {
295         snd_card_free(platform_get_drvdata(devptr));
296         return 0;
297 }
298
299 /* free -- it has been defined by create */
300 static int snd_sh_dac_free(struct snd_sh_dac *chip)
301 {
302         /* release the data */
303         kfree(chip->data_buffer);
304         kfree(chip);
305
306         return 0;
307 }
308
309 static int snd_sh_dac_dev_free(struct snd_device *device)
310 {
311         struct snd_sh_dac *chip = device->device_data;
312
313         return snd_sh_dac_free(chip);
314 }
315
316 static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
317 {
318         struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
319                                                hrtimer);
320         struct snd_pcm_runtime *runtime = chip->substream->runtime;
321         ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
322
323         if (!chip->empty) {
324                 sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
325                 chip->buffer_begin++;
326
327                 chip->processed++;
328                 if (chip->processed >= b_ps) {
329                         chip->processed -= b_ps;
330                         snd_pcm_period_elapsed(chip->substream);
331                 }
332
333                 if (chip->buffer_begin == (chip->data_buffer +
334                                            chip->buffer_size - 1))
335                         chip->buffer_begin = chip->data_buffer;
336
337                 if (chip->buffer_begin == chip->buffer_end)
338                         chip->empty = 1;
339
340         }
341
342         if (!chip->empty)
343                 hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
344                               HRTIMER_MODE_REL);
345
346         return HRTIMER_NORESTART;
347 }
348
349 /* create  --  chip-specific constructor for the cards components */
350 static int snd_sh_dac_create(struct snd_card *card,
351                              struct platform_device *devptr,
352                              struct snd_sh_dac **rchip)
353 {
354         struct snd_sh_dac *chip;
355         int err;
356
357         static struct snd_device_ops ops = {
358                    .dev_free = snd_sh_dac_dev_free,
359         };
360
361         *rchip = NULL;
362
363         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
364         if (chip == NULL)
365                 return -ENOMEM;
366
367         chip->card = card;
368
369         hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
370         chip->hrtimer.function = sh_dac_audio_timer;
371
372         dac_audio_reset(chip);
373         chip->rate = 8000;
374         dac_audio_set_rate(chip);
375
376         chip->pdata = devptr->dev.platform_data;
377
378         chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
379         if (chip->data_buffer == NULL) {
380                 kfree(chip);
381                 return -ENOMEM;
382         }
383
384         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
385         if (err < 0) {
386                 snd_sh_dac_free(chip);
387                 return err;
388         }
389
390         *rchip = chip;
391
392         return 0;
393 }
394
395 /* driver .probe  --  constructor */
396 static int snd_sh_dac_probe(struct platform_device *devptr)
397 {
398         struct snd_sh_dac *chip;
399         struct snd_card *card;
400         int err;
401
402         err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
403         if (err < 0) {
404                         snd_printk(KERN_ERR "cannot allocate the card\n");
405                         return err;
406         }
407
408         err = snd_sh_dac_create(card, devptr, &chip);
409         if (err < 0)
410                 goto probe_error;
411
412         err = snd_sh_dac_pcm(chip, 0);
413         if (err < 0)
414                 goto probe_error;
415
416         strcpy(card->driver, "snd_sh_dac");
417         strcpy(card->shortname, "SuperH DAC audio driver");
418         printk(KERN_INFO "%s %s", card->longname, card->shortname);
419
420         err = snd_card_register(card);
421         if (err < 0)
422                 goto probe_error;
423
424         snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
425
426         platform_set_drvdata(devptr, card);
427         return 0;
428
429 probe_error:
430         snd_card_free(card);
431         return err;
432 }
433
434 /*
435  * "driver" definition
436  */
437 static struct platform_driver sh_dac_driver = {
438         .probe  = snd_sh_dac_probe,
439         .remove = snd_sh_dac_remove,
440         .driver = {
441                 .name = "dac_audio",
442         },
443 };
444
445 module_platform_driver(sh_dac_driver);