xref: /linux/sound/sh/sh_dac_audio.c (revision 9a4df8251ac0aed09d67405c7dfa59f14bde82ed)
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_prepare(struct snd_pcm_substream *substream)
129 {
130 	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
131 	struct snd_pcm_runtime *runtime = chip->substream->runtime;
132 
133 	chip->buffer_size = runtime->buffer_size;
134 	memset(chip->data_buffer, 0, chip->pdata->buffer_size);
135 
136 	return 0;
137 }
138 
139 static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
140 {
141 	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
142 
143 	switch (cmd) {
144 	case SNDRV_PCM_TRIGGER_START:
145 		dac_audio_start_timer(chip);
146 		break;
147 	case SNDRV_PCM_TRIGGER_STOP:
148 		chip->buffer_begin = chip->buffer_end = chip->data_buffer;
149 		chip->processed = 0;
150 		chip->empty = 1;
151 		dac_audio_stop_timer(chip);
152 		break;
153 	default:
154 		 return -EINVAL;
155 	}
156 
157 	return 0;
158 }
159 
160 static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
161 			       int channel, unsigned long pos,
162 			       void __user *src, unsigned long count)
163 {
164 	/* channel is not used (interleaved data) */
165 	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
166 	struct snd_pcm_runtime *runtime = substream->runtime;
167 
168 	if (copy_from_user_toio(chip->data_buffer + pos, src, count))
169 		return -EFAULT;
170 	chip->buffer_end = chip->data_buffer + pos + count;
171 
172 	if (chip->empty) {
173 		chip->empty = 0;
174 		dac_audio_start_timer(chip);
175 	}
176 
177 	return 0;
178 }
179 
180 static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
181 				      int channel, unsigned long pos,
182 				      void *src, unsigned long count)
183 {
184 	/* channel is not used (interleaved data) */
185 	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
186 	struct snd_pcm_runtime *runtime = substream->runtime;
187 
188 	memcpy_toio(chip->data_buffer + pos, src, count);
189 	chip->buffer_end = chip->data_buffer + pos + count;
190 
191 	if (chip->empty) {
192 		chip->empty = 0;
193 		dac_audio_start_timer(chip);
194 	}
195 
196 	return 0;
197 }
198 
199 static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
200 				  int channel, unsigned long pos,
201 				  unsigned long count)
202 {
203 	/* channel is not used (interleaved data) */
204 	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
205 	struct snd_pcm_runtime *runtime = substream->runtime;
206 
207 	memset_io(chip->data_buffer + pos, 0, count);
208 	chip->buffer_end = chip->data_buffer + pos + count;
209 
210 	if (chip->empty) {
211 		chip->empty = 0;
212 		dac_audio_start_timer(chip);
213 	}
214 
215 	return 0;
216 }
217 
218 static
219 snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
220 {
221 	struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
222 	int pointer = chip->buffer_begin - chip->data_buffer;
223 
224 	return pointer;
225 }
226 
227 /* pcm ops */
228 static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
229 	.open		= snd_sh_dac_pcm_open,
230 	.close		= snd_sh_dac_pcm_close,
231 	.ioctl		= snd_pcm_lib_ioctl,
232 	.prepare	= snd_sh_dac_pcm_prepare,
233 	.trigger	= snd_sh_dac_pcm_trigger,
234 	.pointer	= snd_sh_dac_pcm_pointer,
235 	.copy_user	= snd_sh_dac_pcm_copy,
236 	.copy_kernel	= snd_sh_dac_pcm_copy_kernel,
237 	.fill_silence	= snd_sh_dac_pcm_silence,
238 	.mmap		= snd_pcm_lib_mmap_iomem,
239 };
240 
241 static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
242 {
243 	int err;
244 	struct snd_pcm *pcm;
245 
246 	/* device should be always 0 for us */
247 	err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
248 	if (err < 0)
249 		return err;
250 
251 	pcm->private_data = chip;
252 	strcpy(pcm->name, "SH_DAC PCM");
253 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
254 
255 	/* buffer size=48K */
256 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
257 				       NULL, 48 * 1024, 48 * 1024);
258 
259 	return 0;
260 }
261 /* END OF PCM INTERFACE */
262 
263 
264 /* driver .remove  --  destructor */
265 static int snd_sh_dac_remove(struct platform_device *devptr)
266 {
267 	snd_card_free(platform_get_drvdata(devptr));
268 	return 0;
269 }
270 
271 /* free -- it has been defined by create */
272 static int snd_sh_dac_free(struct snd_sh_dac *chip)
273 {
274 	/* release the data */
275 	kfree(chip->data_buffer);
276 	kfree(chip);
277 
278 	return 0;
279 }
280 
281 static int snd_sh_dac_dev_free(struct snd_device *device)
282 {
283 	struct snd_sh_dac *chip = device->device_data;
284 
285 	return snd_sh_dac_free(chip);
286 }
287 
288 static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
289 {
290 	struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
291 					       hrtimer);
292 	struct snd_pcm_runtime *runtime = chip->substream->runtime;
293 	ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
294 
295 	if (!chip->empty) {
296 		sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
297 		chip->buffer_begin++;
298 
299 		chip->processed++;
300 		if (chip->processed >= b_ps) {
301 			chip->processed -= b_ps;
302 			snd_pcm_period_elapsed(chip->substream);
303 		}
304 
305 		if (chip->buffer_begin == (chip->data_buffer +
306 					   chip->buffer_size - 1))
307 			chip->buffer_begin = chip->data_buffer;
308 
309 		if (chip->buffer_begin == chip->buffer_end)
310 			chip->empty = 1;
311 
312 	}
313 
314 	if (!chip->empty)
315 		hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
316 			      HRTIMER_MODE_REL);
317 
318 	return HRTIMER_NORESTART;
319 }
320 
321 /* create  --  chip-specific constructor for the cards components */
322 static int snd_sh_dac_create(struct snd_card *card,
323 			     struct platform_device *devptr,
324 			     struct snd_sh_dac **rchip)
325 {
326 	struct snd_sh_dac *chip;
327 	int err;
328 
329 	static struct snd_device_ops ops = {
330 		   .dev_free = snd_sh_dac_dev_free,
331 	};
332 
333 	*rchip = NULL;
334 
335 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
336 	if (chip == NULL)
337 		return -ENOMEM;
338 
339 	chip->card = card;
340 
341 	hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
342 	chip->hrtimer.function = sh_dac_audio_timer;
343 
344 	dac_audio_reset(chip);
345 	chip->rate = 8000;
346 	dac_audio_set_rate(chip);
347 
348 	chip->pdata = devptr->dev.platform_data;
349 
350 	chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
351 	if (chip->data_buffer == NULL) {
352 		kfree(chip);
353 		return -ENOMEM;
354 	}
355 
356 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
357 	if (err < 0) {
358 		snd_sh_dac_free(chip);
359 		return err;
360 	}
361 
362 	*rchip = chip;
363 
364 	return 0;
365 }
366 
367 /* driver .probe  --  constructor */
368 static int snd_sh_dac_probe(struct platform_device *devptr)
369 {
370 	struct snd_sh_dac *chip;
371 	struct snd_card *card;
372 	int err;
373 
374 	err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
375 	if (err < 0) {
376 			snd_printk(KERN_ERR "cannot allocate the card\n");
377 			return err;
378 	}
379 
380 	err = snd_sh_dac_create(card, devptr, &chip);
381 	if (err < 0)
382 		goto probe_error;
383 
384 	err = snd_sh_dac_pcm(chip, 0);
385 	if (err < 0)
386 		goto probe_error;
387 
388 	strcpy(card->driver, "snd_sh_dac");
389 	strcpy(card->shortname, "SuperH DAC audio driver");
390 	printk(KERN_INFO "%s %s", card->longname, card->shortname);
391 
392 	err = snd_card_register(card);
393 	if (err < 0)
394 		goto probe_error;
395 
396 	snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
397 
398 	platform_set_drvdata(devptr, card);
399 	return 0;
400 
401 probe_error:
402 	snd_card_free(card);
403 	return err;
404 }
405 
406 /*
407  * "driver" definition
408  */
409 static struct platform_driver sh_dac_driver = {
410 	.probe	= snd_sh_dac_probe,
411 	.remove = snd_sh_dac_remove,
412 	.driver = {
413 		.name = "dac_audio",
414 	},
415 };
416 
417 module_platform_driver(sh_dac_driver);
418