sh_dac_audio.c 11 KB

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