sh_dac_audio.c 11 KB

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