root/sound/sh/sh_dac_audio.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dac_audio_start_timer
  2. dac_audio_stop_timer
  3. dac_audio_reset
  4. dac_audio_set_rate
  5. snd_sh_dac_pcm_open
  6. snd_sh_dac_pcm_close
  7. snd_sh_dac_pcm_hw_params
  8. snd_sh_dac_pcm_hw_free
  9. snd_sh_dac_pcm_prepare
  10. snd_sh_dac_pcm_trigger
  11. snd_sh_dac_pcm_copy
  12. snd_sh_dac_pcm_copy_kernel
  13. snd_sh_dac_pcm_silence
  14. snd_sh_dac_pcm_pointer
  15. snd_sh_dac_pcm
  16. snd_sh_dac_remove
  17. snd_sh_dac_free
  18. snd_sh_dac_dev_free
  19. sh_dac_audio_timer
  20. snd_sh_dac_create
  21. snd_sh_dac_probe

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

/* [<][>][^][v][top][bottom][index][help] */