Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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