Linux Audio

Check our new training course

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