Blame view

sound/sh/sh_dac_audio.c 10.9 KB
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  /*
   * 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)
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   */
  
  #include <linux/hrtimer.h>
  #include <linux/interrupt.h>
  #include <linux/io.h>
  #include <linux/platform_device.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
29
  #include <linux/slab.h>
da155d5b4   Paul Gortmaker   sound: Add module...
30
  #include <linux/module.h>
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  #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_SUPPORTED_DEVICE("{{SuperH DAC audio support}}");
  
  /* 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)
  {
8b0e19531   Thomas Gleixner   ktime: Cleanup kt...
90
  	chip->wakeups_per_second = 1000000000 / chip->rate;
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
91
92
93
94
  }
  
  
  /* PCM INTERFACE */
cd196fe2d   Bhumika Goyal   ALSA: sh: make sn...
95
  static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  	.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_hw_params(struct snd_pcm_substream *substream,
  				struct snd_pcm_hw_params *hw_params)
  {
  	return snd_pcm_lib_malloc_pages(substream,
  			params_buffer_bytes(hw_params));
  }
  
  static int snd_sh_dac_pcm_hw_free(struct snd_pcm_substream *substream)
  {
  	return snd_pcm_lib_free_pages(substream);
  }
  
  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;
  }
1cc2f8ba0   Takashi Iwai   ALSA: sh: Convert...
185
186
187
  static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
  			       int channel, unsigned long pos,
  			       void __user *src, unsigned long count)
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
188
189
190
191
  {
  	/* channel is not used (interleaved data) */
  	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
192

1cc2f8ba0   Takashi Iwai   ALSA: sh: Convert...
193
194
195
  	if (copy_from_user_toio(chip->data_buffer + pos, src, count))
  		return -EFAULT;
  	chip->buffer_end = chip->data_buffer + pos + count;
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
196

1cc2f8ba0   Takashi Iwai   ALSA: sh: Convert...
197
198
199
200
201
202
203
  	if (chip->empty) {
  		chip->empty = 0;
  		dac_audio_start_timer(chip);
  	}
  
  	return 0;
  }
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
204

1cc2f8ba0   Takashi Iwai   ALSA: sh: Convert...
205
206
207
208
209
210
211
212
213
214
  static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
  				      int channel, unsigned long pos,
  				      void *src, unsigned long count)
  {
  	/* channel is not used (interleaved data) */
  	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  
  	memcpy_toio(chip->data_buffer + pos, src, count);
  	chip->buffer_end = chip->data_buffer + pos + count;
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
215
216
217
218
219
220
221
222
223
224
  
  	if (chip->empty) {
  		chip->empty = 0;
  		dac_audio_start_timer(chip);
  	}
  
  	return 0;
  }
  
  static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
1cc2f8ba0   Takashi Iwai   ALSA: sh: Convert...
225
226
  				  int channel, unsigned long pos,
  				  unsigned long count)
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
227
228
229
230
  {
  	/* channel is not used (interleaved data) */
  	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
231

1cc2f8ba0   Takashi Iwai   ALSA: sh: Convert...
232
233
  	memset_io(chip->data_buffer + pos, 0, count);
  	chip->buffer_end = chip->data_buffer + pos + count;
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  
  	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 */
46a085a3e   Arvind Yadav   ALSA: sh: constif...
253
  static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
254
255
256
257
258
259
260
261
  	.open		= snd_sh_dac_pcm_open,
  	.close		= snd_sh_dac_pcm_close,
  	.ioctl		= snd_pcm_lib_ioctl,
  	.hw_params	= snd_sh_dac_pcm_hw_params,
  	.hw_free	= snd_sh_dac_pcm_hw_free,
  	.prepare	= snd_sh_dac_pcm_prepare,
  	.trigger	= snd_sh_dac_pcm_trigger,
  	.pointer	= snd_sh_dac_pcm_pointer,
1cc2f8ba0   Takashi Iwai   ALSA: sh: Convert...
262
263
264
  	.copy_user	= snd_sh_dac_pcm_copy,
  	.copy_kernel	= snd_sh_dac_pcm_copy_kernel,
  	.fill_silence	= snd_sh_dac_pcm_silence,
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
265
266
  	.mmap		= snd_pcm_lib_mmap_iomem,
  };
e74033a85   Bill Pemberton   ALSA: sh: remove ...
267
  static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  {
  	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 */
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  					  snd_dma_continuous_data(GFP_KERNEL),
  							48 * 1024,
  							48 * 1024);
  
  	return 0;
  }
  /* END OF PCM INTERFACE */
  
  
  /* driver .remove  --  destructor */
  static int snd_sh_dac_remove(struct platform_device *devptr)
  {
  	snd_card_free(platform_get_drvdata(devptr));
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  	return 0;
  }
  
  /* 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 */
e74033a85   Bill Pemberton   ALSA: sh: remove ...
350
351
352
  static int snd_sh_dac_create(struct snd_card *card,
  			     struct platform_device *devptr,
  			     struct snd_sh_dac **rchip)
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  {
  	struct snd_sh_dac *chip;
  	int err;
  
  	static struct snd_device_ops ops = {
  		   .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 */
e74033a85   Bill Pemberton   ALSA: sh: remove ...
396
  static int snd_sh_dac_probe(struct platform_device *devptr)
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
397
398
399
400
  {
  	struct snd_sh_dac *chip;
  	struct snd_card *card;
  	int err;
e7182ac5a   Takashi Iwai   ALSA: sh: Convert...
401
  	err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  	if (err < 0) {
  			snd_printk(KERN_ERR "cannot allocate the card
  ");
  			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;
c054c8a0c   Takashi Iwai   ALSA: sh: Put mis...
423
  	snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
424
425
426
427
428
429
430
431
432
433
434
435
  
  	platform_set_drvdata(devptr, card);
  	return 0;
  
  probe_error:
  	snd_card_free(card);
  	return err;
  }
  
  /*
   * "driver" definition
   */
d4c698385   Paul Mundt   ALSA: sh: Fix up ...
436
  static struct platform_driver sh_dac_driver = {
9dcaa7b25   Rafael Ignacio Zurita   ALSA: sh: add Sup...
437
438
439
440
441
442
  	.probe	= snd_sh_dac_probe,
  	.remove = snd_sh_dac_remove,
  	.driver = {
  		.name = "dac_audio",
  	},
  };
d4c698385   Paul Mundt   ALSA: sh: Fix up ...
443
  module_platform_driver(sh_dac_driver);