blob: 56bcb46abf0d255693d9b3bec415b328c781abde [file] [log] [blame]
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -03001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040030#include <linux/module.h>
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -030031#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
40MODULE_AUTHOR("Rafael Ignacio Zurita <rizurita@yahoo.com>");
41MODULE_DESCRIPTION("SuperH DAC audio driver");
42MODULE_LICENSE("GPL");
43MODULE_SUPPORTED_DEVICE("{{SuperH DAC audio support}}");
44
45/* Module Parameters */
46static int index = SNDRV_DEFAULT_IDX1;
47static char *id = SNDRV_DEFAULT_STR1;
48module_param(index, int, 0444);
49MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
50module_param(id, charp, 0444);
51MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
52
53/* main struct */
54struct 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
69static 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
75static void dac_audio_stop_timer(struct snd_sh_dac *chip)
76{
77 hrtimer_cancel(&chip->hrtimer);
78}
79
80static 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
88static void dac_audio_set_rate(struct snd_sh_dac *chip)
89{
90 chip->wakeups_per_second = ktime_set(0, 1000000000 / chip->rate);
91}
92
93
94/* PCM INTERFACE */
95
96static 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
114static 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
131static 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
143static 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
150static int snd_sh_dac_pcm_hw_free(struct snd_pcm_substream *substream)
151{
152 return snd_pcm_lib_free_pages(substream);
153}
154
155static 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
166static 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
187static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream, int channel,
188 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
189{
190 /* channel is not used (interleaved data) */
191 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
192 struct snd_pcm_runtime *runtime = substream->runtime;
193 ssize_t b_count = frames_to_bytes(runtime , count);
194 ssize_t b_pos = frames_to_bytes(runtime , pos);
195
196 if (count < 0)
197 return -EINVAL;
198
199 if (!count)
200 return 0;
201
202 memcpy_toio(chip->data_buffer + b_pos, src, b_count);
203 chip->buffer_end = chip->data_buffer + b_pos + b_count;
204
205 if (chip->empty) {
206 chip->empty = 0;
207 dac_audio_start_timer(chip);
208 }
209
210 return 0;
211}
212
213static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
214 int channel, snd_pcm_uframes_t pos,
215 snd_pcm_uframes_t count)
216{
217 /* channel is not used (interleaved data) */
218 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
219 struct snd_pcm_runtime *runtime = substream->runtime;
220 ssize_t b_count = frames_to_bytes(runtime , count);
221 ssize_t b_pos = frames_to_bytes(runtime , pos);
222
223 if (count < 0)
224 return -EINVAL;
225
226 if (!count)
227 return 0;
228
229 memset_io(chip->data_buffer + b_pos, 0, b_count);
230 chip->buffer_end = chip->data_buffer + b_pos + b_count;
231
232 if (chip->empty) {
233 chip->empty = 0;
234 dac_audio_start_timer(chip);
235 }
236
237 return 0;
238}
239
240static
241snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
242{
243 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
244 int pointer = chip->buffer_begin - chip->data_buffer;
245
246 return pointer;
247}
248
249/* pcm ops */
250static struct snd_pcm_ops snd_sh_dac_pcm_ops = {
251 .open = snd_sh_dac_pcm_open,
252 .close = snd_sh_dac_pcm_close,
253 .ioctl = snd_pcm_lib_ioctl,
254 .hw_params = snd_sh_dac_pcm_hw_params,
255 .hw_free = snd_sh_dac_pcm_hw_free,
256 .prepare = snd_sh_dac_pcm_prepare,
257 .trigger = snd_sh_dac_pcm_trigger,
258 .pointer = snd_sh_dac_pcm_pointer,
259 .copy = snd_sh_dac_pcm_copy,
260 .silence = snd_sh_dac_pcm_silence,
261 .mmap = snd_pcm_lib_mmap_iomem,
262};
263
264static int __devinit snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
265{
266 int err;
267 struct snd_pcm *pcm;
268
269 /* device should be always 0 for us */
270 err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
271 if (err < 0)
272 return err;
273
274 pcm->private_data = chip;
275 strcpy(pcm->name, "SH_DAC PCM");
276 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
277
278 /* buffer size=48K */
279 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
280 snd_dma_continuous_data(GFP_KERNEL),
281 48 * 1024,
282 48 * 1024);
283
284 return 0;
285}
286/* END OF PCM INTERFACE */
287
288
289/* driver .remove -- destructor */
290static int snd_sh_dac_remove(struct platform_device *devptr)
291{
292 snd_card_free(platform_get_drvdata(devptr));
293 platform_set_drvdata(devptr, NULL);
294
295 return 0;
296}
297
298/* free -- it has been defined by create */
299static int snd_sh_dac_free(struct snd_sh_dac *chip)
300{
301 /* release the data */
302 kfree(chip->data_buffer);
303 kfree(chip);
304
305 return 0;
306}
307
308static int snd_sh_dac_dev_free(struct snd_device *device)
309{
310 struct snd_sh_dac *chip = device->device_data;
311
312 return snd_sh_dac_free(chip);
313}
314
315static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
316{
317 struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
318 hrtimer);
319 struct snd_pcm_runtime *runtime = chip->substream->runtime;
320 ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
321
322 if (!chip->empty) {
323 sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
324 chip->buffer_begin++;
325
326 chip->processed++;
327 if (chip->processed >= b_ps) {
328 chip->processed -= b_ps;
329 snd_pcm_period_elapsed(chip->substream);
330 }
331
332 if (chip->buffer_begin == (chip->data_buffer +
333 chip->buffer_size - 1))
334 chip->buffer_begin = chip->data_buffer;
335
336 if (chip->buffer_begin == chip->buffer_end)
337 chip->empty = 1;
338
339 }
340
341 if (!chip->empty)
342 hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
343 HRTIMER_MODE_REL);
344
345 return HRTIMER_NORESTART;
346}
347
348/* create -- chip-specific constructor for the cards components */
349static int __devinit snd_sh_dac_create(struct snd_card *card,
350 struct platform_device *devptr,
351 struct snd_sh_dac **rchip)
352{
353 struct snd_sh_dac *chip;
354 int err;
355
356 static struct snd_device_ops ops = {
357 .dev_free = snd_sh_dac_dev_free,
358 };
359
360 *rchip = NULL;
361
362 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
363 if (chip == NULL)
364 return -ENOMEM;
365
366 chip->card = card;
367
368 hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
369 chip->hrtimer.function = sh_dac_audio_timer;
370
371 dac_audio_reset(chip);
372 chip->rate = 8000;
373 dac_audio_set_rate(chip);
374
375 chip->pdata = devptr->dev.platform_data;
376
377 chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
378 if (chip->data_buffer == NULL) {
379 kfree(chip);
380 return -ENOMEM;
381 }
382
383 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
384 if (err < 0) {
385 snd_sh_dac_free(chip);
386 return err;
387 }
388
389 *rchip = chip;
390
391 return 0;
392}
393
394/* driver .probe -- constructor */
395static int __devinit snd_sh_dac_probe(struct platform_device *devptr)
396{
397 struct snd_sh_dac *chip;
398 struct snd_card *card;
399 int err;
400
401 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
402 if (err < 0) {
403 snd_printk(KERN_ERR "cannot allocate the card\n");
404 return err;
405 }
406
407 err = snd_sh_dac_create(card, devptr, &chip);
408 if (err < 0)
409 goto probe_error;
410
411 err = snd_sh_dac_pcm(chip, 0);
412 if (err < 0)
413 goto probe_error;
414
415 strcpy(card->driver, "snd_sh_dac");
416 strcpy(card->shortname, "SuperH DAC audio driver");
417 printk(KERN_INFO "%s %s", card->longname, card->shortname);
418
419 err = snd_card_register(card);
420 if (err < 0)
421 goto probe_error;
422
423 snd_printk("ALSA driver for SuperH DAC audio");
424
425 platform_set_drvdata(devptr, card);
426 return 0;
427
428probe_error:
429 snd_card_free(card);
430 return err;
431}
432
433/*
434 * "driver" definition
435 */
436static struct platform_driver driver = {
437 .probe = snd_sh_dac_probe,
438 .remove = snd_sh_dac_remove,
439 .driver = {
440 .name = "dac_audio",
441 },
442};
443
444static int __init sh_dac_init(void)
445{
446 return platform_driver_register(&driver);
447}
448
449static void __exit sh_dac_exit(void)
450{
451 platform_driver_unregister(&driver);
452}
453
454module_init(sh_dac_init);
455module_exit(sh_dac_exit);