2 * sh_dac_audio.c - SuperH DAC audio driver for ALSA
4 * Copyright (c) 2009 by Rafael Ignacio Zurita <rizurita@yahoo.com>
7 * Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh)
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.
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.
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
25 #include <linux/hrtimer.h>
26 #include <linux/interrupt.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>
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}}");
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.");
55 struct snd_card
*card
;
56 struct snd_pcm_substream
*substream
;
57 struct hrtimer hrtimer
;
58 ktime_t wakeups_per_second
;
62 char *data_buffer
, *buffer_begin
, *buffer_end
;
63 int processed
; /* bytes proccesed, to compare with period_size */
65 struct dac_audio_pdata
*pdata
;
69 static void dac_audio_start_timer(struct snd_sh_dac
*chip
)
71 hrtimer_start(&chip
->hrtimer
, chip
->wakeups_per_second
,
75 static void dac_audio_stop_timer(struct snd_sh_dac
*chip
)
77 hrtimer_cancel(&chip
->hrtimer
);
80 static void dac_audio_reset(struct snd_sh_dac
*chip
)
82 dac_audio_stop_timer(chip
);
83 chip
->buffer_begin
= chip
->buffer_end
= chip
->data_buffer
;
88 static void dac_audio_set_rate(struct snd_sh_dac
*chip
)
90 chip
->wakeups_per_second
= 1000000000 / chip
->rate
;
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
,
107 .buffer_bytes_max
= (48*1024),
108 .period_bytes_min
= 1,
109 .period_bytes_max
= (48*1024),
114 static int snd_sh_dac_pcm_open(struct snd_pcm_substream
*substream
)
116 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
117 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
119 runtime
->hw
= snd_sh_dac_pcm_hw
;
121 chip
->substream
= substream
;
122 chip
->buffer_begin
= chip
->buffer_end
= chip
->data_buffer
;
126 chip
->pdata
->start(chip
->pdata
);
131 static int snd_sh_dac_pcm_close(struct snd_pcm_substream
*substream
)
133 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
135 chip
->substream
= NULL
;
137 dac_audio_stop_timer(chip
);
138 chip
->pdata
->stop(chip
->pdata
);
143 static int snd_sh_dac_pcm_hw_params(struct snd_pcm_substream
*substream
,
144 struct snd_pcm_hw_params
*hw_params
)
146 return snd_pcm_lib_malloc_pages(substream
,
147 params_buffer_bytes(hw_params
));
150 static int snd_sh_dac_pcm_hw_free(struct snd_pcm_substream
*substream
)
152 return snd_pcm_lib_free_pages(substream
);
155 static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream
*substream
)
157 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
158 struct snd_pcm_runtime
*runtime
= chip
->substream
->runtime
;
160 chip
->buffer_size
= runtime
->buffer_size
;
161 memset(chip
->data_buffer
, 0, chip
->pdata
->buffer_size
);
166 static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
168 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
171 case SNDRV_PCM_TRIGGER_START
:
172 dac_audio_start_timer(chip
);
174 case SNDRV_PCM_TRIGGER_STOP
:
175 chip
->buffer_begin
= chip
->buffer_end
= chip
->data_buffer
;
178 dac_audio_stop_timer(chip
);
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
)
191 /* channel is not used (interleaved data) */
192 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
193 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
195 if (copy_from_user_toio(chip
->data_buffer
+ pos
, src
, count
))
197 chip
->buffer_end
= chip
->data_buffer
+ pos
+ count
;
201 dac_audio_start_timer(chip
);
207 static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream
*substream
,
208 int channel
, unsigned long pos
,
209 void *src
, unsigned long count
)
211 /* channel is not used (interleaved data) */
212 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
213 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
215 memcpy_toio(chip
->data_buffer
+ pos
, src
, count
);
216 chip
->buffer_end
= chip
->data_buffer
+ pos
+ count
;
220 dac_audio_start_timer(chip
);
226 static int snd_sh_dac_pcm_silence(struct snd_pcm_substream
*substream
,
227 int channel
, unsigned long pos
,
230 /* channel is not used (interleaved data) */
231 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
232 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
234 memset_io(chip
->data_buffer
+ pos
, 0, count
);
235 chip
->buffer_end
= chip
->data_buffer
+ pos
+ count
;
239 dac_audio_start_timer(chip
);
246 snd_pcm_uframes_t
snd_sh_dac_pcm_pointer(struct snd_pcm_substream
*substream
)
248 struct snd_sh_dac
*chip
= snd_pcm_substream_chip(substream
);
249 int pointer
= chip
->buffer_begin
- chip
->data_buffer
;
255 static const struct snd_pcm_ops snd_sh_dac_pcm_ops
= {
256 .open
= snd_sh_dac_pcm_open
,
257 .close
= snd_sh_dac_pcm_close
,
258 .ioctl
= snd_pcm_lib_ioctl
,
259 .hw_params
= snd_sh_dac_pcm_hw_params
,
260 .hw_free
= snd_sh_dac_pcm_hw_free
,
261 .prepare
= snd_sh_dac_pcm_prepare
,
262 .trigger
= snd_sh_dac_pcm_trigger
,
263 .pointer
= snd_sh_dac_pcm_pointer
,
264 .copy_user
= snd_sh_dac_pcm_copy
,
265 .copy_kernel
= snd_sh_dac_pcm_copy_kernel
,
266 .fill_silence
= snd_sh_dac_pcm_silence
,
267 .mmap
= snd_pcm_lib_mmap_iomem
,
270 static int snd_sh_dac_pcm(struct snd_sh_dac
*chip
, int device
)
275 /* device should be always 0 for us */
276 err
= snd_pcm_new(chip
->card
, "SH_DAC PCM", device
, 1, 0, &pcm
);
280 pcm
->private_data
= chip
;
281 strcpy(pcm
->name
, "SH_DAC PCM");
282 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_sh_dac_pcm_ops
);
284 /* buffer size=48K */
285 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
286 snd_dma_continuous_data(GFP_KERNEL
),
292 /* END OF PCM INTERFACE */
295 /* driver .remove -- destructor */
296 static int snd_sh_dac_remove(struct platform_device
*devptr
)
298 snd_card_free(platform_get_drvdata(devptr
));
302 /* free -- it has been defined by create */
303 static int snd_sh_dac_free(struct snd_sh_dac
*chip
)
305 /* release the data */
306 kfree(chip
->data_buffer
);
312 static int snd_sh_dac_dev_free(struct snd_device
*device
)
314 struct snd_sh_dac
*chip
= device
->device_data
;
316 return snd_sh_dac_free(chip
);
319 static enum hrtimer_restart
sh_dac_audio_timer(struct hrtimer
*handle
)
321 struct snd_sh_dac
*chip
= container_of(handle
, struct snd_sh_dac
,
323 struct snd_pcm_runtime
*runtime
= chip
->substream
->runtime
;
324 ssize_t b_ps
= frames_to_bytes(runtime
, runtime
->period_size
);
327 sh_dac_output(*chip
->buffer_begin
, chip
->pdata
->channel
);
328 chip
->buffer_begin
++;
331 if (chip
->processed
>= b_ps
) {
332 chip
->processed
-= b_ps
;
333 snd_pcm_period_elapsed(chip
->substream
);
336 if (chip
->buffer_begin
== (chip
->data_buffer
+
337 chip
->buffer_size
- 1))
338 chip
->buffer_begin
= chip
->data_buffer
;
340 if (chip
->buffer_begin
== chip
->buffer_end
)
346 hrtimer_start(&chip
->hrtimer
, chip
->wakeups_per_second
,
349 return HRTIMER_NORESTART
;
352 /* create -- chip-specific constructor for the cards components */
353 static int snd_sh_dac_create(struct snd_card
*card
,
354 struct platform_device
*devptr
,
355 struct snd_sh_dac
**rchip
)
357 struct snd_sh_dac
*chip
;
360 static struct snd_device_ops ops
= {
361 .dev_free
= snd_sh_dac_dev_free
,
366 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
372 hrtimer_init(&chip
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
373 chip
->hrtimer
.function
= sh_dac_audio_timer
;
375 dac_audio_reset(chip
);
377 dac_audio_set_rate(chip
);
379 chip
->pdata
= devptr
->dev
.platform_data
;
381 chip
->data_buffer
= kmalloc(chip
->pdata
->buffer_size
, GFP_KERNEL
);
382 if (chip
->data_buffer
== NULL
) {
387 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
389 snd_sh_dac_free(chip
);
398 /* driver .probe -- constructor */
399 static int snd_sh_dac_probe(struct platform_device
*devptr
)
401 struct snd_sh_dac
*chip
;
402 struct snd_card
*card
;
405 err
= snd_card_new(&devptr
->dev
, index
, id
, THIS_MODULE
, 0, &card
);
407 snd_printk(KERN_ERR
"cannot allocate the card\n");
411 err
= snd_sh_dac_create(card
, devptr
, &chip
);
415 err
= snd_sh_dac_pcm(chip
, 0);
419 strcpy(card
->driver
, "snd_sh_dac");
420 strcpy(card
->shortname
, "SuperH DAC audio driver");
421 printk(KERN_INFO
"%s %s", card
->longname
, card
->shortname
);
423 err
= snd_card_register(card
);
427 snd_printk(KERN_INFO
"ALSA driver for SuperH DAC audio");
429 platform_set_drvdata(devptr
, card
);
438 * "driver" definition
440 static struct platform_driver sh_dac_driver
= {
441 .probe
= snd_sh_dac_probe
,
442 .remove
= snd_sh_dac_remove
,
448 module_platform_driver(sh_dac_driver
);