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