sh_dac_audio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 <[email protected]>
  6. *
  7. * Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh)
  8. */
  9. #include <linux/hrtimer.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <sound/core.h>
  16. #include <sound/initval.h>
  17. #include <sound/pcm.h>
  18. #include <sound/sh_dac_audio.h>
  19. #include <asm/clock.h>
  20. #include <asm/hd64461.h>
  21. #include <mach/hp6xx.h>
  22. #include <cpu/dac.h>
  23. MODULE_AUTHOR("Rafael Ignacio Zurita <[email protected]>");
  24. MODULE_DESCRIPTION("SuperH DAC audio driver");
  25. MODULE_LICENSE("GPL");
  26. /* Module Parameters */
  27. static int index = SNDRV_DEFAULT_IDX1;
  28. static char *id = SNDRV_DEFAULT_STR1;
  29. module_param(index, int, 0444);
  30. MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
  31. module_param(id, charp, 0444);
  32. MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
  33. /* main struct */
  34. struct snd_sh_dac {
  35. struct snd_card *card;
  36. struct snd_pcm_substream *substream;
  37. struct hrtimer hrtimer;
  38. ktime_t wakeups_per_second;
  39. int rate;
  40. int empty;
  41. char *data_buffer, *buffer_begin, *buffer_end;
  42. int processed; /* bytes proccesed, to compare with period_size */
  43. int buffer_size;
  44. struct dac_audio_pdata *pdata;
  45. };
  46. static void dac_audio_start_timer(struct snd_sh_dac *chip)
  47. {
  48. hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
  49. HRTIMER_MODE_REL);
  50. }
  51. static void dac_audio_stop_timer(struct snd_sh_dac *chip)
  52. {
  53. hrtimer_cancel(&chip->hrtimer);
  54. }
  55. static void dac_audio_reset(struct snd_sh_dac *chip)
  56. {
  57. dac_audio_stop_timer(chip);
  58. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  59. chip->processed = 0;
  60. chip->empty = 1;
  61. }
  62. static void dac_audio_set_rate(struct snd_sh_dac *chip)
  63. {
  64. chip->wakeups_per_second = 1000000000 / chip->rate;
  65. }
  66. /* PCM INTERFACE */
  67. static const struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
  68. .info = (SNDRV_PCM_INFO_MMAP |
  69. SNDRV_PCM_INFO_MMAP_VALID |
  70. SNDRV_PCM_INFO_INTERLEAVED |
  71. SNDRV_PCM_INFO_HALF_DUPLEX),
  72. .formats = SNDRV_PCM_FMTBIT_U8,
  73. .rates = SNDRV_PCM_RATE_8000,
  74. .rate_min = 8000,
  75. .rate_max = 8000,
  76. .channels_min = 1,
  77. .channels_max = 1,
  78. .buffer_bytes_max = (48*1024),
  79. .period_bytes_min = 1,
  80. .period_bytes_max = (48*1024),
  81. .periods_min = 1,
  82. .periods_max = 1024,
  83. };
  84. static int snd_sh_dac_pcm_open(struct snd_pcm_substream *substream)
  85. {
  86. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  87. struct snd_pcm_runtime *runtime = substream->runtime;
  88. runtime->hw = snd_sh_dac_pcm_hw;
  89. chip->substream = substream;
  90. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  91. chip->processed = 0;
  92. chip->empty = 1;
  93. chip->pdata->start(chip->pdata);
  94. return 0;
  95. }
  96. static int snd_sh_dac_pcm_close(struct snd_pcm_substream *substream)
  97. {
  98. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  99. chip->substream = NULL;
  100. dac_audio_stop_timer(chip);
  101. chip->pdata->stop(chip->pdata);
  102. return 0;
  103. }
  104. static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream *substream)
  105. {
  106. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  107. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  108. chip->buffer_size = runtime->buffer_size;
  109. memset(chip->data_buffer, 0, chip->pdata->buffer_size);
  110. return 0;
  111. }
  112. static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  113. {
  114. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  115. switch (cmd) {
  116. case SNDRV_PCM_TRIGGER_START:
  117. dac_audio_start_timer(chip);
  118. break;
  119. case SNDRV_PCM_TRIGGER_STOP:
  120. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  121. chip->processed = 0;
  122. chip->empty = 1;
  123. dac_audio_stop_timer(chip);
  124. break;
  125. default:
  126. return -EINVAL;
  127. }
  128. return 0;
  129. }
  130. static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
  131. int channel, unsigned long pos,
  132. void __user *src, unsigned long count)
  133. {
  134. /* channel is not used (interleaved data) */
  135. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  136. if (copy_from_user_toio(chip->data_buffer + pos, src, count))
  137. return -EFAULT;
  138. chip->buffer_end = chip->data_buffer + pos + count;
  139. if (chip->empty) {
  140. chip->empty = 0;
  141. dac_audio_start_timer(chip);
  142. }
  143. return 0;
  144. }
  145. static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
  146. int channel, unsigned long pos,
  147. void *src, unsigned long count)
  148. {
  149. /* channel is not used (interleaved data) */
  150. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  151. memcpy_toio(chip->data_buffer + pos, src, count);
  152. chip->buffer_end = chip->data_buffer + pos + count;
  153. if (chip->empty) {
  154. chip->empty = 0;
  155. dac_audio_start_timer(chip);
  156. }
  157. return 0;
  158. }
  159. static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
  160. int channel, unsigned long pos,
  161. unsigned long count)
  162. {
  163. /* channel is not used (interleaved data) */
  164. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  165. memset_io(chip->data_buffer + pos, 0, count);
  166. chip->buffer_end = chip->data_buffer + pos + count;
  167. if (chip->empty) {
  168. chip->empty = 0;
  169. dac_audio_start_timer(chip);
  170. }
  171. return 0;
  172. }
  173. static
  174. snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
  175. {
  176. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  177. int pointer = chip->buffer_begin - chip->data_buffer;
  178. return pointer;
  179. }
  180. /* pcm ops */
  181. static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
  182. .open = snd_sh_dac_pcm_open,
  183. .close = snd_sh_dac_pcm_close,
  184. .prepare = snd_sh_dac_pcm_prepare,
  185. .trigger = snd_sh_dac_pcm_trigger,
  186. .pointer = snd_sh_dac_pcm_pointer,
  187. .copy_user = snd_sh_dac_pcm_copy,
  188. .copy_kernel = snd_sh_dac_pcm_copy_kernel,
  189. .fill_silence = snd_sh_dac_pcm_silence,
  190. .mmap = snd_pcm_lib_mmap_iomem,
  191. };
  192. static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
  193. {
  194. int err;
  195. struct snd_pcm *pcm;
  196. /* device should be always 0 for us */
  197. err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
  198. if (err < 0)
  199. return err;
  200. pcm->private_data = chip;
  201. strcpy(pcm->name, "SH_DAC PCM");
  202. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
  203. /* buffer size=48K */
  204. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  205. NULL, 48 * 1024, 48 * 1024);
  206. return 0;
  207. }
  208. /* END OF PCM INTERFACE */
  209. /* driver .remove -- destructor */
  210. static int snd_sh_dac_remove(struct platform_device *devptr)
  211. {
  212. snd_card_free(platform_get_drvdata(devptr));
  213. return 0;
  214. }
  215. /* free -- it has been defined by create */
  216. static int snd_sh_dac_free(struct snd_sh_dac *chip)
  217. {
  218. /* release the data */
  219. kfree(chip->data_buffer);
  220. kfree(chip);
  221. return 0;
  222. }
  223. static int snd_sh_dac_dev_free(struct snd_device *device)
  224. {
  225. struct snd_sh_dac *chip = device->device_data;
  226. return snd_sh_dac_free(chip);
  227. }
  228. static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
  229. {
  230. struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
  231. hrtimer);
  232. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  233. ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
  234. if (!chip->empty) {
  235. sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
  236. chip->buffer_begin++;
  237. chip->processed++;
  238. if (chip->processed >= b_ps) {
  239. chip->processed -= b_ps;
  240. snd_pcm_period_elapsed(chip->substream);
  241. }
  242. if (chip->buffer_begin == (chip->data_buffer +
  243. chip->buffer_size - 1))
  244. chip->buffer_begin = chip->data_buffer;
  245. if (chip->buffer_begin == chip->buffer_end)
  246. chip->empty = 1;
  247. }
  248. if (!chip->empty)
  249. hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
  250. HRTIMER_MODE_REL);
  251. return HRTIMER_NORESTART;
  252. }
  253. /* create -- chip-specific constructor for the cards components */
  254. static int snd_sh_dac_create(struct snd_card *card,
  255. struct platform_device *devptr,
  256. struct snd_sh_dac **rchip)
  257. {
  258. struct snd_sh_dac *chip;
  259. int err;
  260. static const struct snd_device_ops ops = {
  261. .dev_free = snd_sh_dac_dev_free,
  262. };
  263. *rchip = NULL;
  264. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  265. if (chip == NULL)
  266. return -ENOMEM;
  267. chip->card = card;
  268. hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  269. chip->hrtimer.function = sh_dac_audio_timer;
  270. dac_audio_reset(chip);
  271. chip->rate = 8000;
  272. dac_audio_set_rate(chip);
  273. chip->pdata = devptr->dev.platform_data;
  274. chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
  275. if (chip->data_buffer == NULL) {
  276. kfree(chip);
  277. return -ENOMEM;
  278. }
  279. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  280. if (err < 0) {
  281. snd_sh_dac_free(chip);
  282. return err;
  283. }
  284. *rchip = chip;
  285. return 0;
  286. }
  287. /* driver .probe -- constructor */
  288. static int snd_sh_dac_probe(struct platform_device *devptr)
  289. {
  290. struct snd_sh_dac *chip;
  291. struct snd_card *card;
  292. int err;
  293. err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
  294. if (err < 0) {
  295. snd_printk(KERN_ERR "cannot allocate the card\n");
  296. return err;
  297. }
  298. err = snd_sh_dac_create(card, devptr, &chip);
  299. if (err < 0)
  300. goto probe_error;
  301. err = snd_sh_dac_pcm(chip, 0);
  302. if (err < 0)
  303. goto probe_error;
  304. strcpy(card->driver, "snd_sh_dac");
  305. strcpy(card->shortname, "SuperH DAC audio driver");
  306. printk(KERN_INFO "%s %s", card->longname, card->shortname);
  307. err = snd_card_register(card);
  308. if (err < 0)
  309. goto probe_error;
  310. snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
  311. platform_set_drvdata(devptr, card);
  312. return 0;
  313. probe_error:
  314. snd_card_free(card);
  315. return err;
  316. }
  317. /*
  318. * "driver" definition
  319. */
  320. static struct platform_driver sh_dac_driver = {
  321. .probe = snd_sh_dac_probe,
  322. .remove = snd_sh_dac_remove,
  323. .driver = {
  324. .name = "dac_audio",
  325. },
  326. };
  327. module_platform_driver(sh_dac_driver);