Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
9dcaa7b2 RIZ |
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 | * | |
9dcaa7b2 | 7 | * Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh) |
9dcaa7b2 RIZ |
8 | */ |
9 | ||
10 | #include <linux/hrtimer.h> | |
11 | #include <linux/interrupt.h> | |
12 | #include <linux/io.h> | |
13 | #include <linux/platform_device.h> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
da155d5b | 15 | #include <linux/module.h> |
9dcaa7b2 RIZ |
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"); | |
9dcaa7b2 RIZ |
28 | |
29 | /* Module Parameters */ | |
30 | static int index = SNDRV_DEFAULT_IDX1; | |
31 | static char *id = SNDRV_DEFAULT_STR1; | |
32 | module_param(index, int, 0444); | |
33 | MODULE_PARM_DESC(index, "Index value for SuperH DAC audio."); | |
34 | module_param(id, charp, 0444); | |
35 | MODULE_PARM_DESC(id, "ID string for SuperH DAC audio."); | |
36 | ||
37 | /* main struct */ | |
38 | struct 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 | ||
53 | static 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 | ||
59 | static void dac_audio_stop_timer(struct snd_sh_dac *chip) | |
60 | { | |
61 | hrtimer_cancel(&chip->hrtimer); | |
62 | } | |
63 | ||
64 | static 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 | ||
72 | static void dac_audio_set_rate(struct snd_sh_dac *chip) | |
73 | { | |
8b0e1953 | 74 | chip->wakeups_per_second = 1000000000 / chip->rate; |
9dcaa7b2 RIZ |
75 | } |
76 | ||
77 | ||
78 | /* PCM INTERFACE */ | |
79 | ||
cd196fe2 | 80 | static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = { |
9dcaa7b2 RIZ |
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 | ||
98 | static 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 | ||
115 | static 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 | ||
9dcaa7b2 RIZ |
127 | static 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 | ||
138 | static 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 | ||
1cc2f8ba TI |
159 | static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream, |
160 | int channel, unsigned long pos, | |
161 | void __user *src, unsigned long count) | |
9dcaa7b2 RIZ |
162 | { |
163 | /* channel is not used (interleaved data) */ | |
164 | struct snd_sh_dac *chip = snd_pcm_substream_chip(substream); | |
9dcaa7b2 | 165 | |
1cc2f8ba TI |
166 | if (copy_from_user_toio(chip->data_buffer + pos, src, count)) |
167 | return -EFAULT; | |
168 | chip->buffer_end = chip->data_buffer + pos + count; | |
9dcaa7b2 | 169 | |
1cc2f8ba TI |
170 | if (chip->empty) { |
171 | chip->empty = 0; | |
172 | dac_audio_start_timer(chip); | |
173 | } | |
174 | ||
175 | return 0; | |
176 | } | |
9dcaa7b2 | 177 | |
1cc2f8ba TI |
178 | static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream, |
179 | int channel, unsigned long pos, | |
180 | void *src, unsigned long count) | |
181 | { | |
182 | /* channel is not used (interleaved data) */ | |
183 | struct snd_sh_dac *chip = snd_pcm_substream_chip(substream); | |
1cc2f8ba TI |
184 | |
185 | memcpy_toio(chip->data_buffer + pos, src, count); | |
186 | chip->buffer_end = chip->data_buffer + pos + count; | |
9dcaa7b2 RIZ |
187 | |
188 | if (chip->empty) { | |
189 | chip->empty = 0; | |
190 | dac_audio_start_timer(chip); | |
191 | } | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream, | |
1cc2f8ba TI |
197 | int channel, unsigned long pos, |
198 | unsigned long count) | |
9dcaa7b2 RIZ |
199 | { |
200 | /* channel is not used (interleaved data) */ | |
201 | struct snd_sh_dac *chip = snd_pcm_substream_chip(substream); | |
9dcaa7b2 | 202 | |
1cc2f8ba TI |
203 | memset_io(chip->data_buffer + pos, 0, count); |
204 | chip->buffer_end = chip->data_buffer + pos + count; | |
9dcaa7b2 RIZ |
205 | |
206 | if (chip->empty) { | |
207 | chip->empty = 0; | |
208 | dac_audio_start_timer(chip); | |
209 | } | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
214 | static | |
215 | snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream) | |
216 | { | |
217 | struct snd_sh_dac *chip = snd_pcm_substream_chip(substream); | |
218 | int pointer = chip->buffer_begin - chip->data_buffer; | |
219 | ||
220 | return pointer; | |
221 | } | |
222 | ||
223 | /* pcm ops */ | |
46a085a3 | 224 | static const struct snd_pcm_ops snd_sh_dac_pcm_ops = { |
9dcaa7b2 RIZ |
225 | .open = snd_sh_dac_pcm_open, |
226 | .close = snd_sh_dac_pcm_close, | |
9dcaa7b2 RIZ |
227 | .prepare = snd_sh_dac_pcm_prepare, |
228 | .trigger = snd_sh_dac_pcm_trigger, | |
229 | .pointer = snd_sh_dac_pcm_pointer, | |
1cc2f8ba TI |
230 | .copy_user = snd_sh_dac_pcm_copy, |
231 | .copy_kernel = snd_sh_dac_pcm_copy_kernel, | |
232 | .fill_silence = snd_sh_dac_pcm_silence, | |
9dcaa7b2 RIZ |
233 | .mmap = snd_pcm_lib_mmap_iomem, |
234 | }; | |
235 | ||
e74033a8 | 236 | static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device) |
9dcaa7b2 RIZ |
237 | { |
238 | int err; | |
239 | struct snd_pcm *pcm; | |
240 | ||
241 | /* device should be always 0 for us */ | |
242 | err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm); | |
243 | if (err < 0) | |
244 | return err; | |
245 | ||
246 | pcm->private_data = chip; | |
247 | strcpy(pcm->name, "SH_DAC PCM"); | |
248 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops); | |
249 | ||
250 | /* buffer size=48K */ | |
50cdf3a0 TI |
251 | snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
252 | NULL, 48 * 1024, 48 * 1024); | |
9dcaa7b2 RIZ |
253 | |
254 | return 0; | |
255 | } | |
256 | /* END OF PCM INTERFACE */ | |
257 | ||
258 | ||
259 | /* driver .remove -- destructor */ | |
a0f4aa0a | 260 | static void snd_sh_dac_remove(struct platform_device *devptr) |
9dcaa7b2 RIZ |
261 | { |
262 | snd_card_free(platform_get_drvdata(devptr)); | |
9dcaa7b2 RIZ |
263 | } |
264 | ||
265 | /* free -- it has been defined by create */ | |
266 | static int snd_sh_dac_free(struct snd_sh_dac *chip) | |
267 | { | |
268 | /* release the data */ | |
269 | kfree(chip->data_buffer); | |
270 | kfree(chip); | |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
275 | static int snd_sh_dac_dev_free(struct snd_device *device) | |
276 | { | |
277 | struct snd_sh_dac *chip = device->device_data; | |
278 | ||
279 | return snd_sh_dac_free(chip); | |
280 | } | |
281 | ||
282 | static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle) | |
283 | { | |
284 | struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac, | |
285 | hrtimer); | |
286 | struct snd_pcm_runtime *runtime = chip->substream->runtime; | |
287 | ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size); | |
288 | ||
289 | if (!chip->empty) { | |
290 | sh_dac_output(*chip->buffer_begin, chip->pdata->channel); | |
291 | chip->buffer_begin++; | |
292 | ||
293 | chip->processed++; | |
294 | if (chip->processed >= b_ps) { | |
295 | chip->processed -= b_ps; | |
296 | snd_pcm_period_elapsed(chip->substream); | |
297 | } | |
298 | ||
299 | if (chip->buffer_begin == (chip->data_buffer + | |
300 | chip->buffer_size - 1)) | |
301 | chip->buffer_begin = chip->data_buffer; | |
302 | ||
303 | if (chip->buffer_begin == chip->buffer_end) | |
304 | chip->empty = 1; | |
305 | ||
306 | } | |
307 | ||
308 | if (!chip->empty) | |
309 | hrtimer_start(&chip->hrtimer, chip->wakeups_per_second, | |
310 | HRTIMER_MODE_REL); | |
311 | ||
312 | return HRTIMER_NORESTART; | |
313 | } | |
314 | ||
315 | /* create -- chip-specific constructor for the cards components */ | |
e74033a8 BP |
316 | static int snd_sh_dac_create(struct snd_card *card, |
317 | struct platform_device *devptr, | |
318 | struct snd_sh_dac **rchip) | |
9dcaa7b2 RIZ |
319 | { |
320 | struct snd_sh_dac *chip; | |
321 | int err; | |
322 | ||
52b136d2 | 323 | static const struct snd_device_ops ops = { |
9dcaa7b2 RIZ |
324 | .dev_free = snd_sh_dac_dev_free, |
325 | }; | |
326 | ||
327 | *rchip = NULL; | |
328 | ||
329 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); | |
330 | if (chip == NULL) | |
331 | return -ENOMEM; | |
332 | ||
333 | chip->card = card; | |
334 | ||
335 | hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
336 | chip->hrtimer.function = sh_dac_audio_timer; | |
337 | ||
338 | dac_audio_reset(chip); | |
339 | chip->rate = 8000; | |
340 | dac_audio_set_rate(chip); | |
341 | ||
342 | chip->pdata = devptr->dev.platform_data; | |
343 | ||
344 | chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL); | |
345 | if (chip->data_buffer == NULL) { | |
346 | kfree(chip); | |
347 | return -ENOMEM; | |
348 | } | |
349 | ||
350 | err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); | |
351 | if (err < 0) { | |
352 | snd_sh_dac_free(chip); | |
353 | return err; | |
354 | } | |
355 | ||
356 | *rchip = chip; | |
357 | ||
358 | return 0; | |
359 | } | |
360 | ||
361 | /* driver .probe -- constructor */ | |
e74033a8 | 362 | static int snd_sh_dac_probe(struct platform_device *devptr) |
9dcaa7b2 RIZ |
363 | { |
364 | struct snd_sh_dac *chip; | |
365 | struct snd_card *card; | |
366 | int err; | |
367 | ||
e7182ac5 | 368 | err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card); |
9dcaa7b2 RIZ |
369 | if (err < 0) { |
370 | snd_printk(KERN_ERR "cannot allocate the card\n"); | |
371 | return err; | |
372 | } | |
373 | ||
374 | err = snd_sh_dac_create(card, devptr, &chip); | |
375 | if (err < 0) | |
376 | goto probe_error; | |
377 | ||
378 | err = snd_sh_dac_pcm(chip, 0); | |
379 | if (err < 0) | |
380 | goto probe_error; | |
381 | ||
382 | strcpy(card->driver, "snd_sh_dac"); | |
383 | strcpy(card->shortname, "SuperH DAC audio driver"); | |
384 | printk(KERN_INFO "%s %s", card->longname, card->shortname); | |
385 | ||
386 | err = snd_card_register(card); | |
387 | if (err < 0) | |
388 | goto probe_error; | |
389 | ||
c054c8a0 | 390 | snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio"); |
9dcaa7b2 RIZ |
391 | |
392 | platform_set_drvdata(devptr, card); | |
393 | return 0; | |
394 | ||
395 | probe_error: | |
396 | snd_card_free(card); | |
397 | return err; | |
398 | } | |
399 | ||
400 | /* | |
401 | * "driver" definition | |
402 | */ | |
d4c69838 | 403 | static struct platform_driver sh_dac_driver = { |
9dcaa7b2 | 404 | .probe = snd_sh_dac_probe, |
a0f4aa0a | 405 | .remove_new = snd_sh_dac_remove, |
9dcaa7b2 RIZ |
406 | .driver = { |
407 | .name = "dac_audio", | |
408 | }, | |
409 | }; | |
410 | ||
d4c69838 | 411 | module_platform_driver(sh_dac_driver); |