sh_dac_audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. struct snd_pcm_runtime *runtime = substream->runtime;
  163. if (copy_from_user_toio(chip->data_buffer + pos, src, count))
  164. return -EFAULT;
  165. chip->buffer_end = chip->data_buffer + pos + count;
  166. if (chip->empty) {
  167. chip->empty = 0;
  168. dac_audio_start_timer(chip);
  169. }
  170. return 0;
  171. }
  172. static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
  173. int channel, unsigned long pos,
  174. void *src, unsigned long count)
  175. {
  176. /* channel is not used (interleaved data) */
  177. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  178. struct snd_pcm_runtime *runtime = substream->runtime;
  179. memcpy_toio(chip->data_buffer + pos, src, count);
  180. chip->buffer_end = chip->data_buffer + pos + count;
  181. if (chip->empty) {
  182. chip->empty = 0;
  183. dac_audio_start_timer(chip);
  184. }
  185. return 0;
  186. }
  187. static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
  188. int channel, unsigned long pos,
  189. unsigned long count)
  190. {
  191. /* channel is not used (interleaved data) */
  192. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  193. struct snd_pcm_runtime *runtime = substream->runtime;
  194. memset_io(chip->data_buffer + pos, 0, count);
  195. chip->buffer_end = chip->data_buffer + pos + count;
  196. if (chip->empty) {
  197. chip->empty = 0;
  198. dac_audio_start_timer(chip);
  199. }
  200. return 0;
  201. }
  202. static
  203. snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
  204. {
  205. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  206. int pointer = chip->buffer_begin - chip->data_buffer;
  207. return pointer;
  208. }
  209. /* pcm ops */
  210. static const struct snd_pcm_ops snd_sh_dac_pcm_ops = {
  211. .open = snd_sh_dac_pcm_open,
  212. .close = snd_sh_dac_pcm_close,
  213. .ioctl = snd_pcm_lib_ioctl,
  214. .hw_params = snd_sh_dac_pcm_hw_params,
  215. .hw_free = snd_sh_dac_pcm_hw_free,
  216. .prepare = snd_sh_dac_pcm_prepare,
  217. .trigger = snd_sh_dac_pcm_trigger,
  218. .pointer = snd_sh_dac_pcm_pointer,
  219. .copy_user = snd_sh_dac_pcm_copy,
  220. .copy_kernel = snd_sh_dac_pcm_copy_kernel,
  221. .fill_silence = snd_sh_dac_pcm_silence,
  222. .mmap = snd_pcm_lib_mmap_iomem,
  223. };
  224. static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
  225. {
  226. int err;
  227. struct snd_pcm *pcm;
  228. /* device should be always 0 for us */
  229. err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
  230. if (err < 0)
  231. return err;
  232. pcm->private_data = chip;
  233. strcpy(pcm->name, "SH_DAC PCM");
  234. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
  235. /* buffer size=48K */
  236. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  237. snd_dma_continuous_data(GFP_KERNEL),
  238. 48 * 1024,
  239. 48 * 1024);
  240. return 0;
  241. }
  242. /* END OF PCM INTERFACE */
  243. /* driver .remove -- destructor */
  244. static int snd_sh_dac_remove(struct platform_device *devptr)
  245. {
  246. snd_card_free(platform_get_drvdata(devptr));
  247. return 0;
  248. }
  249. /* free -- it has been defined by create */
  250. static int snd_sh_dac_free(struct snd_sh_dac *chip)
  251. {
  252. /* release the data */
  253. kfree(chip->data_buffer);
  254. kfree(chip);
  255. return 0;
  256. }
  257. static int snd_sh_dac_dev_free(struct snd_device *device)
  258. {
  259. struct snd_sh_dac *chip = device->device_data;
  260. return snd_sh_dac_free(chip);
  261. }
  262. static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
  263. {
  264. struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
  265. hrtimer);
  266. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  267. ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
  268. if (!chip->empty) {
  269. sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
  270. chip->buffer_begin++;
  271. chip->processed++;
  272. if (chip->processed >= b_ps) {
  273. chip->processed -= b_ps;
  274. snd_pcm_period_elapsed(chip->substream);
  275. }
  276. if (chip->buffer_begin == (chip->data_buffer +
  277. chip->buffer_size - 1))
  278. chip->buffer_begin = chip->data_buffer;
  279. if (chip->buffer_begin == chip->buffer_end)
  280. chip->empty = 1;
  281. }
  282. if (!chip->empty)
  283. hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
  284. HRTIMER_MODE_REL);
  285. return HRTIMER_NORESTART;
  286. }
  287. /* create -- chip-specific constructor for the cards components */
  288. static int snd_sh_dac_create(struct snd_card *card,
  289. struct platform_device *devptr,
  290. struct snd_sh_dac **rchip)
  291. {
  292. struct snd_sh_dac *chip;
  293. int err;
  294. static struct snd_device_ops ops = {
  295. .dev_free = snd_sh_dac_dev_free,
  296. };
  297. *rchip = NULL;
  298. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  299. if (chip == NULL)
  300. return -ENOMEM;
  301. chip->card = card;
  302. hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  303. chip->hrtimer.function = sh_dac_audio_timer;
  304. dac_audio_reset(chip);
  305. chip->rate = 8000;
  306. dac_audio_set_rate(chip);
  307. chip->pdata = devptr->dev.platform_data;
  308. chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
  309. if (chip->data_buffer == NULL) {
  310. kfree(chip);
  311. return -ENOMEM;
  312. }
  313. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  314. if (err < 0) {
  315. snd_sh_dac_free(chip);
  316. return err;
  317. }
  318. *rchip = chip;
  319. return 0;
  320. }
  321. /* driver .probe -- constructor */
  322. static int snd_sh_dac_probe(struct platform_device *devptr)
  323. {
  324. struct snd_sh_dac *chip;
  325. struct snd_card *card;
  326. int err;
  327. err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
  328. if (err < 0) {
  329. snd_printk(KERN_ERR "cannot allocate the card\n");
  330. return err;
  331. }
  332. err = snd_sh_dac_create(card, devptr, &chip);
  333. if (err < 0)
  334. goto probe_error;
  335. err = snd_sh_dac_pcm(chip, 0);
  336. if (err < 0)
  337. goto probe_error;
  338. strcpy(card->driver, "snd_sh_dac");
  339. strcpy(card->shortname, "SuperH DAC audio driver");
  340. printk(KERN_INFO "%s %s", card->longname, card->shortname);
  341. err = snd_card_register(card);
  342. if (err < 0)
  343. goto probe_error;
  344. snd_printk(KERN_INFO "ALSA driver for SuperH DAC audio");
  345. platform_set_drvdata(devptr, card);
  346. return 0;
  347. probe_error:
  348. snd_card_free(card);
  349. return err;
  350. }
  351. /*
  352. * "driver" definition
  353. */
  354. static struct platform_driver sh_dac_driver = {
  355. .probe = snd_sh_dac_probe,
  356. .remove = snd_sh_dac_remove,
  357. .driver = {
  358. .name = "dac_audio",
  359. },
  360. };
  361. module_platform_driver(sh_dac_driver);