1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sh_dac_audio.c - SuperH DAC audio driver for ALSA
5 * Copyright (c) 2009 by Rafael Ignacio Zurita <rizurita@yahoo.com>
7 * Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh)
10 #include <linux/hrtimer.h>
11 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <sound/core.h>
17 #include <sound/initval.h>
18 #include <sound/pcm.h>
19 #include <sound/sh_dac_audio.h>
20 #include <asm/clock.h>
21 #include <asm/hd64461.h>
22 #include <mach/hp6xx.h>
25 MODULE_AUTHOR("Rafael Ignacio Zurita <rizurita@yahoo.com>");
26 MODULE_DESCRIPTION("SuperH DAC audio driver");
27 MODULE_LICENSE("GPL");
29 /* Module Parameters */
30 static int index = SNDRV_DEFAULT_IDX1;
31 static char *id = SNDRV_DEFAULT_STR1;
32 module_param(index, int, 0444);
33 MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
34 module_param(id, charp, 0444);
35 MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
39 struct snd_card *card;
40 struct snd_pcm_substream *substream;
41 struct hrtimer hrtimer;
42 ktime_t wakeups_per_second;
46 char *data_buffer, *buffer_begin, *buffer_end;
47 int processed; /* bytes proccesed, to compare with period_size */
49 struct dac_audio_pdata *pdata;
53 static void dac_audio_start_timer(struct snd_sh_dac *chip)
55 hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
59 static void dac_audio_stop_timer(struct snd_sh_dac *chip)
61 hrtimer_cancel(&chip->hrtimer);
64 static void dac_audio_reset(struct snd_sh_dac *chip)
66 dac_audio_stop_timer(chip);
67 chip->buffer_begin = chip->buffer_end = chip->data_buffer;
72 static void dac_audio_set_rate(struct snd_sh_dac *chip)
74 chip->wakeups_per_second = 1000000000 / chip->rate;
80 static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
81 .info = (SNDRV_PCM_INFO_MMAP |
82 SNDRV_PCM_INFO_MMAP_VALID |
83 SNDRV_PCM_INFO_INTERLEAVED |
84 SNDRV_PCM_INFO_HALF_DUPLEX),
85 .formats = SNDRV_PCM_FMTBIT_U8,
86 .rates = SNDRV_PCM_RATE_8000,
91 .buffer_bytes_max = (48*1024),
92 .period_bytes_min = 1,
93 .period_bytes_max = (48*1024),
98 static int snd_sh_dac_pcm_open(struct snd_pcm_substream *substream)
100 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
101 struct snd_pcm_runtime *runtime = substream->runtime;
103 runtime->hw = snd_sh_dac_pcm_hw;
105 chip->substream = substream;
106 chip->buffer_begin = chip->buffer_end = chip->data_buffer;
110 chip->pdata->start(chip->pdata);
115 static int snd_sh_dac_pcm_close(struct snd_pcm_substream *substream)
117 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
119 chip->substream = NULL;
121 dac_audio_stop_timer(chip);
122 chip->pdata->stop(chip->pdata);
127 static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream *substream)
129 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
130 struct snd_pcm_runtime *runtime = chip->substream->runtime;
132 chip->buffer_size = runtime->buffer_size;
133 memset(chip->data_buffer, 0, chip->pdata->buffer_size);
138 static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
140 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
143 case SNDRV_PCM_TRIGGER_START:
144 dac_audio_start_timer(chip);
146 case SNDRV_PCM_TRIGGER_STOP:
147 chip->buffer_begin = chip->buffer_end = chip->data_buffer;
150 dac_audio_stop_timer(chip);
159 static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
160 int channel, unsigned long pos,
161 void __user *src, unsigned long count)
163 /* channel is not used (interleaved data) */
164 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
166 if (copy_from_user_toio(chip->data_buffer + pos, src, count))
168 chip->buffer_end = chip->data_buffer + pos + count;
172 dac_audio_start_timer(chip);
178 static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
179 int channel, unsigned long pos,
180 void *src, unsigned long count)
182 /* channel is not used (interleaved data) */
183 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
185 memcpy_toio(chip->data_buffer + pos, src, count);
186 chip->buffer_end = chip->data_buffer + pos + count;
190 dac_audio_start_timer(chip);
196 static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
197 int channel, unsigned long pos,
200 /* channel is not used (interleaved data) */
201 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
203 memset_io(chip->data_buffer + pos, 0, count);
204 chip->buffer_end = chip->data_buffer + pos + count;
208 dac_audio_start_timer(chip);
215 snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
217 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
218 int pointer = chip->buffer_begin - chip->data_buffer;
224 static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
225 .open = snd_sh_dac_pcm_open,
226 .close = snd_sh_dac_pcm_close,
227 .prepare = snd_sh_dac_pcm_prepare,
228 .trigger = snd_sh_dac_pcm_trigger,
229 .pointer = snd_sh_dac_pcm_pointer,
230 .copy_user = snd_sh_dac_pcm_copy,
231 .copy_kernel = snd_sh_dac_pcm_copy_kernel,
232 .fill_silence = snd_sh_dac_pcm_silence,
233 .mmap = snd_pcm_lib_mmap_iomem,
236 static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
241 /* device should be always 0 for us */
242 err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
246 pcm->private_data = chip;
247 strcpy(pcm->name, "SH_DAC PCM");
248 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
250 /* buffer size=48K */
251 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
252 NULL, 48 * 1024, 48 * 1024);
256 /* END OF PCM INTERFACE */
259 /* driver .remove -- destructor */
260 static int snd_sh_dac_remove(struct platform_device *devptr)
262 snd_card_free(platform_get_drvdata(devptr));
266 /* free -- it has been defined by create */
267 static int snd_sh_dac_free(struct snd_sh_dac *chip)
269 /* release the data */
270 kfree(chip->data_buffer);
276 static int snd_sh_dac_dev_free(struct snd_device *device)
278 struct snd_sh_dac *chip = device->device_data;
280 return snd_sh_dac_free(chip);
283 static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
285 struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
287 struct snd_pcm_runtime *runtime = chip->substream->runtime;
288 ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
291 sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
292 chip->buffer_begin++;
295 if (chip->processed >= b_ps) {
296 chip->processed -= b_ps;
297 snd_pcm_period_elapsed(chip->substream);
300 if (chip->buffer_begin == (chip->data_buffer +
301 chip->buffer_size - 1))
302 chip->buffer_begin = chip->data_buffer;
304 if (chip->buffer_begin == chip->buffer_end)
310 hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
313 return HRTIMER_NORESTART;
316 /* create -- chip-specific constructor for the cards components */
317 static int snd_sh_dac_create(struct snd_card *card,
318 struct platform_device *devptr,
319 struct snd_sh_dac **rchip)
321 struct snd_sh_dac *chip;
324 static const struct snd_device_ops ops = {
325 .dev_free = snd_sh_dac_dev_free,
330 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
336 hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
337 chip->hrtimer.function = sh_dac_audio_timer;
339 dac_audio_reset(chip);
341 dac_audio_set_rate(chip);
343 chip->pdata = devptr->dev.platform_data;
345 chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
346 if (chip->data_buffer == NULL) {
351 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
353 snd_sh_dac_free(chip);
362 /* driver .probe -- constructor */
363 static int snd_sh_dac_probe(struct platform_device *devptr)
365 struct snd_sh_dac *chip;
366 struct snd_card *card;
369 err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
371 snd_printk(KERN_ERR "cannot allocate the card\n");
375 err = snd_sh_dac_create(card, devptr, &chip);
379 err = snd_sh_dac_pcm(chip, 0);
383 strcpy(card->driver, "snd_sh_dac");
384 strcpy(card->shortname, "SuperH DAC audio driver");
385 printk(KERN_INFO "%s %s", card->longname, card->shortname);
387 err = snd_card_register(card);
391 snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
393 platform_set_drvdata(devptr, card);
402 * "driver" definition
404 static struct platform_driver sh_dac_driver = {
405 .probe = snd_sh_dac_probe,
406 .remove = snd_sh_dac_remove,
412 module_platform_driver(sh_dac_driver);