blob: feb28502940f1add6f96568fb860acba1257bd48 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -03002/*
3 * sh_dac_audio.c - SuperH DAC audio driver for ALSA
4 *
5 * Copyright (c) 2009 by Rafael Ignacio Zurita <rizurita@yahoo.com>
6 *
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -03007 * Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh)
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -03008 */
9
10#include <linux/hrtimer.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040015#include <linux/module.h>
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -030016#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");
28MODULE_SUPPORTED_DEVICE("{{SuperH DAC audio support}}");
29
30/* Module Parameters */
31static int index = SNDRV_DEFAULT_IDX1;
32static char *id = SNDRV_DEFAULT_STR1;
33module_param(index, int, 0444);
34MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
35module_param(id, charp, 0444);
36MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
37
38/* main struct */
39struct 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
54static 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
60static void dac_audio_stop_timer(struct snd_sh_dac *chip)
61{
62 hrtimer_cancel(&chip->hrtimer);
63}
64
65static 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
73static void dac_audio_set_rate(struct snd_sh_dac *chip)
74{
Thomas Gleixner8b0e1952016-12-25 12:30:41 +010075 chip->wakeups_per_second = 1000000000 / chip->rate;
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -030076}
77
78
79/* PCM INTERFACE */
80
Bhumika Goyalcd196fe2017-08-17 14:45:57 +053081static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -030082 .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
99static 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
116static 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
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300128static 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
139static 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
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200160static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
161 int channel, unsigned long pos,
162 void __user *src, unsigned long count)
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300163{
164 /* channel is not used (interleaved data) */
165 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300166
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200167 if (copy_from_user_toio(chip->data_buffer + pos, src, count))
168 return -EFAULT;
169 chip->buffer_end = chip->data_buffer + pos + count;
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300170
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200171 if (chip->empty) {
172 chip->empty = 0;
173 dac_audio_start_timer(chip);
174 }
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300175
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200176 return 0;
177}
178
179static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
180 int channel, unsigned long pos,
181 void *src, unsigned long count)
182{
183 /* channel is not used (interleaved data) */
184 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200185
186 memcpy_toio(chip->data_buffer + pos, src, count);
187 chip->buffer_end = chip->data_buffer + pos + count;
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300188
189 if (chip->empty) {
190 chip->empty = 0;
191 dac_audio_start_timer(chip);
192 }
193
194 return 0;
195}
196
197static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200198 int channel, unsigned long pos,
199 unsigned long count)
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300200{
201 /* channel is not used (interleaved data) */
202 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300203
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200204 memset_io(chip->data_buffer + pos, 0, count);
205 chip->buffer_end = chip->data_buffer + pos + count;
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300206
207 if (chip->empty) {
208 chip->empty = 0;
209 dac_audio_start_timer(chip);
210 }
211
212 return 0;
213}
214
215static
216snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
217{
218 struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
219 int pointer = chip->buffer_begin - chip->data_buffer;
220
221 return pointer;
222}
223
224/* pcm ops */
Arvind Yadav46a085a2017-08-18 13:15:18 +0530225static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300226 .open = snd_sh_dac_pcm_open,
227 .close = snd_sh_dac_pcm_close,
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300228 .prepare = snd_sh_dac_pcm_prepare,
229 .trigger = snd_sh_dac_pcm_trigger,
230 .pointer = snd_sh_dac_pcm_pointer,
Takashi Iwai1cc2f8b2017-05-10 21:57:16 +0200231 .copy_user = snd_sh_dac_pcm_copy,
232 .copy_kernel = snd_sh_dac_pcm_copy_kernel,
233 .fill_silence = snd_sh_dac_pcm_silence,
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300234 .mmap = snd_pcm_lib_mmap_iomem,
235};
236
Bill Pembertone74033a2012-12-06 12:35:24 -0500237static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300238{
239 int err;
240 struct snd_pcm *pcm;
241
242 /* device should be always 0 for us */
243 err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
244 if (err < 0)
245 return err;
246
247 pcm->private_data = chip;
248 strcpy(pcm->name, "SH_DAC PCM");
249 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
250
251 /* buffer size=48K */
Takashi Iwai50cdf3a2019-12-09 10:49:33 +0100252 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
253 NULL, 48 * 1024, 48 * 1024);
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300254
255 return 0;
256}
257/* END OF PCM INTERFACE */
258
259
260/* driver .remove -- destructor */
261static int snd_sh_dac_remove(struct platform_device *devptr)
262{
263 snd_card_free(platform_get_drvdata(devptr));
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300264 return 0;
265}
266
267/* free -- it has been defined by create */
268static int snd_sh_dac_free(struct snd_sh_dac *chip)
269{
270 /* release the data */
271 kfree(chip->data_buffer);
272 kfree(chip);
273
274 return 0;
275}
276
277static int snd_sh_dac_dev_free(struct snd_device *device)
278{
279 struct snd_sh_dac *chip = device->device_data;
280
281 return snd_sh_dac_free(chip);
282}
283
284static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
285{
286 struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
287 hrtimer);
288 struct snd_pcm_runtime *runtime = chip->substream->runtime;
289 ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
290
291 if (!chip->empty) {
292 sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
293 chip->buffer_begin++;
294
295 chip->processed++;
296 if (chip->processed >= b_ps) {
297 chip->processed -= b_ps;
298 snd_pcm_period_elapsed(chip->substream);
299 }
300
301 if (chip->buffer_begin == (chip->data_buffer +
302 chip->buffer_size - 1))
303 chip->buffer_begin = chip->data_buffer;
304
305 if (chip->buffer_begin == chip->buffer_end)
306 chip->empty = 1;
307
308 }
309
310 if (!chip->empty)
311 hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
312 HRTIMER_MODE_REL);
313
314 return HRTIMER_NORESTART;
315}
316
317/* create -- chip-specific constructor for the cards components */
Bill Pembertone74033a2012-12-06 12:35:24 -0500318static int snd_sh_dac_create(struct snd_card *card,
319 struct platform_device *devptr,
320 struct snd_sh_dac **rchip)
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300321{
322 struct snd_sh_dac *chip;
323 int err;
324
Takashi Iwai52b136d2020-01-03 09:16:33 +0100325 static const struct snd_device_ops ops = {
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300326 .dev_free = snd_sh_dac_dev_free,
327 };
328
329 *rchip = NULL;
330
331 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
332 if (chip == NULL)
333 return -ENOMEM;
334
335 chip->card = card;
336
337 hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
338 chip->hrtimer.function = sh_dac_audio_timer;
339
340 dac_audio_reset(chip);
341 chip->rate = 8000;
342 dac_audio_set_rate(chip);
343
344 chip->pdata = devptr->dev.platform_data;
345
346 chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
347 if (chip->data_buffer == NULL) {
348 kfree(chip);
349 return -ENOMEM;
350 }
351
352 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
353 if (err < 0) {
354 snd_sh_dac_free(chip);
355 return err;
356 }
357
358 *rchip = chip;
359
360 return 0;
361}
362
363/* driver .probe -- constructor */
Bill Pembertone74033a2012-12-06 12:35:24 -0500364static int snd_sh_dac_probe(struct platform_device *devptr)
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300365{
366 struct snd_sh_dac *chip;
367 struct snd_card *card;
368 int err;
369
Takashi Iwaie7182ac2014-01-29 14:40:03 +0100370 err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300371 if (err < 0) {
372 snd_printk(KERN_ERR "cannot allocate the card\n");
373 return err;
374 }
375
376 err = snd_sh_dac_create(card, devptr, &chip);
377 if (err < 0)
378 goto probe_error;
379
380 err = snd_sh_dac_pcm(chip, 0);
381 if (err < 0)
382 goto probe_error;
383
384 strcpy(card->driver, "snd_sh_dac");
385 strcpy(card->shortname, "SuperH DAC audio driver");
386 printk(KERN_INFO "%s %s", card->longname, card->shortname);
387
388 err = snd_card_register(card);
389 if (err < 0)
390 goto probe_error;
391
Takashi Iwaic054c8a2017-08-31 10:57:36 +0200392 snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300393
394 platform_set_drvdata(devptr, card);
395 return 0;
396
397probe_error:
398 snd_card_free(card);
399 return err;
400}
401
402/*
403 * "driver" definition
404 */
Paul Mundtd4c69832012-05-21 18:21:54 +0900405static struct platform_driver sh_dac_driver = {
Rafael Ignacio Zurita9dcaa7b2009-11-03 17:16:27 -0300406 .probe = snd_sh_dac_probe,
407 .remove = snd_sh_dac_remove,
408 .driver = {
409 .name = "dac_audio",
410 },
411};
412
Paul Mundtd4c69832012-05-21 18:21:54 +0900413module_platform_driver(sh_dac_driver);