sh_dac_audio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #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 <rizurita@yahoo.com>");
  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. struct iov_iter *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_iter(chip->data_buffer + pos, count, 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_silence(struct snd_pcm_substream *substream,
  146. int channel, unsigned long pos,
  147. unsigned long count)
  148. {
  149. /* channel is not used (interleaved data) */
  150. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  151. memset(chip->data_buffer + pos, 0, 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
  160. snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
  161. {
  162. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  163. int pointer = chip->buffer_begin - chip->data_buffer;
  164. return pointer;
  165. }
  166. /* pcm ops */
  167. static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
  168. .open = snd_sh_dac_pcm_open,
  169. .close = snd_sh_dac_pcm_close,
  170. .prepare = snd_sh_dac_pcm_prepare,
  171. .trigger = snd_sh_dac_pcm_trigger,
  172. .pointer = snd_sh_dac_pcm_pointer,
  173. .copy = snd_sh_dac_pcm_copy,
  174. .fill_silence = snd_sh_dac_pcm_silence,
  175. };
  176. static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
  177. {
  178. int err;
  179. struct snd_pcm *pcm;
  180. /* device should be always 0 for us */
  181. err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
  182. if (err < 0)
  183. return err;
  184. pcm->private_data = chip;
  185. strscpy(pcm->name, "SH_DAC PCM");
  186. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
  187. /* buffer size=48K */
  188. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  189. NULL, 48 * 1024, 48 * 1024);
  190. return 0;
  191. }
  192. /* END OF PCM INTERFACE */
  193. /* driver .remove -- destructor */
  194. static void snd_sh_dac_remove(struct platform_device *devptr)
  195. {
  196. snd_card_free(platform_get_drvdata(devptr));
  197. }
  198. /* free -- it has been defined by create */
  199. static int snd_sh_dac_free(struct snd_sh_dac *chip)
  200. {
  201. /* release the data */
  202. kfree(chip->data_buffer);
  203. kfree(chip);
  204. return 0;
  205. }
  206. static int snd_sh_dac_dev_free(struct snd_device *device)
  207. {
  208. struct snd_sh_dac *chip = device->device_data;
  209. return snd_sh_dac_free(chip);
  210. }
  211. static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
  212. {
  213. struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
  214. hrtimer);
  215. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  216. ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
  217. if (!chip->empty) {
  218. sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
  219. chip->buffer_begin++;
  220. chip->processed++;
  221. if (chip->processed >= b_ps) {
  222. chip->processed -= b_ps;
  223. snd_pcm_period_elapsed(chip->substream);
  224. }
  225. if (chip->buffer_begin == (chip->data_buffer +
  226. chip->buffer_size - 1))
  227. chip->buffer_begin = chip->data_buffer;
  228. if (chip->buffer_begin == chip->buffer_end)
  229. chip->empty = 1;
  230. }
  231. if (!chip->empty)
  232. hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
  233. HRTIMER_MODE_REL);
  234. return HRTIMER_NORESTART;
  235. }
  236. /* create -- chip-specific constructor for the cards components */
  237. static int snd_sh_dac_create(struct snd_card *card,
  238. struct platform_device *devptr,
  239. struct snd_sh_dac **rchip)
  240. {
  241. struct snd_sh_dac *chip;
  242. int err;
  243. static const struct snd_device_ops ops = {
  244. .dev_free = snd_sh_dac_dev_free,
  245. };
  246. *rchip = NULL;
  247. chip = kzalloc_obj(*chip);
  248. if (chip == NULL)
  249. return -ENOMEM;
  250. chip->card = card;
  251. hrtimer_setup(&chip->hrtimer, sh_dac_audio_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  252. dac_audio_reset(chip);
  253. chip->rate = 8000;
  254. dac_audio_set_rate(chip);
  255. chip->pdata = devptr->dev.platform_data;
  256. chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
  257. if (chip->data_buffer == NULL) {
  258. kfree(chip);
  259. return -ENOMEM;
  260. }
  261. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  262. if (err < 0) {
  263. snd_sh_dac_free(chip);
  264. return err;
  265. }
  266. *rchip = chip;
  267. return 0;
  268. }
  269. /* driver .probe -- constructor */
  270. static int snd_sh_dac_probe(struct platform_device *devptr)
  271. {
  272. struct snd_sh_dac *chip;
  273. struct snd_card *card;
  274. int err;
  275. err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
  276. if (err < 0) {
  277. dev_err(&devptr->dev, "cannot allocate the card\n");
  278. return err;
  279. }
  280. err = snd_sh_dac_create(card, devptr, &chip);
  281. if (err < 0)
  282. goto probe_error;
  283. err = snd_sh_dac_pcm(chip, 0);
  284. if (err < 0)
  285. goto probe_error;
  286. strscpy(card->driver, "snd_sh_dac");
  287. strscpy(card->shortname, "SuperH DAC audio driver");
  288. dev_info(&devptr->dev, "%s %s\n", card->longname, card->shortname);
  289. err = snd_card_register(card);
  290. if (err < 0)
  291. goto probe_error;
  292. dev_info(&devptr->dev, "ALSA driver for SuperH DAC audio\n");
  293. platform_set_drvdata(devptr, card);
  294. return 0;
  295. probe_error:
  296. snd_card_free(card);
  297. return err;
  298. }
  299. /*
  300. * "driver" definition
  301. */
  302. static struct platform_driver sh_dac_driver = {
  303. .probe = snd_sh_dac_probe,
  304. .remove = snd_sh_dac_remove,
  305. .driver = {
  306. .name = "dac_audio",
  307. },
  308. };
  309. module_platform_driver(sh_dac_driver);