0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/hrtimer.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/io.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/slab.h>
0015 #include <linux/module.h>
0016 #include <sound/core.h>
0017 #include <sound/initval.h>
0018 #include <sound/pcm.h>
0019 #include <sound/sh_dac_audio.h>
0020 #include <asm/clock.h>
0021 #include <asm/hd64461.h>
0022 #include <mach/hp6xx.h>
0023 #include <cpu/dac.h>
0024
0025 MODULE_AUTHOR("Rafael Ignacio Zurita <rizurita@yahoo.com>");
0026 MODULE_DESCRIPTION("SuperH DAC audio driver");
0027 MODULE_LICENSE("GPL");
0028
0029
0030 static int index = SNDRV_DEFAULT_IDX1;
0031 static char *id = SNDRV_DEFAULT_STR1;
0032 module_param(index, int, 0444);
0033 MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
0034 module_param(id, charp, 0444);
0035 MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
0036
0037
0038 struct snd_sh_dac {
0039 struct snd_card *card;
0040 struct snd_pcm_substream *substream;
0041 struct hrtimer hrtimer;
0042 ktime_t wakeups_per_second;
0043
0044 int rate;
0045 int empty;
0046 char *data_buffer, *buffer_begin, *buffer_end;
0047 int processed;
0048 int buffer_size;
0049 struct dac_audio_pdata *pdata;
0050 };
0051
0052
0053 static void dac_audio_start_timer(struct snd_sh_dac *chip)
0054 {
0055 hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
0056 HRTIMER_MODE_REL);
0057 }
0058
0059 static void dac_audio_stop_timer(struct snd_sh_dac *chip)
0060 {
0061 hrtimer_cancel(&chip->hrtimer);
0062 }
0063
0064 static void dac_audio_reset(struct snd_sh_dac *chip)
0065 {
0066 dac_audio_stop_timer(chip);
0067 chip->buffer_begin = chip->buffer_end = chip->data_buffer;
0068 chip->processed = 0;
0069 chip->empty = 1;
0070 }
0071
0072 static void dac_audio_set_rate(struct snd_sh_dac *chip)
0073 {
0074 chip->wakeups_per_second = 1000000000 / chip->rate;
0075 }
0076
0077
0078
0079
0080 static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
0081 .info = (SNDRV_PCM_INFO_MMAP |
0082 SNDRV_PCM_INFO_MMAP_VALID |
0083 SNDRV_PCM_INFO_INTERLEAVED |
0084 SNDRV_PCM_INFO_HALF_DUPLEX),
0085 .formats = SNDRV_PCM_FMTBIT_U8,
0086 .rates = SNDRV_PCM_RATE_8000,
0087 .rate_min = 8000,
0088 .rate_max = 8000,
0089 .channels_min = 1,
0090 .channels_max = 1,
0091 .buffer_bytes_max = (48*1024),
0092 .period_bytes_min = 1,
0093 .period_bytes_max = (48*1024),
0094 .periods_min = 1,
0095 .periods_max = 1024,
0096 };
0097
0098 static int snd_sh_dac_pcm_open(struct snd_pcm_substream *substream)
0099 {
0100 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0101 struct snd_pcm_runtime *runtime = substream->runtime;
0102
0103 runtime->hw = snd_sh_dac_pcm_hw;
0104
0105 chip->substream = substream;
0106 chip->buffer_begin = chip->buffer_end = chip->data_buffer;
0107 chip->processed = 0;
0108 chip->empty = 1;
0109
0110 chip->pdata->start(chip->pdata);
0111
0112 return 0;
0113 }
0114
0115 static int snd_sh_dac_pcm_close(struct snd_pcm_substream *substream)
0116 {
0117 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0118
0119 chip->substream = NULL;
0120
0121 dac_audio_stop_timer(chip);
0122 chip->pdata->stop(chip->pdata);
0123
0124 return 0;
0125 }
0126
0127 static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream *substream)
0128 {
0129 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0130 struct snd_pcm_runtime *runtime = chip->substream->runtime;
0131
0132 chip->buffer_size = runtime->buffer_size;
0133 memset(chip->data_buffer, 0, chip->pdata->buffer_size);
0134
0135 return 0;
0136 }
0137
0138 static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0139 {
0140 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0141
0142 switch (cmd) {
0143 case SNDRV_PCM_TRIGGER_START:
0144 dac_audio_start_timer(chip);
0145 break;
0146 case SNDRV_PCM_TRIGGER_STOP:
0147 chip->buffer_begin = chip->buffer_end = chip->data_buffer;
0148 chip->processed = 0;
0149 chip->empty = 1;
0150 dac_audio_stop_timer(chip);
0151 break;
0152 default:
0153 return -EINVAL;
0154 }
0155
0156 return 0;
0157 }
0158
0159 static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
0160 int channel, unsigned long pos,
0161 void __user *src, unsigned long count)
0162 {
0163
0164 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0165
0166 if (copy_from_user_toio(chip->data_buffer + pos, src, count))
0167 return -EFAULT;
0168 chip->buffer_end = chip->data_buffer + pos + count;
0169
0170 if (chip->empty) {
0171 chip->empty = 0;
0172 dac_audio_start_timer(chip);
0173 }
0174
0175 return 0;
0176 }
0177
0178 static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
0179 int channel, unsigned long pos,
0180 void *src, unsigned long count)
0181 {
0182
0183 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0184
0185 memcpy_toio(chip->data_buffer + pos, src, count);
0186 chip->buffer_end = chip->data_buffer + pos + count;
0187
0188 if (chip->empty) {
0189 chip->empty = 0;
0190 dac_audio_start_timer(chip);
0191 }
0192
0193 return 0;
0194 }
0195
0196 static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
0197 int channel, unsigned long pos,
0198 unsigned long count)
0199 {
0200
0201 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0202
0203 memset_io(chip->data_buffer + pos, 0, count);
0204 chip->buffer_end = chip->data_buffer + pos + count;
0205
0206 if (chip->empty) {
0207 chip->empty = 0;
0208 dac_audio_start_timer(chip);
0209 }
0210
0211 return 0;
0212 }
0213
0214 static
0215 snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
0216 {
0217 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
0218 int pointer = chip->buffer_begin - chip->data_buffer;
0219
0220 return pointer;
0221 }
0222
0223
0224 static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
0225 .open = snd_sh_dac_pcm_open,
0226 .close = snd_sh_dac_pcm_close,
0227 .prepare = snd_sh_dac_pcm_prepare,
0228 .trigger = snd_sh_dac_pcm_trigger,
0229 .pointer = snd_sh_dac_pcm_pointer,
0230 .copy_user = snd_sh_dac_pcm_copy,
0231 .copy_kernel = snd_sh_dac_pcm_copy_kernel,
0232 .fill_silence = snd_sh_dac_pcm_silence,
0233 .mmap = snd_pcm_lib_mmap_iomem,
0234 };
0235
0236 static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
0237 {
0238 int err;
0239 struct snd_pcm *pcm;
0240
0241
0242 err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
0243 if (err < 0)
0244 return err;
0245
0246 pcm->private_data = chip;
0247 strcpy(pcm->name, "SH_DAC PCM");
0248 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
0249
0250
0251 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
0252 NULL, 48 * 1024, 48 * 1024);
0253
0254 return 0;
0255 }
0256
0257
0258
0259
0260 static int snd_sh_dac_remove(struct platform_device *devptr)
0261 {
0262 snd_card_free(platform_get_drvdata(devptr));
0263 return 0;
0264 }
0265
0266
0267 static int snd_sh_dac_free(struct snd_sh_dac *chip)
0268 {
0269
0270 kfree(chip->data_buffer);
0271 kfree(chip);
0272
0273 return 0;
0274 }
0275
0276 static int snd_sh_dac_dev_free(struct snd_device *device)
0277 {
0278 struct snd_sh_dac *chip = device->device_data;
0279
0280 return snd_sh_dac_free(chip);
0281 }
0282
0283 static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
0284 {
0285 struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
0286 hrtimer);
0287 struct snd_pcm_runtime *runtime = chip->substream->runtime;
0288 ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
0289
0290 if (!chip->empty) {
0291 sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
0292 chip->buffer_begin++;
0293
0294 chip->processed++;
0295 if (chip->processed >= b_ps) {
0296 chip->processed -= b_ps;
0297 snd_pcm_period_elapsed(chip->substream);
0298 }
0299
0300 if (chip->buffer_begin == (chip->data_buffer +
0301 chip->buffer_size - 1))
0302 chip->buffer_begin = chip->data_buffer;
0303
0304 if (chip->buffer_begin == chip->buffer_end)
0305 chip->empty = 1;
0306
0307 }
0308
0309 if (!chip->empty)
0310 hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
0311 HRTIMER_MODE_REL);
0312
0313 return HRTIMER_NORESTART;
0314 }
0315
0316
0317 static int snd_sh_dac_create(struct snd_card *card,
0318 struct platform_device *devptr,
0319 struct snd_sh_dac **rchip)
0320 {
0321 struct snd_sh_dac *chip;
0322 int err;
0323
0324 static const struct snd_device_ops ops = {
0325 .dev_free = snd_sh_dac_dev_free,
0326 };
0327
0328 *rchip = NULL;
0329
0330 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
0331 if (chip == NULL)
0332 return -ENOMEM;
0333
0334 chip->card = card;
0335
0336 hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
0337 chip->hrtimer.function = sh_dac_audio_timer;
0338
0339 dac_audio_reset(chip);
0340 chip->rate = 8000;
0341 dac_audio_set_rate(chip);
0342
0343 chip->pdata = devptr->dev.platform_data;
0344
0345 chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
0346 if (chip->data_buffer == NULL) {
0347 kfree(chip);
0348 return -ENOMEM;
0349 }
0350
0351 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
0352 if (err < 0) {
0353 snd_sh_dac_free(chip);
0354 return err;
0355 }
0356
0357 *rchip = chip;
0358
0359 return 0;
0360 }
0361
0362
0363 static int snd_sh_dac_probe(struct platform_device *devptr)
0364 {
0365 struct snd_sh_dac *chip;
0366 struct snd_card *card;
0367 int err;
0368
0369 err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
0370 if (err < 0) {
0371 snd_printk(KERN_ERR "cannot allocate the card\n");
0372 return err;
0373 }
0374
0375 err = snd_sh_dac_create(card, devptr, &chip);
0376 if (err < 0)
0377 goto probe_error;
0378
0379 err = snd_sh_dac_pcm(chip, 0);
0380 if (err < 0)
0381 goto probe_error;
0382
0383 strcpy(card->driver, "snd_sh_dac");
0384 strcpy(card->shortname, "SuperH DAC audio driver");
0385 printk(KERN_INFO "%s %s", card->longname, card->shortname);
0386
0387 err = snd_card_register(card);
0388 if (err < 0)
0389 goto probe_error;
0390
0391 snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
0392
0393 platform_set_drvdata(devptr, card);
0394 return 0;
0395
0396 probe_error:
0397 snd_card_free(card);
0398 return err;
0399 }
0400
0401
0402
0403
0404 static struct platform_driver sh_dac_driver = {
0405 .probe = snd_sh_dac_probe,
0406 .remove = snd_sh_dac_remove,
0407 .driver = {
0408 .name = "dac_audio",
0409 },
0410 };
0411
0412 module_platform_driver(sh_dac_driver);