2019-05-27 06:55:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-11-03 20:16:27 +00:00
|
|
|
/*
|
|
|
|
* sh_dac_audio.c - SuperH DAC audio driver for ALSA
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 by Rafael Ignacio Zurita <rizurita@yahoo.com>
|
|
|
|
*
|
|
|
|
* Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/hrtimer.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/platform_device.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2011-07-15 16:38:28 +00:00
|
|
|
#include <linux/module.h>
|
2009-11-03 20:16:27 +00:00
|
|
|
#include <sound/core.h>
|
|
|
|
#include <sound/initval.h>
|
|
|
|
#include <sound/pcm.h>
|
|
|
|
#include <sound/sh_dac_audio.h>
|
|
|
|
#include <asm/clock.h>
|
|
|
|
#include <asm/hd64461.h>
|
|
|
|
#include <mach/hp6xx.h>
|
|
|
|
#include <cpu/dac.h>
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Rafael Ignacio Zurita <rizurita@yahoo.com>");
|
|
|
|
MODULE_DESCRIPTION("SuperH DAC audio driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
/* Module Parameters */
|
|
|
|
static int index = SNDRV_DEFAULT_IDX1;
|
|
|
|
static char *id = SNDRV_DEFAULT_STR1;
|
|
|
|
module_param(index, int, 0444);
|
|
|
|
MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
|
|
|
|
module_param(id, charp, 0444);
|
|
|
|
MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
|
|
|
|
|
|
|
|
/* main struct */
|
|
|
|
struct snd_sh_dac {
|
|
|
|
struct snd_card *card;
|
|
|
|
struct snd_pcm_substream *substream;
|
|
|
|
struct hrtimer hrtimer;
|
|
|
|
ktime_t wakeups_per_second;
|
|
|
|
|
|
|
|
int rate;
|
|
|
|
int empty;
|
|
|
|
char *data_buffer, *buffer_begin, *buffer_end;
|
|
|
|
int processed; /* bytes proccesed, to compare with period_size */
|
|
|
|
int buffer_size;
|
|
|
|
struct dac_audio_pdata *pdata;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void dac_audio_start_timer(struct snd_sh_dac *chip)
|
|
|
|
{
|
|
|
|
hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
|
|
|
|
HRTIMER_MODE_REL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dac_audio_stop_timer(struct snd_sh_dac *chip)
|
|
|
|
{
|
|
|
|
hrtimer_cancel(&chip->hrtimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dac_audio_reset(struct snd_sh_dac *chip)
|
|
|
|
{
|
|
|
|
dac_audio_stop_timer(chip);
|
|
|
|
chip->buffer_begin = chip->buffer_end = chip->data_buffer;
|
|
|
|
chip->processed = 0;
|
|
|
|
chip->empty = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dac_audio_set_rate(struct snd_sh_dac *chip)
|
|
|
|
{
|
2016-12-25 11:30:41 +00:00
|
|
|
chip->wakeups_per_second = 1000000000 / chip->rate;
|
2009-11-03 20:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* PCM INTERFACE */
|
|
|
|
|
2017-08-17 09:15:57 +00:00
|
|
|
static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
|
2009-11-03 20:16:27 +00:00
|
|
|
.info = (SNDRV_PCM_INFO_MMAP |
|
|
|
|
SNDRV_PCM_INFO_MMAP_VALID |
|
|
|
|
SNDRV_PCM_INFO_INTERLEAVED |
|
|
|
|
SNDRV_PCM_INFO_HALF_DUPLEX),
|
|
|
|
.formats = SNDRV_PCM_FMTBIT_U8,
|
|
|
|
.rates = SNDRV_PCM_RATE_8000,
|
|
|
|
.rate_min = 8000,
|
|
|
|
.rate_max = 8000,
|
|
|
|
.channels_min = 1,
|
|
|
|
.channels_max = 1,
|
|
|
|
.buffer_bytes_max = (48*1024),
|
|
|
|
.period_bytes_min = 1,
|
|
|
|
.period_bytes_max = (48*1024),
|
|
|
|
.periods_min = 1,
|
|
|
|
.periods_max = 1024,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int snd_sh_dac_pcm_open(struct snd_pcm_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
|
|
|
|
struct snd_pcm_runtime *runtime = substream->runtime;
|
|
|
|
|
|
|
|
runtime->hw = snd_sh_dac_pcm_hw;
|
|
|
|
|
|
|
|
chip->substream = substream;
|
|
|
|
chip->buffer_begin = chip->buffer_end = chip->data_buffer;
|
|
|
|
chip->processed = 0;
|
|
|
|
chip->empty = 1;
|
|
|
|
|
|
|
|
chip->pdata->start(chip->pdata);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_sh_dac_pcm_close(struct snd_pcm_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
|
|
|
|
|
|
|
|
chip->substream = NULL;
|
|
|
|
|
|
|
|
dac_audio_stop_timer(chip);
|
|
|
|
chip->pdata->stop(chip->pdata);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
|
|
|
|
struct snd_pcm_runtime *runtime = chip->substream->runtime;
|
|
|
|
|
|
|
|
chip->buffer_size = runtime->buffer_size;
|
|
|
|
memset(chip->data_buffer, 0, chip->pdata->buffer_size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
|
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SNDRV_PCM_TRIGGER_START:
|
|
|
|
dac_audio_start_timer(chip);
|
|
|
|
break;
|
|
|
|
case SNDRV_PCM_TRIGGER_STOP:
|
|
|
|
chip->buffer_begin = chip->buffer_end = chip->data_buffer;
|
|
|
|
chip->processed = 0;
|
|
|
|
chip->empty = 1;
|
|
|
|
dac_audio_stop_timer(chip);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-10 19:57:16 +00:00
|
|
|
static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
|
|
|
|
int channel, unsigned long pos,
|
2023-08-15 19:01:25 +00:00
|
|
|
struct iov_iter *src, unsigned long count)
|
2009-11-03 20:16:27 +00:00
|
|
|
{
|
|
|
|
/* channel is not used (interleaved data) */
|
|
|
|
struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
|
|
|
|
|
2023-08-15 19:01:25 +00:00
|
|
|
if (copy_from_iter_toio(chip->data_buffer + pos, src, count))
|
2017-05-10 19:57:16 +00:00
|
|
|
return -EFAULT;
|
|
|
|
chip->buffer_end = chip->data_buffer + pos + count;
|
2009-11-03 20:16:27 +00:00
|
|
|
|
2017-05-10 19:57:16 +00:00
|
|
|
if (chip->empty) {
|
|
|
|
chip->empty = 0;
|
|
|
|
dac_audio_start_timer(chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-03 20:16:27 +00:00
|
|
|
|
|
|
|
static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
|
2017-05-10 19:57:16 +00:00
|
|
|
int channel, unsigned long pos,
|
|
|
|
unsigned long count)
|
2009-11-03 20:16:27 +00:00
|
|
|
{
|
|
|
|
/* channel is not used (interleaved data) */
|
|
|
|
struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
|
|
|
|
|
2017-05-10 19:57:16 +00:00
|
|
|
memset_io(chip->data_buffer + pos, 0, count);
|
|
|
|
chip->buffer_end = chip->data_buffer + pos + count;
|
2009-11-03 20:16:27 +00:00
|
|
|
|
|
|
|
if (chip->empty) {
|
|
|
|
chip->empty = 0;
|
|
|
|
dac_audio_start_timer(chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
|
|
|
|
int pointer = chip->buffer_begin - chip->data_buffer;
|
|
|
|
|
|
|
|
return pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pcm ops */
|
2017-08-18 07:45:18 +00:00
|
|
|
static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
|
2009-11-03 20:16:27 +00:00
|
|
|
.open = snd_sh_dac_pcm_open,
|
|
|
|
.close = snd_sh_dac_pcm_close,
|
|
|
|
.prepare = snd_sh_dac_pcm_prepare,
|
|
|
|
.trigger = snd_sh_dac_pcm_trigger,
|
|
|
|
.pointer = snd_sh_dac_pcm_pointer,
|
2023-08-15 19:01:25 +00:00
|
|
|
.copy = snd_sh_dac_pcm_copy,
|
2017-05-10 19:57:16 +00:00
|
|
|
.fill_silence = snd_sh_dac_pcm_silence,
|
2009-11-03 20:16:27 +00:00
|
|
|
.mmap = snd_pcm_lib_mmap_iomem,
|
|
|
|
};
|
|
|
|
|
2012-12-06 17:35:24 +00:00
|
|
|
static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
|
2009-11-03 20:16:27 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct snd_pcm *pcm;
|
|
|
|
|
|
|
|
/* device should be always 0 for us */
|
|
|
|
err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
pcm->private_data = chip;
|
|
|
|
strcpy(pcm->name, "SH_DAC PCM");
|
|
|
|
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
|
|
|
|
|
|
|
|
/* buffer size=48K */
|
2019-12-09 09:49:33 +00:00
|
|
|
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
|
|
|
|
NULL, 48 * 1024, 48 * 1024);
|
2009-11-03 20:16:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* END OF PCM INTERFACE */
|
|
|
|
|
|
|
|
|
|
|
|
/* driver .remove -- destructor */
|
2023-03-15 15:05:04 +00:00
|
|
|
static void snd_sh_dac_remove(struct platform_device *devptr)
|
2009-11-03 20:16:27 +00:00
|
|
|
{
|
|
|
|
snd_card_free(platform_get_drvdata(devptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free -- it has been defined by create */
|
|
|
|
static int snd_sh_dac_free(struct snd_sh_dac *chip)
|
|
|
|
{
|
|
|
|
/* release the data */
|
|
|
|
kfree(chip->data_buffer);
|
|
|
|
kfree(chip);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_sh_dac_dev_free(struct snd_device *device)
|
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip = device->device_data;
|
|
|
|
|
|
|
|
return snd_sh_dac_free(chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
|
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
|
|
|
|
hrtimer);
|
|
|
|
struct snd_pcm_runtime *runtime = chip->substream->runtime;
|
|
|
|
ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
|
|
|
|
|
|
|
|
if (!chip->empty) {
|
|
|
|
sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
|
|
|
|
chip->buffer_begin++;
|
|
|
|
|
|
|
|
chip->processed++;
|
|
|
|
if (chip->processed >= b_ps) {
|
|
|
|
chip->processed -= b_ps;
|
|
|
|
snd_pcm_period_elapsed(chip->substream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chip->buffer_begin == (chip->data_buffer +
|
|
|
|
chip->buffer_size - 1))
|
|
|
|
chip->buffer_begin = chip->data_buffer;
|
|
|
|
|
|
|
|
if (chip->buffer_begin == chip->buffer_end)
|
|
|
|
chip->empty = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chip->empty)
|
|
|
|
hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
|
|
|
|
HRTIMER_MODE_REL);
|
|
|
|
|
|
|
|
return HRTIMER_NORESTART;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create -- chip-specific constructor for the cards components */
|
2012-12-06 17:35:24 +00:00
|
|
|
static int snd_sh_dac_create(struct snd_card *card,
|
|
|
|
struct platform_device *devptr,
|
|
|
|
struct snd_sh_dac **rchip)
|
2009-11-03 20:16:27 +00:00
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip;
|
|
|
|
int err;
|
|
|
|
|
2020-01-03 08:16:33 +00:00
|
|
|
static const struct snd_device_ops ops = {
|
2009-11-03 20:16:27 +00:00
|
|
|
.dev_free = snd_sh_dac_dev_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
*rchip = NULL;
|
|
|
|
|
|
|
|
chip = kzalloc(sizeof(*chip), GFP_KERNEL);
|
|
|
|
if (chip == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
chip->card = card;
|
|
|
|
|
|
|
|
hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
|
|
|
chip->hrtimer.function = sh_dac_audio_timer;
|
|
|
|
|
|
|
|
dac_audio_reset(chip);
|
|
|
|
chip->rate = 8000;
|
|
|
|
dac_audio_set_rate(chip);
|
|
|
|
|
|
|
|
chip->pdata = devptr->dev.platform_data;
|
|
|
|
|
|
|
|
chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
|
|
|
|
if (chip->data_buffer == NULL) {
|
|
|
|
kfree(chip);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
|
|
|
|
if (err < 0) {
|
|
|
|
snd_sh_dac_free(chip);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rchip = chip;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* driver .probe -- constructor */
|
2012-12-06 17:35:24 +00:00
|
|
|
static int snd_sh_dac_probe(struct platform_device *devptr)
|
2009-11-03 20:16:27 +00:00
|
|
|
{
|
|
|
|
struct snd_sh_dac *chip;
|
|
|
|
struct snd_card *card;
|
|
|
|
int err;
|
|
|
|
|
2014-01-29 13:40:03 +00:00
|
|
|
err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
|
2009-11-03 20:16:27 +00:00
|
|
|
if (err < 0) {
|
|
|
|
snd_printk(KERN_ERR "cannot allocate the card\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_sh_dac_create(card, devptr, &chip);
|
|
|
|
if (err < 0)
|
|
|
|
goto probe_error;
|
|
|
|
|
|
|
|
err = snd_sh_dac_pcm(chip, 0);
|
|
|
|
if (err < 0)
|
|
|
|
goto probe_error;
|
|
|
|
|
|
|
|
strcpy(card->driver, "snd_sh_dac");
|
|
|
|
strcpy(card->shortname, "SuperH DAC audio driver");
|
|
|
|
printk(KERN_INFO "%s %s", card->longname, card->shortname);
|
|
|
|
|
|
|
|
err = snd_card_register(card);
|
|
|
|
if (err < 0)
|
|
|
|
goto probe_error;
|
|
|
|
|
2017-08-31 08:57:36 +00:00
|
|
|
snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
|
2009-11-03 20:16:27 +00:00
|
|
|
|
|
|
|
platform_set_drvdata(devptr, card);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
probe_error:
|
|
|
|
snd_card_free(card);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "driver" definition
|
|
|
|
*/
|
2012-05-21 09:21:54 +00:00
|
|
|
static struct platform_driver sh_dac_driver = {
|
2009-11-03 20:16:27 +00:00
|
|
|
.probe = snd_sh_dac_probe,
|
2023-03-15 15:05:04 +00:00
|
|
|
.remove_new = snd_sh_dac_remove,
|
2009-11-03 20:16:27 +00:00
|
|
|
.driver = {
|
|
|
|
.name = "dac_audio",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-05-21 09:21:54 +00:00
|
|
|
module_platform_driver(sh_dac_driver);
|